Pytest中的范围难题

Pytest中的范围难题,pytest,Pytest,例如,我有两个装置,其中第二个装置使用第一个作为参数 @pytest.fixture(scope='function') def fixture_1(): pass @pytest.fixture(scope='function') def fixture_2(fixture_1): pass 在这种情况下,一切正常 但是 如果我有3个测试类TestClass1,TestClass2,TestClass3 我需要使用不同范围的夹具2: TestClass1的功能范围 Tes

例如,我有两个装置,其中第二个装置使用第一个作为参数

@pytest.fixture(scope='function')
def fixture_1():
    pass

@pytest.fixture(scope='function')
def fixture_2(fixture_1):
    pass
在这种情况下,一切正常

但是

如果我有3个测试类
TestClass1
TestClass2
TestClass3

我需要使用不同范围的
夹具2

  • TestClass1的功能范围
  • TestClass2的类范围
  • TestClass3的模块范围
我怎么做

正如我现在看到的,我需要编写具有3个不同范围的3个版本的
fixture_1
,以及具有3个不同范围的3个版本的
fixture_2
。总共6个夹具,以便能够在不同测试类别的所有3个范围内使用
夹具2

有没有办法避免这种重复