pytest

Parametrize tests

Single argument

@pytest.mark.parametrize("color", ["red", "blue", "green"], id=str)

Multiple arguments

@pytest.mark.parametrize("height,width", [(20, 30), (40, 10)], id=str)

Custom human readable ids

@pytest.mark.parametrize(
    "height,width",
    [
        (10, 5),
        (400, 200)
    ],
    ids=[
        'small size',
        'big size'
    ]
)

Parametrizing fixtures

Indirectly on test cases

@pytest.mark.parametrize("fixture_name", ["value1", "value2"], indirect=True)

Directly in fixtures via request.param

@pytest.fixture(params=["smtp.gmail.com", "mail.python.org"])
def some_fixture_value(request):
    # use request.param

Use fixtures

Without having them as argument

@pytest.mark.usefixtures("cleandir")


Children
  1. How To
  2. Troubleshooting