Prompt
How do I set up a test failure explanation tool for debugging failing tests in our CI pipeline?
Latest observation
A good “test failure explanation” tool in CI usually has one job: take a failing test, gather the relevant context, and turn it into a concise, actionable summary.
Here’s a practical way to set one up.
1) Decide what the tool should explain
Start with the output you want when a test fails:
- failing test name
- error message / stack trace
- assertions that failed
- recent code changes in the branch or PR
- related logs, screenshots, or artifacts
- likely root cause categories
- suggested next debugging steps
A useful explanation tool should answer:
- What failed?
- Why did it probably fail?
- Where should I look next?
2) Capture the right CI data
Your CI pipeline should save enough context for the tool to analyze.
Collect:
- test runner output
- stack traces
- stdout/stderr logs
- JUnit/XML or JSON test reports
- screenshots/video for UI tests
- app/server logs
- environment info
- git diff or PR diff
- timing/history of flaky tests
Store as artifacts:
For example:
- GitHub Actions artifacts
- GitLab job artifacts
- Jenkins archived artifacts
- S3/GCS bucket
- internal log store
Make sure the tool can fetch them automatically based on the build ID or commit SHA.
3) Create a failure analysis pipeline
A simple architecture:
- CI job fails
- Post-processing step runs
- It collects artifacts and metadata
- It sends them to an analysis service or script
- The service generates a structured explanation
- The explanation is posted back to PR comments, Slack, or the CI summary
You can implement this as:
- a shell/Python script for a lightweight setup
- a small internal service
- an LLM-powered analysis agent if you want natural-language explanations
4) Parse test failures into structured data
Before trying to “explain” anything, normalize the input.
Example fields:
test_namesuitestatusfailure_typeerror_messagestack_tracelog_excerptcommit_shabranchchanged_fileslast_known_good_build
This makes it easier to:
- search similar failures
- cluster flaky tests
- generate better explanations
- route to the right owner
5) Add heuristics before using AI
Basic rules can solve many failures quickly:
- assertion mismatch → likely logic regression
- timeout → performance issue, deadlock, or environment slowness
- network error → external dependency or mock issue
- missing file/module → build/package problem
- test passes locally but not in CI → environment/config/flaky test
A rules layer makes the tool more reliable and cheaper, and it improves AI output by giving it a starting hypothesis.
6) If using an LLM, constrain the prompt
If you use an LLM, don’t just dump raw logs. Give it a structured prompt like:
- summarize the failure in 3 bullets
- identify the most likely cause
- cite evidence from logs
- note uncertainty if evidence is weak
- suggest 3 next debugging actions
Example prompt skeleton:
- “Here is the failing test report, stack trace, relevant logs, and git diff. Explain the failure for an engineer who wants the fastest path to root cause. Return: summary, likely cause, evidence, next steps, and confidence.”
This reduces hallucinations and makes the output consistent.
7) Add historical comparison
One of the best debugging features is comparison against history.
Look up:
- previous failures of the same test
- recent flaky patterns
- changes introduced in the last N commits
- whether the failure started after a dependency update
This is especially helpful for:
- flaky tests
- intermittent CI-only failures
- failures caused by infrastructure changes
8) Return the explanation where developers already work
Good delivery channels:
- PR comment
- GitHub/GitLab check summary
- Slack alert
- CI job annotation
- dashboard link
A strong format is:
Failure summary
test_checkout_flowfailed inpayment.spec.ts
Likely cause
- API returned 500 from payment service after request payload schema changed
Evidence
- stack trace line X
- log line Y
- diff in file Z
Next steps
- update payload shape
- verify mock contract
- rerun targeted test with verbose logging
9) Keep a feedback loop
Let developers label the explanation:
- correct
- partially correct
- incorrect
Then use that feedback to:
- improve heuristics
- tune prompts
- build a dataset of known failure patterns
- identify flaky tests and recurring regressions
10) Security and safety
If logs may contain secrets or PII:
- redact tokens, credentials, emails, keys
- limit which artifacts are sent to external services
- prefer self-hosted models or internal services for sensitive repos
- enforce access control on failure reports
Minimal implementation approach
If you want a quick first version:
- Configure CI to upload test logs and reports.
- Add a post-failure script.
- Parse the failing test + stack trace.
- Apply simple heuristics.
- Optionally send the structured data to an LLM.
- Post the explanation to the PR/CI summary.
A Python-based version is often enough to start.
Example output format
Failed test: test_user_login
Summary:
The test failed because the login API returned 401 instead of 200.
Likely cause:
The auth token fixture appears to be invalid after the recent change to token generation.
Evidence:
- Assertion failed at auth_test.py:84
- Log: "Unauthorized: invalid signature"
- Recent diff modifies JWT secret handling in auth/config.py
Next steps:
1. Verify the token fixture uses the new signing key.
2. Re-run the test with auth debug logs enabled.
3. Check whether other auth tests fail with the same signature error.
Confidence: medium
If you want, I can also give you:
- a reference architecture,
- a sample Python implementation, or
- a GitHub Actions / GitLab CI example for wiring this into your pipeline.