【发布时间】:2018-03-20 01:36:49
【问题描述】:
import pytest
def add(x):
return x + 1
def sub(x):
return x - 1
testData1 = [1, 2]
testData2 = [3]
class Test_math(object):
@pytest.mark.parametrize('n', testData1)
def test_add(self, n):
result = add(n)
testData2.append(result) <-------- Modify testData here
assert result == 5
@pytest.mark.parametrize('n', testData2)
def test_sub(self, n):
result = sub(n)
assert result == 3
if __name__ == '__main__':
pytest.main()
在这种情况下只执行了 3 个测试:Test_math.test_add[1],Test_math.test_add[2],Test_math.test_sub[3]。
Test_math.test_sub 仅使用预定义数据 [3] 执行,这不是我的预期 [2,3,3]。如何解决?
更新 [1,2,3]-> [2,3,3]
【问题讨论】:
-
为什么
[1,2,3]是你的期望?你给test_sub列表testData2,它只有一个元素长。好的,我明白你为什么会期待不同的东西了。 -
我认为
testData2.append(result)会修改testData2,新的testData2会传递给下一次执行,但它没有 -
我认为这是一个糟糕的设计(答案中有更多详细信息),可以通过将
testData1和testData2作为类属性来实现这一点(尚未测试)。 -
很遗憾,它不起作用
标签: python pytest python-unittest parameterized-unit-test