我如何解决从另一个模块导入的 pytest 夹具的 flake8“未使用的导入”错误

Diy*_*bek 6 python pytest flake8

我在 Fixtures.py 文件中编写了 pytest 夹具,并在 main_test.py 中使用它。但我在 flake8 中收到此错误:F401 'utils_test.backup_path' 已导入但未用于此代码:

@pytest.fixture
def backup_path():
    ...
Run Code Online (Sandbox Code Playgroud)
from fixtures import backup_path


def test_filename(backup_path):
    ...
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

Ant*_*ile 9

一般来说你不应该这样做

通过导入副作用使固定装置可用是固定装置如何工作的无意实现细节,并且可能会在 pytest 的未来中断

如果您想继续这样做,您可以使用# noqa: F403在导入上使用,告诉 flake8 忽略未使用的导入(尽管 linter 在这里告诉你正确的事情!)

制作可重复使用的固定装置的支持方法是将它们放置在conftest.py所有需要它的测试上方的目录中。测试将自动将这些装置置于“范围内”

如果该文件变得太大,您可以在插件模块中编写您的装置,并通过pytest_plugins = ['module.name']在您的conftest.py


免责声明:我是当前的 flake8 维护者,我也是 pytest 核心开发人员