重用 pytest fixtures

Reuse pytest fixtures

我正在使用 pytest 编写一些测试,其中许多测试都有类似的装置。我想将这些 "global" 固定装置放在一个文件中,以便它们可以在多个测试文件中重复使用。我的第一个想法是创建一个 fixtures.py 文件,例如

import pytest


@pytest.fixture()
def my_fixture():
    # do something

现在如何在 my_tests.py 中使用这个夹具?

def test_connect(my_fixture):
    pass

这给出 fixture 'my_fixture' not found。我可以from fixtures import my_fixture。对于这种情况,建议的解决方案是什么?

Pytest 将自动共享 conftest.py 中的固定装置。将共享夹具从 fixtures.py 移动到 conftest.py