PyTest 仅参数化某些排列?

PyT*_*ion 4 python pytest

我正在参数化三个字段:

@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
Run Code Online (Sandbox Code Playgroud)

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

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...
Run Code Online (Sandbox Code Playgroud)

如果有帮助,请将其视为 3 个框相关的下拉菜单。在前一个框具有值之前,框 2 和 3 不能具有值。

city如果我在or声明中放置“None” street,则 aNone可能会在不应该出现的情况下出现,并生成如下所示的无效情况:

Country     City        Street
US          None        Peachtree
Run Code Online (Sandbox Code Playgroud)

我可以用来parameterize得到我想要的东西吗?

kch*_*ski 7

我发现解决这个问题的最简单方法是使用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
Run Code Online (Sandbox Code Playgroud)

检测结果:

$ 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 ======================
Run Code Online (Sandbox Code Playgroud)