我正在尝试嵌套参数化测试。下面的代码可以实现,但我只想在param1发生更改时执行代码(“print param1”非常耗时)
@pytest.mark.parametrize("param3", ["p31", "p32"])
@pytest.mark.parametrize("param2", ["p21", "p22"])
@pytest.mark.parametrize("param1", ["p11", "p12"])
def test_one(param1, param2, param3):
print param1 # goal is to run this only when param1 changes
print param2, param3
我尝试了这个,但它似乎不起作用:
@pytest.mark.parametrize("param1", ["p11", "p12"])
def test_one(param1, param2, param3):
print param1 # goal is to run this only when param1 changes
@pytest.mark.parametrize("param3", ["p31", "p32"])
@pytest.mark.parametrize("param2", ["p21", "p22"])
def test_two(param2, param3):
print param2, param3
有人有想法吗?
答案 0 :(得分:2)
一位同事给了我一个解决方案:
@pytest.fixture(scope="class", params=["B1","B2"])
def two(request):
print "\n SETUP", request.param
yield request.param
#print "\n UNDO", request.param
@pytest.fixture(scope="class", params=["A1", "A2"])
def one(request):
print "\n SETUP", request.param
yield request.param
#print "\n UNDO", request.param
class Test_myclass():
@pytest.mark.parametrize("param4", ["D1", "D2"])
@pytest.mark.parametrize("param3", ["C1", "C2"])
def test_three(self, one, two, param3, param4):
print "\n ({0} {1}) RUN ".format(one, two), param3, param4,
答案 1 :(得分:0)
为了补充 egabro's answer,您可以使用 "module"
(或 "session"
)范围来达到相同的目标,而无需将测试嵌套在类中:
import pytest
@pytest.fixture(scope="module", params=["B1","B2"])
def two(request):
print("\n SETUP", request.param)
return request.param
#print "\n UNDO", request.param
@pytest.fixture(scope="module", params=["A1", "A2"])
def one(request):
print("\n SETUP", request.param)
return request.param
#print "\n UNDO", request.param
@pytest.mark.parametrize("param4", ["D1", "D2"])
@pytest.mark.parametrize("param3", ["C1", "C2"])
def test_three(one, two, param3, param4):
print("\n ({0} {1}) RUN ".format(one, two))