[pytest] New rule idea- Prefer multiple pytest.param(..., id=...)
over pytest.mark.parametrize(..., ids=...)
#14994
Labels
pytest.param(..., id=...)
over pytest.mark.parametrize(..., ids=...)
#14994
Hi !
This is a new rule suggestion for the pytest-style plugin.
It checks for
pytest.mark.parametrize
calls using theids
keyword argument and replace each set of test values with apytest.params(.., id=...)
parameter set, effectively binding the id with the set of arguments.When using the
ids=[…]
syntax, each id is bound to a set of parameters by index.This is quite error prone and makes test hard to modify (adding a test case in the middle means adding an id at the corresponding index in the ids list). Moreover, if one test case fails, it's not directly obvious which set of parameters is associated with the failing id. This issue gets worse the more test case you have in your parametrize call.
Pytest seems to agree this is not a good pattern:
An autofix would look like this:
Don't use:
Use instead:
I've already implemented such fixer in python (using pyupgrade/django-upgrade building blocs) and would be very interested in porting this to ruff.
If you have in mind another MR I could take inspiration from (especially the auto-fix part) it would be awesome!
Also I'm not sure which code to use, PT028 could overlap with a potential new pytest-style rule
The text was updated successfully, but these errors were encountered: