我对下面的代码有疑问,最终结果与预期不符,请帮助!
代码如下:
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 }]
test_env.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配置文件,并且测试数据取决于测试环境,在我的情况下,它是测试和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 }]
[Actual]
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 }]]
我不确定您要完成什么,但是如果您想要像第二个一样进行第一个测试,则必须提供与夹具参数相同的数组:
import pytest
data = [{"imp": 111, "clk": 111, "act": 111}, {"imp": 222, "clk": 222, "act": 222}]
@pytest.fixture(scope="function", params=data)
def one(request):
return request.param
@pytest.mark.parametrize("testdata", [pytest.lazy_fixture("one")])
def test_func01(testdata):
print()
print("*" * 10)
print(testdata)
@pytest.mark.parametrize("testdata", data)
def test_func02(testdata):
print()
print("*" * 10)
print(testdata)
这将导致test_func01
和test_func02
的输出相同。