fixture factories
must be suffixed with "_factory"
Bad code (user
fixture should be renamed to user_factory
):
import pytest
class User:
def __init__(self, first_name, last_name, middle_name=None, email=None, age=None):
self.first_name = first_name
self.last_name = last_name
self.middle_name = middle_name
self.email = email
self.age = age
@pytest.fixture()
def user():
defaults = {"fist_name": "John", "last_name": "Doe"}
def with_params(**overrides):
return User(**defaults, **overrides)
return with_params
Good code:
import pytest
class User:
def __init__(self, first_name, last_name, middle_name=None, email=None, age=None):
self.first_name = first_name
self.last_name = last_name
self.middle_name = middle_name
self.email = email
self.age = age
@pytest.fixture()
def user_factory():
defaults = {"fist_name": "John", "last_name": "Doe"}
def with_params(**overrides):
return User(**defaults, **overrides)
return with_params
- to separate fixtures from fixture factories in a codebase