|
| 1 | +import json |
| 2 | +import os |
| 3 | +import pytest |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | + |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +def test_add_tag_to_execution_data(tmp_path, fake_env): |
| 10 | + """Verify that tags added via the execution_tag marker are correctly captured in the test data.""" |
| 11 | + test_file = Path(__file__).parent / "data" / "test_sample_execution_tag.py" |
| 12 | + json_output_file = tmp_path / "test_results.json" |
| 13 | + |
| 14 | + # Run pytest with our plugin on the test file |
| 15 | + cmd = [ |
| 16 | + sys.executable, "-m", "pytest", |
| 17 | + str(test_file), |
| 18 | + f"--json={json_output_file}", |
| 19 | + ] |
| 20 | + |
| 21 | + # Run pytest in a subprocess |
| 22 | + result = subprocess.run(cmd, capture_output=True, text=True) |
| 23 | + if result.returncode != 0: |
| 24 | + print(result.stdout) |
| 25 | + print(result.stderr) |
| 26 | + pytest.fail("pytest run failed, see output above") |
| 27 | + |
| 28 | + assert json_output_file.exists(), "JSON output file was not created" |
| 29 | + |
| 30 | + with open(json_output_file, 'r') as f: |
| 31 | + test_results = json.load(f) |
| 32 | + |
| 33 | + tests_by_name = {test["name"]: test for test in test_results} |
| 34 | + |
| 35 | + multi_tag_test = tests_by_name["test_with_multiple_tags"] |
| 36 | + assert "tags" in multi_tag_test |
| 37 | + assert multi_tag_test["tags"] == { |
| 38 | + "language.version": "3.12", |
| 39 | + "team": "backend" |
| 40 | + } |
| 41 | + |
| 42 | + single_tag_test = tests_by_name["test_with_single_tag"] |
| 43 | + assert "tags" in single_tag_test |
| 44 | + assert single_tag_test["tags"] == {"team": "frontend"} |
| 45 | + |
| 46 | + no_tag_test = tests_by_name["test_without_tags"] |
| 47 | + assert "tags" not in no_tag_test or no_tag_test.get("tags") == {} |
| 48 | + |
| 49 | +class TestTagFiltering: |
| 50 | + import subprocess |
| 51 | + import sys |
| 52 | + |
| 53 | + def test_filter_by_single_tag(self,tmp_path, fake_env): |
| 54 | + test_file = Path(__file__).parent / "data" / "test_sample_execution_tag_filter.py" |
| 55 | + |
| 56 | + cmd = [ |
| 57 | + sys.executable, "-m", "pytest", "--co", "-q", "--tag-filters", "color:red", |
| 58 | + str(test_file), |
| 59 | + ] |
| 60 | + |
| 61 | + # Run pytest in a subprocess |
| 62 | + result = subprocess.run(cmd, cwd=str(tmp_path), capture_output=True, text=True) |
| 63 | + |
| 64 | + assert "2/5 tests collected" in result.stdout, "collect count mismatch" |
| 65 | + |
| 66 | + # Parse the output to find collected tests |
| 67 | + lines = result.stdout.strip().splitlines() |
| 68 | + collected_tests = [] |
| 69 | + for line in lines: |
| 70 | + if "::" in line: |
| 71 | + collected_tests.append(line) |
| 72 | + |
| 73 | + assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_apple" in collected_tests |
| 74 | + assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_strawberry" in collected_tests |
| 75 | + assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_orange" not in collected_tests |
| 76 | + assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_banana" not in collected_tests |
| 77 | + assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_grape" not in collected_tests |
| 78 | + |
| 79 | + def test_wrong_filter_format(self,tmp_path, fake_env): |
| 80 | + test_file = Path(__file__).parent / "data" / "test_sample_execution_tag_filter.py" |
| 81 | + |
| 82 | + cmd = [ |
| 83 | + sys.executable, "-m", "pytest", "--co", "-q", "--tag-filters", "foobar", |
| 84 | + str(test_file), |
| 85 | + ] |
| 86 | + |
| 87 | + # Run pytest in a subprocess |
| 88 | + result = subprocess.run(cmd, cwd=str(tmp_path), capture_output=True, text=True) |
| 89 | + |
| 90 | + assert "no tests collected" in result.stdout |
| 91 | + |
| 92 | + def test_no_filter(self,tmp_path, fake_env): |
| 93 | + test_file = Path(__file__).parent / "data" / "test_sample_execution_tag_filter.py" |
| 94 | + |
| 95 | + cmd = [ |
| 96 | + sys.executable, "-m", "pytest", "--co", "-q", |
| 97 | + str(test_file), |
| 98 | + ] |
| 99 | + |
| 100 | + # Run pytest in a subprocess |
| 101 | + result = subprocess.run(cmd, cwd=str(tmp_path), capture_output=True, text=True) |
| 102 | + |
| 103 | + # Parse the output to find collected tests |
| 104 | + lines = result.stdout.strip().splitlines() |
| 105 | + collected_tests = [] |
| 106 | + for line in lines: |
| 107 | + if "::" in line: |
| 108 | + collected_tests.append(line) |
| 109 | + |
| 110 | + assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_apple" in collected_tests |
| 111 | + assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_strawberry" in collected_tests |
| 112 | + assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_orange" in collected_tests |
| 113 | + assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_banana" in collected_tests |
| 114 | + assert "tests/buildkite_test_collector/data/test_sample_execution_tag_filter.py::test_grape" in collected_tests |
0 commit comments