forked from pytest-dev/pytest
-
Notifications
You must be signed in to change notification settings - Fork 0
fix: swev-id: pytest-dev__pytest-10356 aggregate class-level MRO marks #11
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
Open
casey-brooks
wants to merge
2
commits into
pytest-dev__pytest-10356
Choose a base branch
from
fix/mro-class-marks-10356
base: pytest-dev__pytest-10356
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Class-level marks now merge across multiple inheritance following MRO; identical duplicates are removed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| import pytest | ||
|
|
||
|
|
||
| def _register_marks(pytester: pytest.Pytester) -> None: | ||
| pytester.makeini( | ||
| """ | ||
| [pytest] | ||
| markers = | ||
| alpha(value): mark used for MRO ordering checks | ||
| beta(value): class-level mark defined on subclasses | ||
| gamma(value): function-level mark used for ordering assertions | ||
| shared(value): mark used to exercise deduplication | ||
| method(value): additional function-level mark | ||
| diamond(value): mark applied in diamond inheritance scenarios | ||
| """ | ||
| ) | ||
|
|
||
|
|
||
| def test_multiple_inheritance_merges_marks(pytester: pytest.Pytester) -> None: | ||
| _register_marks(pytester) | ||
| pytester.makepyfile( | ||
| """ | ||
| import pytest | ||
|
|
||
| class BaseA: | ||
| pytestmark = pytest.mark.alpha("base-a") | ||
|
|
||
| class BaseB: | ||
| pytestmark = [pytest.mark.alpha("base-b")] | ||
|
|
||
| @pytest.mark.beta("derived") | ||
| class TestDerived(BaseA, BaseB): | ||
| @pytest.mark.gamma("method") | ||
| def test_method(self, request): | ||
| seen = [(mark.name, mark.args) for mark in request.node.iter_markers()] | ||
| assert seen == [ | ||
| ("gamma", ("method",)), | ||
| ("beta", ("derived",)), | ||
| ("alpha", ("base-a",)), | ||
| ("alpha", ("base-b",)), | ||
| ] | ||
| """ | ||
| ) | ||
|
|
||
| result = pytester.runpytest() | ||
| result.assert_outcomes(passed=1) | ||
|
|
||
|
|
||
| def test_function_marks_precede_class_marks(pytester: pytest.Pytester) -> None: | ||
| _register_marks(pytester) | ||
| pytester.makepyfile( | ||
| """ | ||
| import pytest | ||
|
|
||
| @pytest.mark.alpha("base") | ||
| class Base: | ||
| pass | ||
|
|
||
| class TestDerived(Base): | ||
| pytestmark = [pytest.mark.beta("derived-list")] | ||
|
|
||
| @pytest.mark.method("first") | ||
| @pytest.mark.gamma("second") | ||
| def test_method(self, request): | ||
| names = [mark.name for mark in request.node.iter_markers()] | ||
| assert names[:2] == ["gamma", "method"] | ||
| assert names[2:] == ["beta", "alpha"] | ||
| """ | ||
| ) | ||
|
|
||
| result = pytester.runpytest() | ||
| result.assert_outcomes(passed=1) | ||
|
|
||
|
|
||
| def test_diamond_inheritance_deduplicates_marks(pytester: pytest.Pytester) -> None: | ||
| _register_marks(pytester) | ||
| pytester.makepyfile( | ||
| """ | ||
| import pytest | ||
|
|
||
| @pytest.mark.shared("root") | ||
| class Root: | ||
| pass | ||
|
|
||
| @pytest.mark.shared("branch") | ||
| @pytest.mark.alpha("left") | ||
| class Left(Root): | ||
| pass | ||
|
|
||
| @pytest.mark.shared("branch") | ||
| @pytest.mark.alpha("right") | ||
| class Right(Root): | ||
| pass | ||
|
|
||
| class TestLeaf(Left, Right): | ||
| @pytest.mark.gamma("method") | ||
| def test_leaf(self, request): | ||
| seen = [(mark.name, mark.args) for mark in request.node.iter_markers()] | ||
| assert seen == [ | ||
| ("gamma", ("method",)), | ||
| ("alpha", ("left",)), | ||
| ("shared", ("branch",)), | ||
| ("alpha", ("right",)), | ||
| ("shared", ("root",)), | ||
| ] | ||
| """ | ||
| ) | ||
|
|
||
| result = pytester.runpytest() | ||
| result.assert_outcomes(passed=1) | ||
|
|
||
|
|
||
| def test_identical_marks_across_bases_deduped_once( | ||
| pytester: pytest.Pytester, | ||
| ) -> None: | ||
| _register_marks(pytester) | ||
| pytester.makepyfile( | ||
| """ | ||
| import pytest | ||
|
|
||
| class BaseA: | ||
| pytestmark = [pytest.mark.shared("duplicate")] | ||
|
|
||
| class BaseB: | ||
| pytestmark = pytest.mark.shared("duplicate") | ||
|
|
||
| class TestDerived(BaseA, BaseB): | ||
| def test_marks(self, request): | ||
| seen = [mark.args for mark in request.node.iter_markers(name="shared")] | ||
| assert seen == [("duplicate",)] | ||
| """ | ||
| ) | ||
|
|
||
| result = pytester.runpytest() | ||
| result.assert_outcomes(passed=1) | ||
|
|
||
|
|
||
| def test_parametrize_marks_merge_and_dedupe(pytester: pytest.Pytester) -> None: | ||
| pytester.makepyfile( | ||
| """ | ||
| import pytest | ||
|
|
||
| class ParamBaseA: | ||
| pytestmark = pytest.mark.parametrize("left", ["L1", "L2"]) | ||
|
|
||
| class ParamBaseB: | ||
| pytestmark = [ | ||
| pytest.mark.parametrize("right", ["R1", "R2"]), | ||
| pytest.mark.parametrize("left", ["L1", "L2"]), | ||
| ] | ||
|
|
||
| class TestParam(ParamBaseA, ParamBaseB): | ||
| def test_params(self, left, right): | ||
| pass | ||
| """ | ||
| ) | ||
|
|
||
| result = pytester.runpytest() | ||
| result.assert_outcomes(passed=4) | ||
|
|
||
|
|
||
| def test_parametrize_marks_with_param_objects(pytester: pytest.Pytester) -> None: | ||
| _register_marks(pytester) | ||
| pytester.makepyfile( | ||
| """ | ||
| import pytest | ||
|
|
||
| class ParamBaseA: | ||
| pytestmark = pytest.mark.parametrize( | ||
| "left", | ||
| [ | ||
| pytest.param("L1", marks=pytest.mark.shared("dup")), | ||
| pytest.param("L2", marks=pytest.mark.alpha("left")), | ||
| ], | ||
| ) | ||
|
|
||
| class ParamBaseB: | ||
| pytestmark = [ | ||
| pytest.mark.parametrize( | ||
| "right", | ||
| [ | ||
| pytest.param("R1", marks=pytest.mark.shared("dup")), | ||
| pytest.param("R2", marks=pytest.mark.beta("right")), | ||
| ], | ||
| ) | ||
| ] | ||
|
|
||
| class TestParamCombined(ParamBaseA, ParamBaseB): | ||
| def test_params(self, left, right, request): | ||
| shared_args = {mark.args for mark in request.node.iter_markers(name="shared")} | ||
| if left == "L1" or right == "R1": | ||
| assert shared_args == {("dup",)} | ||
| else: | ||
| assert shared_args == set() | ||
| """ | ||
| ) | ||
|
|
||
| result = pytester.runpytest() | ||
| result.assert_outcomes(passed=4) | ||
|
|
||
|
|
||
| def test_class_pytestmark_forms_supported(pytester: pytest.Pytester) -> None: | ||
| _register_marks(pytester) | ||
| pytester.makepyfile( | ||
| """ | ||
| import pytest | ||
|
|
||
| class Decorated: | ||
| pytestmark = [pytest.mark.alpha("decorated")] | ||
|
|
||
| @pytest.mark.beta("derived") | ||
| class TestDerived(Decorated): | ||
| @pytest.mark.gamma("method") | ||
| def test_forms(self, request): | ||
| seen = [(mark.name, mark.args) for mark in request.node.iter_markers()] | ||
| assert seen == [ | ||
| ("gamma", ("method",)), | ||
| ("beta", ("derived",)), | ||
| ("alpha", ("decorated",)), | ||
| ] | ||
| """ | ||
| ) | ||
|
|
||
| result = pytester.runpytest() | ||
| result.assert_outcomes(passed=1) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.