PyTest仅参数化某些排列?

时间:2017-08-10 03:28:45

标签: 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

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

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之前不能有值,直到上一个方框有值。

如果我放了一个'无'在citystreet声明中,None可以在不应该出现并生成无效案例时显示:

Country     City        Street
US          None        Peachtree

我可以使用parameterize来获得我想要的内容吗?

1 个答案:

答案 0 :(得分:1)

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

<强> test.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 ======================