参数化后如何运行拆卸功能/夹具
How to run teardown function / fixture after parametrize
我正在尝试 运行 拆卸功能/夹具以对一个测试用例进行整体参数化。所以我想在我的 conftest.py 文件中有这样的东西
@pytest.fixture(autouse=True, scope="parametrization")
def parametrization_scope():
print("First run of parametrization fixture")
# Executing 100 parametrizations
yield
print("Yield run of parametrization fixture")
在我的 test.py 中类似
@pytest.mark.parametrize(n-params)
# run initiate fixture here of parametrization
def test():
# executing n number of times
# run yield fixture here of parametrization
您可以使用测试 class 解决缺少的“参数化”范围,如下所示:
import pytest
@pytest.fixture(scope="class")
def foo():
print("setup")
yield None
print("teardown")
class TestSum:
@pytest.mark.parametrize(
"a,b,c",
[(3, 5, 8), (2, 4, 6)],
)
def test_eval(self, a, b, c, foo):
assert a + b == c
如果只有 TestSum class 使用夹具 foo,您甚至可以将 foo 的定义放在 class.
中
我正在尝试 运行 拆卸功能/夹具以对一个测试用例进行整体参数化。所以我想在我的 conftest.py 文件中有这样的东西
@pytest.fixture(autouse=True, scope="parametrization")
def parametrization_scope():
print("First run of parametrization fixture")
# Executing 100 parametrizations
yield
print("Yield run of parametrization fixture")
在我的 test.py 中类似
@pytest.mark.parametrize(n-params)
# run initiate fixture here of parametrization
def test():
# executing n number of times
# run yield fixture here of parametrization
您可以使用测试 class 解决缺少的“参数化”范围,如下所示:
import pytest
@pytest.fixture(scope="class")
def foo():
print("setup")
yield None
print("teardown")
class TestSum:
@pytest.mark.parametrize(
"a,b,c",
[(3, 5, 8), (2, 4, 6)],
)
def test_eval(self, a, b, c, foo):
assert a + b == c
如果只有 TestSum class 使用夹具 foo,您甚至可以将 foo 的定义放在 class.
中