We're using flake8
to test our code, and we're using pytest
with fixtures. The following code:
from staylists.tests.fixtures import fixture1 # noqa: F401
def test_case(fixture1): # noqa: F811
# Test goes here
assert 1 == 1
Generates a lib/python/test.py:3:1: F811 redefinition of unused 'fixture1' from line 1
error during linting.
Flake8 and its plugins assign a code to each message that we refer to as an error code (or violation ). Most plugins will list their error codes in their documentation or README. Flake8 installs pycodestyle, pyflakes, and mccabe by default and generates its own error code s for pyflakes:
Subscribe to our YouTube Channel! Flake8 is a Python library that wraps PyFlakes, pycodestyle and Ned Batchelder’s McCabe script. It is a great toolkit for checking your code base against coding style (PEP8), programming errors (like “library imported but unused” and “Undefined name”) and to check cyclomatic complexity.
There are two ways to ignore the file: The former is the recommended way of ignoring entire files. By using our exclude list, we can include it in our configuration file and have one central place to find what files aren’t included in Flake8 checks.
See the full list of options. Just add # noqa in the end of the line. The example below is a .travis.yml file from a project of mine. You can run flake8 checks very easily just by adding the commands inside the script list. That’s it!
The F401 and F811 errors can be avoided by moving all fixtures into the conftest.py file. Pytest loads this file automatically and makes all fixtures inside available in all tests, even without explicit import statements.
More discussion about the file can be found here: In py.test, what is the use of conftest.py files?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With