有个不太正经的问题,俩个测试函数有依赖( test_equals )( test_zerodivision ), 我想在执行 test_equals 后修改 test_zerodivision 的入参,但是 pytest_generate_tests 已经根据 param 把这个入参给生成好了。有什么办法在执行了 test_equal 后然后修改 param 重新执行 pytest_generate_tests,或者直接修改 pytest_generate_tests 生成的入参(但是我实在找不到这个东西在哪里修改,他这个黑魔法方法,太玄幻)。
(我有办法避免这个问题,就是拆开来,但是我还是想知道下,能不能直接修改这个值,或者执行的过程中 reload 这个 class 的 param,重新生成参数)
# doc 地址 https://docs.pytest.org/en/latest/example/parametrize.html#paramexamples import pytest def pytest_generate_tests(metafunc): # called once per each test function funcarglist = metafunc.cls.params[metafunc.function.__name__] argnames = sorted(funcarglist[0]) metafunc.parametrize(argnames, [[funcargs[name] for name in argnames] for funcargs in funcarglist]) class TestClass(object): # a map specifying multiple argument sets for a test method params = { 'test_equals': [dict(a=1, b=2), dict(a=3, b=3), ], 'test_zerodivision': [dict(a=1, b=0), ], } #现在我想执行了这个函数把 'test_zerodivision': [dict(a=1, b=0), ]改成 'test_zerodivision': [dict(a=1, b=1), ] def test_equals(self, a, b): assert a == b def test_zerodivision(self, a, b): with pytest.raises(ZeroDivisionError): a / b
![]() | 1 Valkyrie0 2019-01-31 09:45:08 +08:00 |