pytest-dev / pytest-dev/pytest

Override class-level fixture parametrization with method-level parametrization

Open
#9,175 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What's the problem this feature will solve?

Please see the below Python 3.8 code snippet. It involves:

  • Fixture being indirectly parametrized
    • Class parametrization: idea is this parameterization ("b") applies to all tests
    • Method parameterization: idea is this parameterization ("a") applies to just a particular method
from typing import Any

import pytest

@pytest.fixture
def some_fixture(request) -> Any:
    assert "a" in request.param  # Only passes for test_something
    assert "b" in request.param  # Always passes

# I apply to all test methods
@pytest.mark.parametrize("some_fixture", [{"b": "b"}], indirect=True)
class TestMultiParameterization:

    # I apply to just this one test method
    @pytest.mark.parametrize("some_fixture", [{"a": "a"}], indirect=True)
    def test_something(self, some_fixture: Any) -> None:
        pass

Running the test_something method via pytest==6.2.5, it will spit out the following error:

E   ValueError: duplicate 'some_fixture'

Clearly, pytest rejects the duplicated parameterization.

Describe the solution you'd like

I would like if one could somehow "stitch together" parameterization from class-level parameterizations and method-level parameterizations.

The main benefit would be increased flexibility in parameterization of test fixtures. I am not sure how this could be accomplished in practice, though I do think it'd be useful functionality.

Alternative Solutions

I have not tried any alternate solutions.

Additional context

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

Start by reproducing the class- and method-level fixture parametrization example with pytest 6.2.5 and confirm the duplicate 'some_fixture' error. Investigate pytest's parametrization handling, then define behavior where method-level parametrization overrides or combines with the class-level value while preserving the stated fixture assertions.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.