Skip to main content
 首页 » 编程设计

python之PyTest 仅参数化某些排列

2024年10月01日10zdz8207

我要参数化三个字段:

@pytest.mark.parametrize("country", ['US', 'Canada', 'Mexico']) 
@pytest.mark.parametrize("city", ['Chicago', 'Atlanta', 'Mexico City']) 
@pytest.mark.parametrize("street", ['Washington Ave', 'Peachtree']) 
def test_mytest(country, city, street): 
    # ... 
    # assert things 

有没有一种方法可以让我自上而下构建排列,并将较低级别留空以进行某些测试?我想得到这样的参数:

Country     City        Street 
US          None        None 
US          Chicago     None 
US          Chicago     Washington Ave 
US          Chicago     Peachtree 
US          Atlanta     None 
US          Atlanta     Washington Ave 
US          Atlanta     Peachtree 
US          Mexico City None 
US          Mexico City Washington Ave 
US          Mexico City Peachtree 
 
etc... 

如果有帮助,请将其视为依赖于 3 个框的下拉列表。在前面的框有值之前,框 2 和框 3 不能有值。

如果我在 city 或 street 声明中放置“None”,则 None 会在不应该出现的时候出现并生成像这样的无效案例:

Country     City        Street 
US          None        Peachtree 

我可以使用 parameterize 得到我想要的吗?

请您参考如下方法:

我发现解决此问题的最简单方法是使用 pytest.skip。

测试.py

import pytest 
 
@pytest.mark.parametrize("street", [None, 'Washington Ave', 'Peachtree']) 
@pytest.mark.parametrize("city", [None, 'Chicago', 'Atlanta', 'Mexico City']) 
@pytest.mark.parametrize("country", ['US', 'Canada', 'Mexico']) 
def test(country, city, street): 
    if not city and street: 
        pytest.skip('Invalid case') 
    assert True 

测试结果:

$ pytest -vvv test.py  
============================== test session starts =============================== 
platform linux -- Python 3.5.2, pytest-3.2.1, py-1.4.34, pluggy-0.4.0 -- /home/kris/.virtualenvs/tmp/bin/python3 
cachedir: .cache 
rootdir: /home/kris/projects/tmp, inifile: 
plugins: mock-1.6.2 
collected 36 items                                                                 
 
test.py::test[US-None-None] PASSED 
test.py::test[US-None-Washington Ave] SKIPPED 
test.py::test[US-None-Peachtree] SKIPPED 
test.py::test[US-Chicago-None] PASSED 
test.py::test[US-Chicago-Washington Ave] PASSED 
test.py::test[US-Chicago-Peachtree] PASSED 
test.py::test[US-Atlanta-None] PASSED 
test.py::test[US-Atlanta-Washington Ave] PASSED 
test.py::test[US-Atlanta-Peachtree] PASSED 
test.py::test[US-Mexico City-None] PASSED 
test.py::test[US-Mexico City-Washington Ave] PASSED 
test.py::test[US-Mexico City-Peachtree] PASSED 
test.py::test[Canada-None-None] PASSED 
test.py::test[Canada-None-Washington Ave] SKIPPED 
test.py::test[Canada-None-Peachtree] SKIPPED 
test.py::test[Canada-Chicago-None] PASSED 
test.py::test[Canada-Chicago-Washington Ave] PASSED 
test.py::test[Canada-Chicago-Peachtree] PASSED 
test.py::test[Canada-Atlanta-None] PASSED 
test.py::test[Canada-Atlanta-Washington Ave] PASSED 
test.py::test[Canada-Atlanta-Peachtree] PASSED 
test.py::test[Canada-Mexico City-None] PASSED 
test.py::test[Canada-Mexico City-Washington Ave] PASSED 
test.py::test[Canada-Mexico City-Peachtree] PASSED 
test.py::test[Mexico-None-None] PASSED 
test.py::test[Mexico-None-Washington Ave] SKIPPED 
test.py::test[Mexico-None-Peachtree] SKIPPED 
test.py::test[Mexico-Chicago-None] PASSED 
test.py::test[Mexico-Chicago-Washington Ave] PASSED 
test.py::test[Mexico-Chicago-Peachtree] PASSED 
test.py::test[Mexico-Atlanta-None] PASSED 
test.py::test[Mexico-Atlanta-Washington Ave] PASSED 
test.py::test[Mexico-Atlanta-Peachtree] PASSED 
test.py::test[Mexico-Mexico City-None] PASSED 
test.py::test[Mexico-Mexico City-Washington Ave] PASSED 
test.py::test[Mexico-Mexico City-Peachtree] PASSED 
 
====================== 30 passed, 6 skipped in 0.04 seconds ======================