-
Notifications
You must be signed in to change notification settings - Fork 12
Add --tag-filters option to filter tests using execution_tag marker
#83
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
Merged
gchan
merged 4 commits into
main
from
te-5135-add-custom-option-to-filter-tests-by-execution-tag-in-pytest
Jan 26, 2026
Merged
Changes from all commits
Commits
Show all changes
4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,7 +41,7 @@ def pytest_unconfigure(config): | |
|
|
||
| if plugin: | ||
| api = API(os.environ) | ||
| numprocesses = config.getoption("numprocesses") | ||
| numprocesses = config.getoption("numprocesses", None) | ||
| xdist_enabled = ( | ||
| config.pluginmanager.getplugin("xdist") is not None | ||
| and numprocesses is not None | ||
|
|
@@ -89,3 +89,10 @@ def pytest_addoption(parser): | |
| dest="mergejson", | ||
| help='merge json output with existing file, if it exists' | ||
| ) | ||
| group.addoption( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| '--tag-filters', | ||
| default=None, | ||
| action='store', | ||
| dest="tag_filters", | ||
| help='filter tests by execution_tag with `key:value`, e.g. `--tag-filters color:red`' | ||
| ) | ||
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
15 changes: 15 additions & 0 deletions
15
tests/buildkite_test_collector/data/test_sample_execution_tag.py
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,15 @@ | ||
| # This is sample test file used for integration testing of execution_tag marker | ||
| import pytest | ||
|
|
||
|
|
||
| @pytest.mark.execution_tag("language.version", "3.12") | ||
| @pytest.mark.execution_tag("team", "backend") | ||
| def test_with_multiple_tags(): | ||
| assert True | ||
|
|
||
| @pytest.mark.execution_tag("team", "frontend") | ||
| def test_with_single_tag(): | ||
| assert True | ||
|
|
||
| def test_without_tags(): | ||
| assert True |
29 changes: 29 additions & 0 deletions
29
tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py
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,29 @@ | ||
| # This is sample test file used for integration testing of execution_tag marker | ||
| import pytest | ||
|
|
||
|
|
||
| @pytest.mark.execution_tag("color", "red") | ||
| @pytest.mark.execution_tag("size", "medium") | ||
| def test_apple(): | ||
| assert True | ||
|
|
||
| @pytest.mark.execution_tag("color", "orange") | ||
| @pytest.mark.execution_tag("size", "medium") | ||
| def test_orange(): | ||
| assert True | ||
|
|
||
| @pytest.mark.execution_tag("color", "yellow") | ||
| @pytest.mark.execution_tag("size", "large") | ||
| def test_banana(): | ||
| assert True | ||
|
|
||
| @pytest.mark.execution_tag("color", "purple") | ||
| @pytest.mark.execution_tag("size", "small") | ||
| def test_grape(): | ||
| assert True | ||
|
|
||
| @pytest.mark.execution_tag("color", "red") | ||
| @pytest.mark.execution_tag("size", "small") | ||
| def test_strawberry(): | ||
| assert True | ||
|
|
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,114 @@ | ||
| import json | ||
| import os | ||
| import pytest | ||
| import subprocess | ||
| import sys | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| def test_add_tag_to_execution_data(tmp_path, fake_env): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice job backfilling a test |
||
| """Verify that tags added via the execution_tag marker are correctly captured in the test data.""" | ||
| test_file = Path(__file__).parent / "data" / "test_sample_execution_tag.py" | ||
| json_output_file = tmp_path / "test_results.json" | ||
|
|
||
| # Run pytest with our plugin on the test file | ||
| cmd = [ | ||
| sys.executable, "-m", "pytest", | ||
| str(test_file), | ||
| f"--json={json_output_file}", | ||
| ] | ||
|
|
||
| # Run pytest in a subprocess | ||
| result = subprocess.run(cmd, capture_output=True, text=True) | ||
| if result.returncode != 0: | ||
| print(result.stdout) | ||
| print(result.stderr) | ||
| pytest.fail("pytest run failed, see output above") | ||
|
|
||
| assert json_output_file.exists(), "JSON output file was not created" | ||
|
|
||
| with open(json_output_file, 'r') as f: | ||
| test_results = json.load(f) | ||
|
|
||
| tests_by_name = {test["name"]: test for test in test_results} | ||
|
|
||
| multi_tag_test = tests_by_name["test_with_multiple_tags"] | ||
| assert "tags" in multi_tag_test | ||
| assert multi_tag_test["tags"] == { | ||
| "language.version": "3.12", | ||
| "team": "backend" | ||
| } | ||
|
|
||
| single_tag_test = tests_by_name["test_with_single_tag"] | ||
| assert "tags" in single_tag_test | ||
| assert single_tag_test["tags"] == {"team": "frontend"} | ||
|
|
||
| no_tag_test = tests_by_name["test_without_tags"] | ||
| assert "tags" not in no_tag_test or no_tag_test.get("tags") == {} | ||
|
|
||
| class TestTagFiltering: | ||
| import subprocess | ||
| import sys | ||
|
|
||
| def test_filter_by_single_tag(self,tmp_path, fake_env): | ||
| test_file = Path(__file__).parent / "data" / "test_sample_execution_tag_filter.py" | ||
|
|
||
| cmd = [ | ||
| sys.executable, "-m", "pytest", "--co", "-q", "--tag-filters", "color:red", | ||
| str(test_file), | ||
| ] | ||
|
|
||
| # Run pytest in a subprocess | ||
| result = subprocess.run(cmd, cwd=str(tmp_path), capture_output=True, text=True) | ||
|
|
||
| assert "2/5 tests collected" in result.stdout, "collect count mismatch" | ||
|
|
||
| # Parse the output to find collected tests | ||
| lines = result.stdout.strip().splitlines() | ||
| collected_tests = [] | ||
| for line in lines: | ||
| if "::" in line: | ||
| collected_tests.append(line) | ||
|
|
||
| assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_apple" in collected_tests | ||
| assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_strawberry" in collected_tests | ||
| assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_orange" not in collected_tests | ||
| assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_banana" not in collected_tests | ||
| assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_grape" not in collected_tests | ||
|
|
||
| def test_wrong_filter_format(self,tmp_path, fake_env): | ||
| test_file = Path(__file__).parent / "data" / "test_sample_execution_tag_filter.py" | ||
|
|
||
| cmd = [ | ||
| sys.executable, "-m", "pytest", "--co", "-q", "--tag-filters", "foobar", | ||
| str(test_file), | ||
| ] | ||
|
|
||
| # Run pytest in a subprocess | ||
| result = subprocess.run(cmd, cwd=str(tmp_path), capture_output=True, text=True) | ||
|
|
||
| assert "no tests collected" in result.stdout | ||
|
|
||
| def test_no_filter(self,tmp_path, fake_env): | ||
| test_file = Path(__file__).parent / "data" / "test_sample_execution_tag_filter.py" | ||
|
|
||
| cmd = [ | ||
| sys.executable, "-m", "pytest", "--co", "-q", | ||
| str(test_file), | ||
| ] | ||
|
|
||
| # Run pytest in a subprocess | ||
| result = subprocess.run(cmd, cwd=str(tmp_path), capture_output=True, text=True) | ||
|
|
||
| # Parse the output to find collected tests | ||
| lines = result.stdout.strip().splitlines() | ||
| collected_tests = [] | ||
| for line in lines: | ||
| if "::" in line: | ||
| collected_tests.append(line) | ||
|
|
||
| assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_apple" in collected_tests | ||
| assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_strawberry" in collected_tests | ||
| assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_orange" in collected_tests | ||
| assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_banana" in collected_tests | ||
| assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_grape" in collected_tests | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One of the tests I added failed in CI because
numprocessesoption doesn't exists in the subprocess environment. I addedNoneas default to prevent error being raised when evaluation that option.