我对以下代码有问题-最终结果与预期不符:
env.yaml
config:
requestenv: [test, uat]
envinfo:
test: [{imp: 111, clk: 111, "act": 111}, {imp: 222, clk: 222, act: 222}]
uat: [{ imp: 333, clk: 333, act: 333 }, { imp: 444, clk: 444, act: 444 }]
测试环境py
import pytest
from utils import configutil
configpath = r".\monitor.request.config.yaml"
config = configutil.readyamlconfig(configpath)
requestenv = config["config"]["requestenv"]
@pytest.fixture(scope="function", params=requestenv)
def one(request):
env = request.param
return config["config"]["envinfo"][request.param]
@pytest.mark.parametrize("testdata", [pytest.lazy_fixture("one")])
def test_func01(testdata):
print()
print("*" * 10)
print(testdata)
测试数据总是附带一个env.yaml
配置文件,并且取决于测试环境(test
和uat
)。我想在运行pytest -s .\test_env.py
之后循环每个环境和该环境中的每个项目,如下所示:
[预期]
test_env.py::test_func01[test-{ imp: 111, clk: 111, act: 111 }]
test_env.py::test_func01[test-{ imp: 222, clk: 222, act: 222 }]
test_env.py::test_func01[saas-{ imp: 333, clk: 333, act: 333 }]
test_env.py::test_func01[saas-{ imp: 444, clk: 444, act: 444 }]
[实际]
test_env.py::test_func01[test-[[{imp: 111, clk: 111, "act": 111}, {imp: 222, clk: 222, act: 222}]]]
test_env.py::test_func01[test-[{ imp: 333, clk: 333, act: 333 }, { imp: 444, clk: 444, act: 444 }]]
我还不知道如何使用
lazy-fixture
实现这一点。问题是每个测试环境中的测试数量可能不同,我不知道如何在夹具中反映这一点。如果测试的数量相同(如您的示例中所示),则以下操作可行:如果数据的长度是可变的,唯一想到的是使用钩子手动参数化函数:
这两种变体都会生成预期的输出
也许我遗漏了一些东西,还有更好的解决办法
相关问题 更多 >
编程相关推荐