Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyTest parameterize only certain permutations?

Tags:

python

pytest

I have three fields that I am parameterizing:

@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

Is there a way that I can build the permutations from the top down and leave the lower levels empty for some tests? I want to get parameters like this:

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...

If it helps, think of this as a 3 box dependent drop down. Boxes 2 and 3 can't have a value until the preceding box has a value.

If I put a 'None' in the city or street declarations, then a None can appear when it shouldn't and generate an invalid case like this:

Country     City        Street
US          None        Peachtree

Can I use parameterize to get what I want?

like image 869
PyTest Question Avatar asked Sep 12 '25 10:09

PyTest Question


1 Answers

The easiest way I found to solve this problem is to use 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

Test results:

$ 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 ======================
like image 109
kchomski Avatar answered Sep 13 '25 23:09

kchomski