-
Notifications
You must be signed in to change notification settings - Fork 13
Capture collection errors (e.g. import failures) in JSON report #108
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
niceking
merged 6 commits into
buildkite:main
from
altana-ai:fix/capture-collection-errors
Apr 28, 2026
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1d986e0
Capture collection errors (e.g. import failures) in JSON report
451f489
Fix pylint line-too-long in pytest_collectreport
1e3748a
Add dedup guard for collection errors and tighten test assertions
1e7a6b0
Tag collection errors with test.collection_error=true
4e12fc1
Update src/buildkite_test_collector/pytest_plugin/buildkite_plugin.py
jasonwbarnett 262de3b
Update tests for renamed tag: test.pytest_collection_error
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
13 changes: 13 additions & 0 deletions
13
tests/buildkite_test_collector/data/test_sample_import_error.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,13 @@ | ||
| """Sample test file with an import error. | ||
|
|
||
| This file deliberately imports a non-existent module to trigger a collection | ||
| error in pytest — the same scenario as importing a removed/renamed symbol | ||
| from a real package. | ||
| """ | ||
|
|
||
| from nonexistent_module import does_not_exist | ||
|
|
||
|
|
||
| def test_should_be_reported_as_failed(): | ||
| """This test can never run, but should still appear as a failure in the report.""" | ||
| assert does_not_exist() == 42 |
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
92 changes: 92 additions & 0 deletions
92
tests/buildkite_test_collector/test_integration_import_error.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,92 @@ | ||
| """Integration test: collection errors (import failures) are captured in the JSON report. | ||
|
|
||
| When a test file has an import error, pytest fails during collection. The | ||
| ``pytest_collectreport`` hook on ``BuildkitePlugin`` captures the error and | ||
| adds it to the JSON report as a failed test entry. | ||
|
|
||
| See: https://github.com/buildkite/test-collector-python/issues/106 | ||
| """ | ||
|
|
||
| import json | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| BROKEN_FILE = Path(__file__).parent / "data" / "test_sample_import_error.py" | ||
| PASSING_FILE = Path(__file__).parent / "data" / "test_sample_skip.py" | ||
|
|
||
|
|
||
| class TestImportErrorReporting: | ||
| """Verify that import errors are captured in the JSON report.""" | ||
|
|
||
| def _run_pytest(self, tmp_path, test_files, *extra_args): | ||
| json_output = tmp_path / "results.json" | ||
| cmd = [ | ||
| sys.executable, | ||
| "-m", | ||
| "pytest", | ||
| *[str(f) for f in test_files], | ||
| f"--json={json_output}", | ||
| "-v", | ||
| *extra_args, | ||
| ] | ||
| result = subprocess.run(cmd, capture_output=True, text=True) | ||
| return result, json_output | ||
|
|
||
| def test_pytest_exits_nonzero_on_import_error(self, tmp_path): | ||
| """Pytest itself correctly detects the import error and exits non-zero.""" | ||
| result, _ = self._run_pytest(tmp_path, [BROKEN_FILE]) | ||
|
|
||
| assert result.returncode != 0, ( | ||
| "pytest should exit non-zero when a test file has an import error" | ||
| ) | ||
| assert "ModuleNotFoundError" in result.stdout, ( | ||
| "pytest output should mention the import error" | ||
| ) | ||
|
|
||
| def test_import_error_captured_in_json_report(self, tmp_path): | ||
| """The JSON report captures the collection error as a failed test.""" | ||
| result, json_output = self._run_pytest(tmp_path, [BROKEN_FILE]) | ||
|
|
||
| assert result.returncode != 0 | ||
|
|
||
| assert json_output.exists(), f"JSON not created. stderr:\n{result.stderr}" | ||
| data = json.loads(json_output.read_text()) | ||
|
|
||
| assert len(data) == 1, ( | ||
| f"Expected 1 entry for the collection error, got {len(data)}: " | ||
| f"{[t.get('name') for t in data]}" | ||
| ) | ||
|
|
||
| entry = data[0] | ||
| assert entry["result"] == "failed" | ||
| assert entry.get("failure_reason") is not None, "failure_reason should be present" | ||
| assert "ImportError" in entry["failure_reason"] | ||
| assert entry.get("tags", {}).get("test.collection_error") == "true" | ||
|
|
||
| def test_import_error_reported_alongside_passing_tests(self, tmp_path): | ||
| """With --continue-on-collection-errors, both passing tests and the | ||
| collection error appear in the report.""" | ||
| result, json_output = self._run_pytest( | ||
| tmp_path, | ||
| [BROKEN_FILE, PASSING_FILE], | ||
| "--continue-on-collection-errors", | ||
| ) | ||
|
|
||
| assert result.returncode != 0, "pytest should still exit non-zero" | ||
|
|
||
| data = json.loads(json_output.read_text()) | ||
| names = [t["name"] for t in data] | ||
|
|
||
| # 5 tests from test_sample_skip.py + 1 collection error from test_sample_import_error.py | ||
| assert len(data) == 6, ( | ||
| f"Expected 6 entries (5 passing + 1 collection error), got {len(data)}: {names}" | ||
| ) | ||
|
|
||
| assert "test_passing" in names | ||
|
|
||
| failed = [t for t in data if t["result"] == "failed"] | ||
| assert len(failed) == 1 | ||
| assert failed[0].get("failure_reason") is not None, "failure_reason should be present" | ||
| assert "ImportError" in failed[0]["failure_reason"] |
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.