当从模块导入特征时,py.test处理pylint和flake8

wvx*_*xvw 10 python pytest

所以,我大致有这个代码:

from .fixtures import some_fixture


def test_whatever(some_fixture):
    print(some_fixture)
Run Code Online (Sandbox Code Playgroud)

我得到两个警告flake8:

F401'.fixtures.some_fixture'导入但未使用

F811从第1行重新定义未使用的'some_fixture'

我不打算在任何地方移动灯具,但是"装饰"每个测试定义和每个导入noqapylint评论似乎是一个非常悲伤和无色的生活(特别是有时它会使一个合法的警告沉默,当一个夹具不是真的用过).

我还可以做些什么?

Abi*_*aba 14

更好的方法是将您的灯具放在conftest.py文件中,这是共享灯具的推荐位置。

它们会被 pytest 自动发现。您不必导入它们,因此没有 F401,并且由于参数不会与导入发生冲突,因此不再有 F811。


phd*_*phd 6

使用flake8和pylint的指令来禁用检查:

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

def test_whatever(some_fixture):
    print(some_fixture)
Run Code Online (Sandbox Code Playgroud)

没有办法解决这个问题.

  • `conftest.py` 更干净,因为它避免了抑制警告,但也意味着该目录或更深目录中的任何文件都会继承固定装置。这可能不是我们所希望的。 (4认同)