我想知道是否可以使用一个测试生成的数据作为另一个测试的参数。在我的例子中,我需要修改变量(这是列表),如果我可以使用这个列表作为参数(运行与列表一样多的测试),那就太好了
这是代码(它不起作用,但也许你可以给我一些提示)
import pytest
class TestCheck:
x = [1, 4]
@classmethod
def setup_class(self):
print('here setup')
@classmethod
def teardown_class(self):
print('here is finish')
def test_5(self):
self.x.append(6)
assert 1 == 2
@pytest.mark.parametrize("region", x)
def test_6(self, region):
assert region > 5, f"x: {self.x}"
输出:
FAILED sandbox_tests3.py::TestCheck::test_5 - assert 1 == 2
FAILED sandbox_tests3.py::TestCheck::test_6[1] - AssertionError: x: [1, 4, 6]
FAILED sandbox_tests3.py::TestCheck::test_6[4] - AssertionError: x: [1, 4, 6]
所以看起来 x 中有好的值,但是在夹具中新值不可见。
我也尝试使用 pytest_cases,但结果非常相似。
如有任何帮助,我们将不胜感激
class TestParamPropagatable:
DATA1 = None
def test_fetch_data(self, request):
cls = __class__
cls.DATA1 = 'hello'
DATA2 = None
def test_fetch_data(self, request):
cls = __class__
cls.DATA2 = cls.DATA1 + ' world'
您可以使用装饰器等进一步简化 cls 分配(第一行)