Fail fast when no tests are selected after collection#1
Closed
p-datadog wants to merge 4736 commits into
Closed
Conversation
DataDog#6140) Co-authored-by: Alberto Vara <alberto.vara@datadoghq.com>
Co-authored-by: Charles de Beauchesne <charles.debeauchesne@datadoghq.com>
…Dog#6435) Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: oceane.bordeau <oceane.bordeau@datadoghq.com>
## Summary - Replace `bug (APPSEC-61286)` marker on `Test_Headers_Event_Blocking` with a `v2.7.0-dev` version gate in `manifests/golang.yml` - The underlying issue (missing `Content-Length` span tag on blocked responses) has been fixed in dd-trace-go by explicitly setting the header in the block request handler - Tracer fix: DataDog/dd-trace-go#4496 --- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: flavien.darche <flavien.darche@datadoghq.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: dd-octo-sts[bot] <200755185+dd-octo-sts[bot]@users.noreply.github.com> Co-authored-by: Brian Marks <bm1549@users.noreply.github.com>
Co-authored-by: dd-octo-sts[bot] <200755185+dd-octo-sts[bot]@users.noreply.github.com>
When pytest collects zero tests for a scenario (e.g. wrong scenario name, misconfigured test filters), the run would previously either silently succeed or hang. This is a footgun in CI — a green build that tested nothing. Add an early exit in pytest_collection_finish that calls pytest.exit() with returncode=1 when no items survive collection filtering. The check correctly skips these legitimate zero-test modes: - --collect-only (inspection, not execution) - --declaration-report (metadata collection) - --sleep (intentionally deselects all tests) - --skip-empty-scenario (all tests are xfail/skip) Note: session.testscollected cannot be used here because in pytest 7.1.3 it is assigned on the line *after* the collection_finish hook fires in Session.perform_collect. len(session.items) is the correct check. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do?
When pytest collects zero tests for a scenario, the run currently either silently exits with code 0 (a "green" CI build that tested nothing) or hangs waiting for test setup that never arrives. This makes it easy to miss misconfigured scenarios or broken test selectors — the build looks healthy when it isn't.
This PR adds a check in
pytest_collection_finishthat callspytest.exit()with return code 1 when no test items survive collection filtering, giving a clear error message instead of a silent pass.Why this approach?
session.testscollecteddoesn't work hereOne thing that caught my eye during investigation:
session.testscollectedis always0insidepytest_collection_finish. This isn't a bug in system-tests — it's a pytest 7.1.3 hook ordering issue. InSession.perform_collect, the assignment happens after the hook fires:len(session.items)is the correct check — it reflects the already-filtered list.Graceful exit, not a hard kill
An earlier attempt at this used
os.kill(os.getpid(), 9)(SIGKILL) to force an immediate exit. That works, but it skips all cleanup — container teardown, log flushing, pytest plugins, atexit handlers. If containers were started during session setup, they'd be orphaned.pytest.exit()runs the normal shutdown path.Respecting legitimate zero-test modes
The check is placed after the
--collect-onlyand--declaration-reportguards (which return early and don't need tests), and explicitly skips two modes where zero tests are intentional:--sleepdeselects all items on purpose (it keeps the environment running for manual exploration)--skip-empty-scenariois used when all tests in a scenario are xfail/skip —pytest_sessionfinishalready convertsNO_TESTS_COLLECTEDto exit code 0 for this caseThe change
conftest.py— 10 lines added inpytest_collection_finish, between the existing early-return guards and the sleep mode handler:What it looks like in practice
Before (wrong scenario name):
After:
Modes unaffected
--collect-only--declaration-report--sleep--skip-empty-scenarioTest plan
--collect-only— works as before--skip-empty-scenarioon a scenario with only xfail tests — exits 0--sleep— enters sleep mode as before🤖 Generated with Claude Code