|
1 | 1 | import json |
2 | 2 | import os |
3 | 3 | import pytest |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | + |
4 | 7 | from pathlib import Path |
5 | 8 |
|
6 | 9 | def test_add_tag_to_execution_data(tmp_path, fake_env): |
7 | 10 | """Verify that tags added via the execution_tag marker are correctly captured in the test data.""" |
8 | | - import subprocess |
9 | | - import sys |
10 | | - |
11 | 11 | test_file = Path(__file__).parent / "data" / "test_sample_execution_tag.py" |
12 | 12 | json_output_file = tmp_path / "test_results.json" |
13 | 13 |
|
@@ -41,3 +41,70 @@ def test_add_tag_to_execution_data(tmp_path, fake_env): |
41 | 41 |
|
42 | 42 | no_tag_test = tests_by_name["test_without_tags"] |
43 | 43 | assert "tags" not in no_tag_test or no_tag_test.get("tags") == {} |
| 44 | + |
| 45 | +class TestTagFiltering: |
| 46 | + import subprocess |
| 47 | + import sys |
| 48 | + |
| 49 | + def test_filter_by_single_tag(self,tmp_path, fake_env): |
| 50 | + test_file = Path(__file__).parent / "data" / "test_sample_execution_tag_filter.py" |
| 51 | + |
| 52 | + cmd = [ |
| 53 | + sys.executable, "-m", "pytest", "--co", "-q", "--tag-filters", "color:red", |
| 54 | + str(test_file), |
| 55 | + ] |
| 56 | + |
| 57 | + # Run pytest in a subprocess |
| 58 | + result = subprocess.run(cmd, cwd=str(tmp_path), capture_output=True, text=True) |
| 59 | + |
| 60 | + assert "2/5 tests collected" in result.stdout, "collect count mismatch" |
| 61 | + |
| 62 | + # Parse the output to find collected tests |
| 63 | + lines = result.stdout.strip().splitlines() |
| 64 | + collected_tests = [] |
| 65 | + for line in lines: |
| 66 | + if "::" in line: |
| 67 | + collected_tests.append(line) |
| 68 | + |
| 69 | + assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_apple" in collected_tests |
| 70 | + assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_strawberry" in collected_tests |
| 71 | + assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_orange" not in collected_tests |
| 72 | + assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_banana" not in collected_tests |
| 73 | + assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_grape" not in collected_tests |
| 74 | + |
| 75 | + def test_wrong_filter_format(self,tmp_path, fake_env): |
| 76 | + test_file = Path(__file__).parent / "data" / "test_sample_execution_tag_filter.py" |
| 77 | + |
| 78 | + cmd = [ |
| 79 | + sys.executable, "-m", "pytest", "--co", "-q", "--tag-filters", "foobar", |
| 80 | + str(test_file), |
| 81 | + ] |
| 82 | + |
| 83 | + # Run pytest in a subprocess |
| 84 | + result = subprocess.run(cmd, cwd=str(tmp_path), capture_output=True, text=True) |
| 85 | + |
| 86 | + assert "no tests collected" in result.stdout |
| 87 | + |
| 88 | + def test_no_filter(self,tmp_path, fake_env): |
| 89 | + test_file = Path(__file__).parent / "data" / "test_sample_execution_tag_filter.py" |
| 90 | + |
| 91 | + cmd = [ |
| 92 | + sys.executable, "-m", "pytest", "--co", "-q", |
| 93 | + str(test_file), |
| 94 | + ] |
| 95 | + |
| 96 | + # Run pytest in a subprocess |
| 97 | + result = subprocess.run(cmd, cwd=str(tmp_path), capture_output=True, text=True) |
| 98 | + |
| 99 | + # Parse the output to find collected tests |
| 100 | + lines = result.stdout.strip().splitlines() |
| 101 | + collected_tests = [] |
| 102 | + for line in lines: |
| 103 | + if "::" in line: |
| 104 | + collected_tests.append(line) |
| 105 | + |
| 106 | + assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_apple" in collected_tests |
| 107 | + assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_strawberry" in collected_tests |
| 108 | + assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_orange" in collected_tests |
| 109 | + assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_banana" in collected_tests |
| 110 | + assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_grape" in collected_tests |
0 commit comments