How do I get Flake8 to work with F811 errors?
Asked Answered
P

2

11

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.

  • Why does it ignore the noqa flag?
  • Is there a better way to avoid flagging this error?
Pacificas answered 2/5, 2017 at 20:10 Comment(2)
Sounds like a bug. fixture1 in the parameter seems to be a bit misleading. The problem seems to be that you're ghosting fixture1 that you're importing and not using the parameter in the function anyway. You could simply rename the parameter if possible.Psittacosis
Fixtures don't work like that. They're made available to the function by the pytest framework based on the name matching.Pacificas
P
11

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?

Pacien answered 22/3, 2018 at 10:27 Comment(0)
A
4

There are two "best practices" for sharing fixtures:

  1. define them in a conftest above both test modules
    • pytest will automatically share them between the two tests
  2. define a pytest plugin which exposes fixtures
    • pytest will make your fixtures available to all tests

bringing a fixture into a scope via import side-effects will trigger the issues you're seeing and is not recommended

Ardrey answered 7/2, 2020 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.