Skip to content

Latest commit

 

History

History
59 lines (39 loc) · 1.17 KB

File metadata and controls

59 lines (39 loc) · 1.17 KB

PF001

fixture factories must be suffixed with "_factory"

Examples

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

Rationale

  • to separate fixtures from fixture factories in a codebase