flake8-pytest-style
flake8-pytest-style is a flake8 plugin designed to enforce common style guidelines and identify inconsistencies within pytest-based test suites. It helps maintain clean and idiomatic pytest code by reporting a variety of style violations. The current version is 2.2.0, released in October 2025, and it maintains a somewhat regular release cadence, with major/minor updates typically every 3-9 months.
Common errors
-
PT001 use @pytest.fixture over @pytest.fixture()
cause After upgrading to `flake8-pytest-style` v2.0.0+, the default for `pytest-fixture-no-parentheses` was inverted. It now prefers `@pytest.fixture` without parentheses when no arguments are passed.fixChange `@pytest.fixture()` to `@pytest.fixture` in your code, or set `pytest-fixture-no-parentheses = false` in your `flake8` configuration to revert to the old behavior. -
PT013 found incorrect import of pytest, use simple 'import pytest' instead
cause You are using `from pytest import ...` (e.g., `from pytest import fixture`) which `flake8-pytest-style` discourages.fixReplace specific imports from `pytest` with `import pytest` and then use `pytest.fixture`, `pytest.mark`, etc. -
PT028 default values in test functions
cause A test function parameter has a default value, which conflicts with pytest's fixture injection mechanism. Pytest will use the default value instead of the fixture.fixRemove the default value from the test function parameter. For example, change `def test_foo(bar=42):` to `def test_foo(bar):`. -
F401 'my_fixture' imported but unused
cause This is a standard `flake8` (PyFlakes) error, often seen when importing pytest fixtures from a separate file (not `conftest.py`) into your test module. Fixtures are used by injection, not direct calls, making the import appear 'unused' to `flake8`.fixThe recommended approach is to define reusable fixtures in a `conftest.py` file or use `pytest_plugins = [...]` in `conftest.py` to load them. If you must import, add `# noqa: F401` to the import line to suppress the warning.
Warnings
- breaking Version 2.0.0 (released 2024-04-01) inverted the default values for `pytest-fixture-no-parentheses` (PT001) and `pytest-mark-no-parentheses` (PT023). This change aligns with the official pytest style, but may introduce many new violations upon upgrade.
- breaking The minimum Python version requirement has increased across minor and major versions. Version 2.0.0 requires Python >=3.8.1, version 2.1.0 requires Python >=3.9, and version 2.2.0 requires Python >=3.10.
- gotcha Rules PT004 (fixture without return value should have leading underscore) and PT005 (fixture returning value should not have leading underscore) have been noted by the Ruff linter team (a popular alternative to flake8) as potentially not aligning with all pytest 'best practices'.
- gotcha Rule PT028 warns against defining default values for parameters in test functions (e.g., `def test_foo(bar=42):`). Pytest's behavior is to use the default value instead of injecting a fixture if one is defined for that parameter, leading to unexpected test behavior.
Install
-
pip install flake8-pytest-style
Imports
- pytest
from pytest import fixture, mark
import pytest
Quickstart
import os
# Create a dummy test file
with open('test_example.py', 'w') as f:
f.write("""
import pytest
@pytest.fixture
def my_fixture(scope='function'): # PT001, PT003
return 1
def test_example(my_fixture): # PT004
assert my_fixture == 1
@pytest.mark.parametrize(('val'), [1,2]) # PT023, PT006
def test_parametrized_bad_style(val=10): # PT028
assert val > 0
def test_raises():
with pytest.raises(Exception):
pass # PT010, PT011, PT012
""")
# Run flake8 with the plugin enabled
# flake8 automatically discovers installed plugins
print("\n--- Running flake8 ---")
os.system("flake8 test_example.py")
# Clean up
os.remove('test_example.py')