pytest-flake8
pytest-flake8 is a pytest plugin that integrates flake8 into your test suite to check Python code against PEP8, PyFlakes, and McCabe complexity. As of version 1.3.0, it is actively maintained by coherent-oss with regular releases and supports Python >=3.9.
Warnings
- breaking pytest-flake8 has experienced breaking changes with various pytest versions. Specifically, changes in pytest 3.5.0 (JUnit XML output) and pytest 4.1.0 (deprecated functions) caused incompatibilities in older pytest-flake8 versions. Additionally, Python 2, 3.5, and 3.6 are no longer supported since pytest-flake8 v1.1.0.
- gotcha By default, pytest-flake8 caches clean results. This can lead to all flake8 tests appearing to 'skip' if no relevant files have changed, which might be confusing during development.
- gotcha pytest-flake8 does not provide a direct mechanism to configure *other* flake8 plugins (e.g., flake8-bugbear, flake8-docstrings) through its own pytest configuration options.
- gotcha Running pytest-flake8 concurrently with pytest-cov (for code coverage) can significantly increase test execution time.
- gotcha pytest-flake8-specific options (e.g., `flake8-ignore`, `flake8-max-line-length`) are read from the `[tool:pytest]` section in `setup.cfg` or the `[pytest]` or `[tool.pytest.ini_options]` sections in `pyproject.toml`. Placing these options directly under a generic `[flake8]` section may not be honored by the plugin.
Install
-
pip install pytest-flake8
Quickstart
# content of your_module.py
def long_line_function(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12):
pass
# content of setup.cfg or pyproject.toml
# For setup.cfg:
# [tool:pytest]
# flake8-max-line-length = 79
# For pyproject.toml (requires pytest>=6.0):
# [tool.pytest.ini_options]
# flake8-max-line-length = 79
# Run pytest with flake8 checks
# pytest --flake8 your_module.py