pytest-faker
raw JSON → 2.0.0 verified Fri May 01 auth: no python
pytest-faker is a plugin for pytest that provides access to Faker fixtures for generating fake data in tests. Current version is 2.0.0. The library is actively maintained by the pytest-dev organization, with releases following changes to Faker and pytest.
pip install pytest-faker Common errors
error ModuleNotFoundError: No module named 'faker' ↓
cause Faker library not installed; pytest-faker only depends on Faker via runtime, not install dependency.
fix
Run: pip install Faker
error fixture 'faker' not found ↓
cause Caused by using pytest-faker with pytest < 3.0 or not declaring plugin in conftest.py or pytest.ini.
fix
Update pytest to 3.0+ or add 'pytest-faker' to pytest_plugins in conftest.py: pytest_plugins = ['pytest_faker']
error AttributeError: 'Faker' object has no attribute 'seed' ↓
cause Using deprecated seed() method in newer versions of Faker.
fix
Replace with faker.seed_instance(seed_value) or use the 'faker_seed' fixture.
Warnings
breaking Version 1.0 used 'fake' fixture; version 2.0 changed to 'faker'. ↓
fix Rename fixture usage from 'fake' to 'faker'.
deprecated Faker's 'seed' method is deprecated; use 'seed_instance' or configure via pytest-faker's seed fixture. ↓
fix Use 'faker.seed_instance(12345)' or the 'faker_seed' fixture.
gotcha The 'faker' fixture uses a new Faker instance per test by default, but seeding with 'faker_seed' can cause shared state if not careful. ↓
fix Use 'faker_seed' fixture per test function to control reproducibility without cross-test contamination.
Imports
- faker fixture wrong
from faker import Faker; fake = Faker()correctdef test_example(faker):
Quickstart
import pytest
def test_name(faker):
name = faker.name()
assert isinstance(name, str)
def test_email(faker):
email = faker.email()
assert '@' in email