我正在尝试使用pytest在不同类之间共享类范围和方法范围的固定装置。目前,如果尝试从另一个类中添加固定装置,则会出现语法错误,如下所示。
我知道解决此问题的一种方法是不要将类包含在灯具中。但是,将灯具封闭起来对于我想做的事情很有意义。
import pytest
class Test_example1(object):
@classmethod
@pytest.fixture(scope='class')
def example1_fixture(self):
print("example1_fixture setup")
yield
print("example1_fixture teardown")
def test_1(self, example1_fixture):
print("class example1::test_1")
# This works if I comment out the code causing errors
@pytest.fixture(scope='class')
def global_example_fixture():
print("global_example_fixture setup")
yield
print("global_example_fixture teardown")
class Test_example2(object):
@pytest.fixture(scope='class')
def example2_fixture(self):
print("example2_fixture setup")
yield
print("example2_fixture teardown")
# Results in fixture not found error
def test_1(self, example1_fixture):
print("class example2::test_1")
# Results in syntax error
def test_2(self, Test_example1.example1_fixture, global_example_fixture):
print("class example2::test_1")
# This works...
def test_2(self, example2_fixture, global_example_fixture):
print("class example2::test_2")
我希望能够跨类调用类级别和方法级别的固定装置。