Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

py.test deal with both pylint and flake8 when importing features from a module

Tags:

python

pytest

So, I have roughly this code:

from .fixtures import some_fixture


def test_whatever(some_fixture):
    print(some_fixture)

I get two warnings from flake8 for this:

F401 '.fixtures.some_fixture' imported but unused

and

F811 redefinition of unused 'some_fixture' from line 1

I'm not going to move fixtures anywhere, but "decorating" every test definition and every import with noqa and pylint comments seems like a very sad and colorless life (especially so that sometimes it will silence a legitimate warning, s.a. when a fixture isn't really used).

What else can I do?

like image 637
wvxvw Avatar asked Nov 09 '17 08:11

wvxvw


2 Answers

The better way is to place your fixtures in the conftest.py file which is the recommended location for shared fixtures.

They will be automatically discovered by pytest. You won't have to import them so no F401 and since the arguments won't be colliding with the imports no more F811.

like image 100
Abid H. Mujtaba Avatar answered Sep 21 '22 17:09

Abid H. Mujtaba


Use flake8's and pylint's directives to disable checks:

from .fixtures import some_fixture  # noqa: F401; pylint: disable=unused-variable

def test_whatever(some_fixture):
    print(some_fixture)

There is no way around that.

like image 41
phd Avatar answered Sep 23 '22 17:09

phd