Replies: 1 comment
-
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Proposal: dict form for
parametrizeas shorthand for stacked decoratorsSummary
Allow
pytest.mark.parametrizeto accept adict[str, Sequence], where each key is a parameter name and each value is the list of values for that parameter. The result is the cartesian product of all value lists — identical to stacking multiple@pytest.mark.parametrizedecorators.Purely syntactic sugar. No new behaviour.
Motivation
Codebases that use declarative fixture overrides — most notably through pytest-factoryboy — routinely parametrise many attributes on the same test. A complex scenario can easily accumulate 10+ stacked decorators:
Where does
authorend andsecond_authorbegin? You have to read every line to find out.The existing multi-argument form groups parameters into one decorator, but separates names from values:
The names and values are in two separate lists — you have to count positions and mentally match the 5th value to the 5th name. This is error-prone, and it only expresses zipped combinations — not a cartesian product.
Neither form scales well when a test parametrises multiple fixtures.
The dict form solves both problems
Semantics
The dict is expanded into multiple stacked
parametrizecalls internally — one per key. Ordering follows dict insertion order (Python 3.7+): first key varies slowest, last key varies fastest. Stacking two dict-form decorators works exactly like stacking two regular ones.Relationship to prior proposals
params(x=[1,2], y=[3,4])). Closest in spirit, but introduced a new decorator. Was rejected and became the pytest-parametrized plugin. The dict form proposed here works within the existingpytest.mark.parametrizeAPI — no new decorator, no new concept to learn.Open questions
These can be deferred if the core idea is of interest:
indirect— could accept a list of keys to pass through as fixtures.ids— auto-generated IDs would combine values across keys, same as stacked decorators do today.pytest.param()— values in the lists could bepytest.param(...)for attaching marks or custom IDs.scope— could be passed as a keyword argument.Implementation sketch
A dict is expanded into multiple
parametrizecalls — one per key. This can be done in 6 lines at the top ofMetafunc.parametrize(), before any existing logic runs:No changes to test collection, reporting, or node IDs — the expansion happens before any of that machinery runs.
Beta Was this translation helpful? Give feedback.
All reactions