gpt4 book ai didi

python - 根据 py.test 中的参数跳过测试

转载 作者:太空狗 更新时间:2023-10-29 22:10:59 24 4
gpt4 key购买 nike

我有一个带有 session 作用域的测试 fixture ,它是参数化的,例如

@pytest.fixture(scope="session", params=["one", "two", "three"])
def myfixture():
...

在我的目录中,我有使用 pytest.mark.usefixtures("myfixture") 的文件和一个包含测试的文件应该只为 myfixture 运行 "two" 参数和 py.test 应该跳过它否则。

有什么方法可以在 py.test 中实现这一点,或者我是否需要在 myfixture() 函数的某个类中设置一个特殊变量?

最佳答案

自己找到解决方案,可以在conftest.py中定义函数:

def pytest_namespace():
return {"param": None}

在 fixture 函数中我们可以做:

@pytest.fixture(scope="session", params=["one", "two", "three"])
def myfixture():
pytest.param = request.param
# ...

所以我们可以包装测试类:

@pytest.mark.skipif("pytest.param == 'value'")
class TestSmth(object):
...

关于python - 根据 py.test 中的参数跳过测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31437025/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com