pytest-pylint
pytest-pylint is a pytest plugin that integrates Pylint, a Python static code analysis tool, into the pytest testing framework. It allows developers to run Pylint checks as part of their test suite, ensuring code quality and adherence to coding standards. The library is actively maintained, with regular updates to support newer versions of pytest, Pylint, and Python. The current version is 0.21.0.
Warnings
- breaking Version 0.21.0 dropped support for pytest versions older than 7.0 and Pylint versions older than 2.15. Version 0.20.0 was the last to support Python 3.7 and Pylint < 2.6. Ensure your environment meets the minimum version requirements.
- gotcha By default, `--pylint` runs Pylint for all message types. This can lead to many 'failures' from convention or refactor messages. To make Pylint failures more strict, configure `--pylint-error-types` to specify which message types (e.g., Errors and Fatal) should cause the pytest run to fail.
- gotcha When trying to run only Pylint checks and no other tests using `pytest --pylint -m pylint`, pytest might still attempt to collect other test modules. If these modules have import issues or other collection-time errors, pytest may report them, even though you intended to skip their execution.
- gotcha Configuring Pylint's `ignore-patterns` in a `.pylintrc` or `pyproject.toml` file is crucial for excluding non-Python files, generated code, or specific directories from linting. Incorrect configuration can lead to Pylint trying to parse unexpected file types or linting irrelevant code.
- gotcha Integrating `pytest-pylint` with `pytest-xdist` (for parallel test execution) might lead to performance degradation or unexpected behavior in some scenarios. Users have reported slowdowns when `xdist` is enabled with `pytest-pylint`.
Install
-
pip install pytest-pylint
Imports
- pytest-pylint is a plugin and not typically imported directly in user code.
Functionality is enabled via `pip install pytest-pylint` and invoked using pytest command-line options or configuration.
Quickstart
import pytest
# my_bad_module.py (example of code with a pylint warning)
def calculate(a, b):
result = a + b # W0612: Unused variable 'result'
return a + b
# To run pylint with pytest, save the above in 'my_bad_module.py'
# and create a dummy 'test_my_module.py' if needed for pytest discovery:
# touch test_my_module.py
# Then, run from your terminal:
# pytest --pylint my_bad_module.py
# Or to restrict error types:
# pytest --pylint --pylint-error-types=E,F my_bad_module.py
# Example .pylintrc content (optional, in project root):
# [MESSAGES CONTROL]
# disable=W0612
# Example pytest.ini content (optional, in project root):
# [pytest]
# addopts = --pylint --pylint-error-types=E,F