Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 参数化后如何运行拆卸功能/夹具_Python_Parameters_Automated Tests_Pytest_Fixtures - Fatal编程技术网

Python 参数化后如何运行拆卸功能/夹具

Python 参数化后如何运行拆卸功能/夹具,python,parameters,automated-tests,pytest,fixtures,Python,Parameters,Automated Tests,Pytest,Fixtures,我正在尝试为一个测试用例的整个参数化运行拆卸功能/夹具。所以我想在conftest.py文件中有这样的内容 @pytest.fixture(autouse=True, scope="parametrization") def parametrization_scope(): print("First run of parametrization fixture") # Executing 100 parametrizations yield print("Yiel

我正在尝试为一个测试用例的整个参数化运行拆卸功能/夹具。所以我想在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")
在我的测试中.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

您可以使用测试类来解决缺少的“参数化”范围,如下所示:

导入pytest
@pytest.夹具(scope=“class”)
def foo():
打印(“设置”)
一无所获
打印(“拆卸”)
类TestSum:
@pytest.mark.parametize(
“a,b,c”,
[(3, 5, 8), (2, 4, 6)],
)
def测试评估(自身、a、b、c、foo):
断言a+b==c

如果只有TestSum类使用fixture foo,您甚至可以将foo的定义放在类中。

在fixture中的
yield
之后放置用于拆卸的代码。是的,如果存在范围“参数化”,这将起作用函数“作用域在每次测试用例运行之前/之后运行”,“模块”作用域在整个模块/文件运行之前/之后运行,“会话”作用域用于在整个会话运行之前/之后运行。我只需要在参数化之前/之后是
参数化
甚至是一个有效的范围?它不是。我只是举了一个例子,说明我在这里努力取得的成功。因此,与其他fixture作用域的行为类似。我已经做了一些变通,为我的上一次配置(列表[-1])提供了一个名为“last_run”且其值设置为“True”的键,并在测试用例中检查此类键的存在性,而不是在“last_run”为True时执行teardown函数……不过,您可以做的是在hooks中实现自定义逻辑:在测试集合之前调用(因此也是参数化)运行,并在测试收集完成后调用(所有测试都被参数化),但在实际测试执行开始之前。哇,@bnjmnp,好主意和解决方案。遗憾的是,我已经用另一种测试逻辑的方式解决了这一问题,它不需要这个。但仍然是好解决方案:)