python - 参数化后如何运行拆卸功能/fixture

标签 python parameters automated-tests pytest fixtures

我正在尝试为一个测试用例的整个参数化运行拆解函数/fixture 。所以我想在我的 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

最佳答案

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

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 类使用 fixture foo,您甚至可以将 foo 的定义放在类中。

关于python - 参数化后如何运行拆卸功能/fixture ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55492529/

相关文章:

python - 删除每行的特定列

c# - 为采用参数/参数的类创建单例工厂

ruby - 超时::在 ruby​​ 中使用 selenium-webdriver 时出错

testing - 使用 Watir 创建测试 "framework"的提示?

python - 尝试使用 python 和 openimageIO 提取子图像失败

python - 银行账户类。如何让它更强大/更高效? (面向对象)

python - 为什么我没有看到 Numpy 的 DeprecationWarning?

java - Java中带有通用参数的for循环

c++ - 通过引用传递是作为指针传递的特例吗?

java - 使用 selenium webdriver 查找表行号