gpt4 book ai didi

python - 如何参数化测试以在 pytest 中使用不同的装置运行?

转载 作者:行者123 更新时间:2023-12-01 15:23:30 26 4
gpt4 key购买 nike

来自 pytest documentation :

@pytest.mark.parametrize allows one to define multiple sets of arguments and fixtures at the test function or class.



看起来这意味着 pytest.mark.parametrize可以将测试标记为使用多组装置运行吗?我可以找到很多参数化参数的例子,但我不知道如何参数化不同的装置集。

我想 this answer接近,但这实际上只是参数化参数,然后解决测试主体中的不同 fixture 。

是否可以使用不同的装置集将测试标记为多次运行?

注意我正在尝试做这样的事情:
import pytest

# some data fixutres
@pytest.fixture()
def data1():
"""Create some data"""

@pytest.fixture()
def data2():
"""Create some different data"""

@pytest.fixture()
def data3():
"""Create yet different data"""


# The tests
@pytest.mark.parametrize('data', [data1, data2])
def test_foo(data):
"""Test something that makes sense with datasets 1 and 2"""

@pytest.mark.parametrize('data', [data2, data3])
def test_bar(data):
"""Test something that makes sense with datasets 2 and 3"""

最佳答案

您可以使用 pytest-lazy-fixture 执行此操作插入:

import pytest
from pytest_lazyfixture import lazy_fixture

@pytest.fixture()
def fixture1():
return "data1"

@pytest.fixture()
def fixture2():
return "data2"

@pytest.fixture()
def fixture3():
return "data3"

@pytest.mark.parametrize("data", [lazy_fixture("fixture1"),
lazy_fixture("fixture2")])
def test_foo(data):
assert data in ("data1", "data2")

@pytest.mark.parametrize("data", [lazy_fixture("fixture2"),
lazy_fixture("fixture3")])
def test_bar(data):
assert data in ("data2", "data3")

请注意,有人建议将类似的功能直接添加到 pytest: https://docs.pytest.org/en/latest/proposals/parametrize_with_fixtures.html

关于python - 如何参数化测试以在 pytest 中使用不同的装置运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56307329/

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