Skip to content

Better syntax for Parametrize's named test cases #12997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
irevolve opened this issue Nov 26, 2024 · 2 comments
Closed

Better syntax for Parametrize's named test cases #12997

irevolve opened this issue Nov 26, 2024 · 2 comments

Comments

@irevolve
Copy link

irevolve commented Nov 26, 2024

What's the problem this feature will solve?

Currently test-cases created by parametrize are automatically named by their values. If provided they will be named by their corresponding entry in the "ids" list. That is a way to manually override names for testcases which is useful when there are more complex cases which you want to describe to know what went wrong fast upon failure. Currently the code looks like that:

@pytest.mark.parametrize(
  ["input", "expected"],
  [
    (
      "some stuff",
      "my expected result",
    ),
    (
      "some other stuff",
      "my other expected result",
    ),
  ],
  ids=[
    "Testcase 1",
    "Testcase 2",
  ],
)
def test_stuff(input, expected):
  assert stuff(input) == expected

For small tests that is pretty good as everyone can count to two in this case.
However i often find myself in a situation where tests not only have 2 test cases but rather like 10 - 30 as there are probably some edge-cases which need to be considered. When having so much cases its hard to find the inputs with which a case failed due to you just get to see the case name, which you need to manually correlate with the argvalues by index, which is tedious.

Describe the solution you'd like

In my opinion that current implementation is working fine, however its somewhat inconvenient.
Therefore a object-based approach either for the argvalues or the whole decorator would be suitable to get rid of that issue:

@pytest.mark.parametrize(
    {
        "Testcase1": {  # <- The name of the testcase
            "input": "some stuff",  # <- The value for the parameter "input"
            "expected": "my expected result",  # <- The value for the parameter "expected"
        },
        "Testcase2": {
            "input": "some other stuff",
            "expected": "my other expected result",
        },
    }
)
def test_stuff(input, expected):
  assert stuff(input) == expected

alternatively if this would be too much of a change it could also be done like that:

@pytest.mark.parametrize(
    ["input", "expected"],
    {
        "Testcase1": (  # <- The name of the testcase
            "some stuff",  # <- The value for the parameter "input"
            "my expected result",  # <- The value for the parameter "expected"
        ),
        "Testcase2": (
            "some other stuff",
            "my other expected result",
        ),
    },
)
def test_stuff(input, expected):
  assert stuff(input) == expected

to not introduce a breaking change that object could be handed over by a new parameter and making the current ones optional if the new one is set.

@irevolve irevolve changed the title Better Syntax for Parametrize named test cases Better syntax for Parametrize's named test cases Nov 26, 2024
@RonnyPfannschmidt
Copy link
Member

Please take a look at pytest.param

It's id kwarg solves the naming

@The-Compiler
Copy link
Member

Duplicate of #7977

You can already use pytest.param(..., id="...") instead, see Parametrizing tests - pytest documentation.

@The-Compiler The-Compiler closed this as not planned Won't fix, can't repro, duplicate, stale Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants