Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 38 ยท Parametrization and Marks

IDs, Indirect Parametrization and Parametrized Fixtures

Give parametrized tests readable ids, use pytest.param to attach marks and ids to single cases, parametrize fixtures with params, and route values through fixtures with indirect.

The basics of parametrization get you far, but real test tables raise questions. How do I name a case so the failure report is readable? How do I mark just one row as expected to fail? How do I run a whole fixture for several values, for example the same tests against two database backends? This lesson answers all of them.

Test ids

Every generated test has an id shown in brackets. pytest builds it from the values: numbers and strings are used directly, and other objects become argname0, argname1, and so on. Long or opaque ids make failures hard to read, so you can supply your own with the ids argument:

Python ids as a list

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

ids can also be a function that receives each value and returns a string (or None to fall back to the default):

Python ids as a function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Readable ids matter because you select a single case with them: pytest "test_ids.py::test_cart_total[empty cart]". Ids may contain spaces; quote them in the shell.

pytest.param: options for a single row

Wrap a row in pytest.param(...) to give it its own id and marks. This is how you mark one case as slow, skipped or expected to fail without touching the others:

Python pytest.param with id and marks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output shows XFAIL for the euro case (it fails as predicted) and SKIPPED for the last one. Everything else runs normally. The idea of xfail and skip is the topic of the next lesson.

Parametrizing a fixture with params

Instead of parametrizing a test, you can parametrize a fixture. Every test that uses it then runs once per parameter, which is perfect for "run all these tests against each backend":

Python Fixture params

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Three test functions times two implementations make six tests. The implementation is chosen in one place, the fixture, so tests stay clean. When two fixtures have params, the tests that use both run for every combination.

Parametrized fixtures cooperate with scope: a scope="module" parametrized fixture is built once per parameter, and pytest orders tests to reuse each instance as much as possible.

Indirect parametrization

Sometimes a test needs the value from the table to pass through a fixture first. For example the table lists user names, and a fixture turns a name into a logged-in session. Set indirect=True (or a list of argument names): pytest then sends each value to the fixture of the same name as request.param.

Python indirect=True

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Only current_user is indirect: it is built by the fixture from the string "ada" or "bob". The other argument, can_delete, passes straight to the test. The big win is that fixture setup and teardown can depend on the value (start a different server, open a different file).

flowchart LR
    T["table row<br/>('ada', True)"] -->|"direct"| X["can_delete=True"]
    T -->|"indirect"| F["fixture current_user<br/>request.param = 'ada'"]
    F --> Y["current_user = {...}"]
    X --> R["test function"]
    Y --> R

Direct values go straight to the test; indirect ones travel through the fixture of the same name.

Parametrizing with generated data

Because the argument list is ordinary Python, you can build it with code, from a function, a dictionary or a file:

Python Generated parameters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Keeping the table as a dict whose keys become ids is a tidy pattern: the case name lives beside its data.

Parametrize arguments are evaluated at collection time, before any test runs. Do not put expensive or random work in them; a table that changes between runs makes failures impossible to reproduce.

pytest_generate_tests: parametrize by hook

For parametrization that follows a convention, use the pytest_generate_tests(metafunc) hook in a test file or conftest. It is called for each test function and can add parameters based on the test's argument names:

Python
def pytest_generate_tests(metafunc):
    if "browser" in metafunc.fixturenames:
        metafunc.parametrize("browser", ["firefox", "chrome"])

Every test that takes a browser argument now runs twice. This is how plugins add dimensions such as backends or environments.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does pytest.param(1, 2, id="edge", marks=pytest.mark.xfail) let you do?

    pytest.param wraps a single row so you can set its id and marks individually.
  2. 2. How do you run all tests that use a fixture against several implementations?

    A parametrized fixture makes every test that requests it run once per parameter.
  3. 3. What does indirect=True do in parametrize?

    With indirect, the value goes through the fixture first, which can turn it into a richer object and run setup or teardown.
  4. 4. Which ids argument creates readable names from a custom function?

    ids accepts a list of strings or a callable; the callable is applied to each parameter value.

Summary

  • Use ids=[...] or a function to give parametrized cases readable names; select one with file::test[id].
  • pytest.param(values, id=..., marks=...) customizes a single row.
  • @pytest.fixture(params=[...]) runs every test using the fixture once per parameter, with the value in request.param.
  • indirect=True routes table values through a fixture.
  • Generate tables from Python data, but keep them deterministic; pytest_generate_tests adds parameters through a hook.

Next: Skip, skipif and xfail