From f7886624ff34f655ef9a3a783063df8adaf4bb45 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 9 Nov 2019 01:44:02 +0100 Subject: [PATCH 1/5] Add --only-using-fixture option to select tests by used fixture --- src/_pytest/fixtures.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index fc55ef2cf7e..69c269682d3 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -28,6 +28,7 @@ from _pytest.compat import is_generator from _pytest.compat import NOTSET from _pytest.compat import safe_getattr +from _pytest.config import hookimpl from _pytest.deprecated import FIXTURE_POSITIONAL_ARGUMENTS from _pytest.outcomes import fail from _pytest.outcomes import TEST_OUTCOME @@ -1195,6 +1196,11 @@ def pytest_addoption(parser): default=[], help="list of default fixtures to be used with this project", ) + parser.addoption( + "--only-using-fixture", + action="store", + help=("Run tests using a specific fixture only."), + ) class FixtureManager: @@ -1388,7 +1394,24 @@ def pytest_generate_tests(self, metafunc): else: continue # will raise FixtureLookupError at setup time - def pytest_collection_modifyitems(self, items): + @hookimpl(hookwrapper=True) + def pytest_collection_modifyitems(self, config, items): + + yield + + only_using_fixture = config.getoption("--only-using-fixture") + if only_using_fixture: + new_items = [] + for item in items: + try: + fixturenames = item.fixturenames + except AttributeError: + # DoctestItem + continue + if only_using_fixture in fixturenames: + new_items.append(item) + items[:] = new_items + # separate parametrized setups items[:] = reorder_items(items) From b51111cbf60fc4b1a22c77000782595ca7e51f2a Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 9 Nov 2019 02:01:52 +0100 Subject: [PATCH 2/5] --mark-fixture --- src/_pytest/fixtures.py | 25 +------------------------ src/_pytest/mark/__init__.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/_pytest/fixtures.py b/src/_pytest/fixtures.py index 69c269682d3..fc55ef2cf7e 100644 --- a/src/_pytest/fixtures.py +++ b/src/_pytest/fixtures.py @@ -28,7 +28,6 @@ from _pytest.compat import is_generator from _pytest.compat import NOTSET from _pytest.compat import safe_getattr -from _pytest.config import hookimpl from _pytest.deprecated import FIXTURE_POSITIONAL_ARGUMENTS from _pytest.outcomes import fail from _pytest.outcomes import TEST_OUTCOME @@ -1196,11 +1195,6 @@ def pytest_addoption(parser): default=[], help="list of default fixtures to be used with this project", ) - parser.addoption( - "--only-using-fixture", - action="store", - help=("Run tests using a specific fixture only."), - ) class FixtureManager: @@ -1394,24 +1388,7 @@ def pytest_generate_tests(self, metafunc): else: continue # will raise FixtureLookupError at setup time - @hookimpl(hookwrapper=True) - def pytest_collection_modifyitems(self, config, items): - - yield - - only_using_fixture = config.getoption("--only-using-fixture") - if only_using_fixture: - new_items = [] - for item in items: - try: - fixturenames = item.fixturenames - except AttributeError: - # DoctestItem - continue - if only_using_fixture in fixturenames: - new_items.append(item) - items[:] = new_items - + def pytest_collection_modifyitems(self, items): # separate parametrized setups items[:] = reorder_items(items) diff --git a/src/_pytest/mark/__init__.py b/src/_pytest/mark/__init__.py index e21e234e774..d96f782971f 100644 --- a/src/_pytest/mark/__init__.py +++ b/src/_pytest/mark/__init__.py @@ -65,6 +65,14 @@ def pytest_addoption(parser): "example: -m 'mark1 and not mark2'.", ) + group.addoption( + "--mark-fixture", + action="store", + help=( + "Add mark to tests using given fixture. Format: fixture[:mark] ('mark' defaults to the fixture name)." + ), + ) + group.addoption( "--markers", action="store_true", @@ -139,6 +147,22 @@ def deselect_by_mark(items, config): def pytest_collection_modifyitems(items, config): + mark_fixture = config.getoption("--mark-fixture") + if mark_fixture: + fixture, _, mark = mark_fixture.partition(":") + if not mark: + mark = fixture + if mark not in MARK_GEN._markers: + MARK_GEN._markers.add(mark) + for item in items: + try: + fixturenames = item.fixturenames + except AttributeError: + # DoctestItem + continue + if fixture in fixturenames: + item.add_marker(mark) + deselect_by_keyword(items, config) deselect_by_mark(items, config) From 1d3cb7a46cda0b6591fa196210bc714f0bfbedb7 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 9 Nov 2019 02:19:23 +0100 Subject: [PATCH 3/5] test --- testing/test_mark.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/testing/test_mark.py b/testing/test_mark.py index 2c12c0451c0..7ec117b9693 100644 --- a/testing/test_mark.py +++ b/testing/test_mark.py @@ -940,6 +940,31 @@ class TestBarClass(BaseTests): # assert skipped_k == failed_k == 0 +def test_mark_fixture(testdir): + testdir.makepyfile( + """ + import pytest + + @pytest.fixture + def fix(): + pass + + def test_1(fix): + pass + + @pytest.mark.FOO + def test_2(): + pass + """ + ) + reprec = testdir.inline_run("--mark-fixture", "fix:mymark", "-m", "mymark") + assert reprec.countoutcomes() == [1, 0, 0] + reprec = testdir.inline_run("--mark-fixture", "fix:FOO", "-m", "FOO") + assert reprec.countoutcomes() == [2, 0, 0] + reprec = testdir.inline_run("--mark-fixture", "fix", "-m", "fix") + assert reprec.countoutcomes() == [1, 0, 0] + + def test_addmarker_order(): node = Node("Test", config=mock.Mock(), session=mock.Mock(), nodeid="Test") node.add_marker("foo") From b1b41c86999421e542d2d92408c0f9c4930d8389 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 9 Nov 2019 02:43:26 +0100 Subject: [PATCH 4/5] fixup! test --- testing/test_mark.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/testing/test_mark.py b/testing/test_mark.py index 7ec117b9693..97b0a829da6 100644 --- a/testing/test_mark.py +++ b/testing/test_mark.py @@ -941,6 +941,13 @@ class TestBarClass(BaseTests): def test_mark_fixture(testdir): + testdir.makeini( + """ + [pytest] + markers = + regged: registered + """ + ) testdir.makepyfile( """ import pytest @@ -955,6 +962,10 @@ def test_1(fix): @pytest.mark.FOO def test_2(): pass + + @pytest.mark.registered + def test_3(): + pass """ ) reprec = testdir.inline_run("--mark-fixture", "fix:mymark", "-m", "mymark") @@ -963,6 +974,10 @@ def test_2(): assert reprec.countoutcomes() == [2, 0, 0] reprec = testdir.inline_run("--mark-fixture", "fix", "-m", "fix") assert reprec.countoutcomes() == [1, 0, 0] + reprec = testdir.inline_run( + "--mark-fixture", "fix:registered", "-m", "registered or FOO" + ) + assert reprec.countoutcomes() == [3, 0, 0] def test_addmarker_order(): From 3e64394413ee72dbb11727e4133b1d74e69f0700 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 9 Nov 2019 20:42:15 +0100 Subject: [PATCH 5/5] fixup! fixup! test --- testing/test_mark.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/testing/test_mark.py b/testing/test_mark.py index 97b0a829da6..1d6555bb194 100644 --- a/testing/test_mark.py +++ b/testing/test_mark.py @@ -963,7 +963,7 @@ def test_1(fix): def test_2(): pass - @pytest.mark.registered + @pytest.mark.regged def test_3(): pass """ @@ -974,9 +974,7 @@ def test_3(): assert reprec.countoutcomes() == [2, 0, 0] reprec = testdir.inline_run("--mark-fixture", "fix", "-m", "fix") assert reprec.countoutcomes() == [1, 0, 0] - reprec = testdir.inline_run( - "--mark-fixture", "fix:registered", "-m", "registered or FOO" - ) + reprec = testdir.inline_run("--mark-fixture", "fix:regged", "-m", "regged or FOO") assert reprec.countoutcomes() == [3, 0, 0]