pytest icon indicating copy to clipboard operation
pytest copied to clipboard

Allow parametrize to depend on previous parametrize

Open Anton3 opened this issue 9 months ago • 5 comments

Closes: #13233

Anton3 avatar Feb 20 '25 11:02 Anton3

the exposure of callspec, the hook order change as well as future considerations make this one extra hard to review

I'll need to schedule something for that

RonnyPfannschmidt avatar Feb 20 '25 19:02 RonnyPfannschmidt

An alternative to exposing CallSpec is, as I said, to add a new Node subtype with an additional getparam method. If it was a Node, the hook from docs

    def pytest_generate_tests(metafunc: pytest.Metafunc):
        if "bar" in metafunc.fixturenames:
            # parametrize "bar" arg based on "bar_params" mark
            base_bar_marks = list(metafunc.definition.iter_markers("bar_params"))

            def gen_params(callspec: pytest.CallSpec):
                # collect all marks
                bar_marks = base_bar_marks + [
                    mark for mark in callspec.marks if mark.name == "bar_params"
                ]
                # collect all args from all marks
                return [arg for mark in bar_marks for arg in mark.args]

            metafunc.parametrize("bar", gen_params)

would be rewritten as just:

    def pytest_generate_tests(metafunc: pytest.Metafunc):
        if "bar" in metafunc.fixturenames:
            def gen_params(proto_item: pytest.ProtoItem):
                bar_marks = proto_item.iter_markers("bar_params")
                return [arg for mark in bar_marks for arg in mark.args]

            metafunc.parametrize("bar", gen_params)

Anton3 avatar Feb 21 '25 08:02 Anton3

I would like to avoid using nodes or node lookalikes

In fact thinking about potentially nesting and/or cascading I'd like to defer inventing new apis

Just ensuring the apis to experiment/ learn from this are experimental

We can't hope to initially account for something like acting in the bases of mark coming in from lazy fixtures

RonnyPfannschmidt avatar Feb 21 '25 11:02 RonnyPfannschmidt

@RonnyPfannschmidt I've figured out the ordering issue I struggled with

I can add parametrize_experimental that has everything of the normal parametrize, plus the new feature. What do you think?

Anton3 avatar Feb 24 '25 18:02 Anton3

im struggling wit the naming still

instead of expanding parametrize, i'd like to see a new name that only offers the new feature and is marked as experimental api

something like parametrize_dependent as name for both the marker on the tests as well as the function name on metafunc

that way the oder issue would also resolve as the default implementation could just invoke the parametrize_dependent after parametrize

im still very torn on the naming issue

i think its a good idea to keep a distinction between parametrize thats just defined and parametrize thats computed based on other parameters

RonnyPfannschmidt avatar Mar 10 '25 09:03 RonnyPfannschmidt