pytest-dev / pytest-dev/pytest

Test Parametrization Id Function Receives Arg Tuple

Open
#8,283 7 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: parametrize type: proposal
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

I've seen requests similar to this, but I don't believe the exact same. Forgive me if this is a duplicate.

Proposal

For multi-argument parametrized test functions, pass the arguments as the original tuple to ids when ids` is a callable.

@pytest.mark.parametrize(
    "input_val, expected_output_val",
    [
        (0, 1),
        ("hello", "world")
    ],
    ids=lambda vals: f"Input val: {vals[0]}, expected output val: {vals[1]}"
)
def test_some_function(input_val, expected_ouput_val):
   ...

would output:

test_something.py::test_some_function[Input val: 0, expected output val: 1]
test_something.py::test_some_function[Input val: hello, expected output val: world]

Current Workarounds

If each arg in a given arg set is a different type, the above output could be achieved by using conditional statements based on arg type. e.g. ids=lambda val: f"Input val: {val} if type(val) == str else f"expected output val: {val}".

However, this is only sometimes the case. Exceptions could be found if a function accepts multiple types, or the likelihood of unique types for each parameter decreases with increasing number of parameters for a given function.

The other workaround I have is to declare the argument sets as a list, pass the list to argvals, and then pass a list comprehension to ids:

some_function_parametrize_args = [
        (0, 1),
        ("hello", "world")
]

@pytest.mark.parametrize(
    "input_val, expected_output_val",
    some_function_parametrize_args,
    ids=[f"Input val: {vals[0]}, expected output val: {vals[1]}" for vals in some_function_parametrize_args]
)
def test_some_function(input_val, expected_ouput_val):
   ...

This workaround works just fine, but I think it makes the code messier by exposing the args passed into argvals as a variable in the module when the intended scope would be just for that test function.


Thank you in advanced for reading this and discussing with me!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue centers on pytest.mark.parametrize and callable ids handling, but names no implementation files or tests. Start by tracing how ids receives values for multi-argument parametrization and reviewing related tests; done means a callable receives each original argument tuple and the documented example and regression coverage pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.