Turning git blame Into a Faster Test Debug Session, With Agents
When a test starts failing and nobody knows why, the first instinct is to stare at the assertion and start poking at the code. That's the slow path. The faster path is to ask a simpler question first: what changed, and when? git blame and its relatives answer that quickly, yet I see testers skip straight to print-debugging or re-running with extra verbosity before they've looked at the history at all.
I treat blame as a structured first step, not a way of pointing fingers. A commit hash and a date are enough to pull up the diff, read the change in context and form a hypothesis before writing any debug code. That habit matters even more now that coding agents write a growing share of the commits. History gets bigger, refactors get broader, and "who wrote this line?" is often less useful than "which change touched it, and what was it trying to do?"
Here's the workflow I use, from the raw commands to the editor, to letting an agent run the search for me, plus a few habits that keep history useful when agents are making the changes.
Learn Python, Behave, GitHub Copilot, APIs, and CI/CD by building a real framework you can finish in a weekend.
Blame the lines that matter, in both files
Blaming a whole file is rarely useful. Blame the assertion, the fixture it depends on, or the helper that's misbehaving:
git blame -w -L 42,58 tests/test_orders_api.py
-L limits the output to a line range, and -w ignores whitespace-only changes, so a reformat doesn't hide the real edit. Each line shows a short hash, an author, a date and the line itself. Take the hash and read the whole change:
git show a3f9c12e
Often the cause is right there: a changed expectation, a renamed field, a helper refactored so a side effect moved. You haven't run anything yet and you already have a hypothesis.
The mistake I see most is blaming only the test. Frequently the test hasn't changed at all and the code it covers has. If the test calls create_order(), blame that function's lines in the service module too. For code that moved between files during a refactor, add -M (moved lines within a file) or -C (lines copied from other files) so blame follows the code instead of stopping at the move.
When blame isn't enough: log -S and log -L
Blame tells you who last touched a line. Sometimes what you need is when something disappeared. The pickaxe search finds commits that added or removed a string:
git log -S "order_id" --oneline -- src/ tests/
That's the fastest way I know to answer "when did the response stop including order_id?" To see how a block of lines changed over time, git log -L shows the history of a range, with diffs:
git log -L 42,58:tests/test_orders_api.py
A flaky test that suddenly became a consistent failure almost always has a commit behind it, and these commands find it faster than re-running. When the failure really is intermittent, the approach in debugging a flaky API test that only fails in CI fits better.
Staying in the editor
Switching between terminal and editor breaks your reading flow, so I keep as much of this in VS Code as I can. The Timeline view in the Explorer shows a file's commit history, and clicking an entry opens the diff. Recent VS Code versions can also show blame for the current line inline (look for the git.blame settings), and the GitLens extension goes further with commit messages and diffs on hover. My sequence: cursor on the failing assertion, read the annotation, open the diff, then repeat on the function under test.
Don't trust the commit message too literally. "Fix tests" or "update helpers" tells you almost nothing. The message is a pointer, and the diff is the evidence.
Let git bisect do the searching
When the history between the last green run and now is long, reading diffs one by one is slow. git bisect does a binary search for you, and git bisect run automates it with any command whose exit code means pass or fail:
git bisect start HEAD v2.3.0 # bad commit first, then a known good one
git bisect run ./scripts/test.sh tests/api/test_orders_api.py::test_create_order_returns_201
git bisect reset
Exit code 0 marks a commit good. Codes 1 to 127, except 125, mark it bad. Exit 125 tells bisect to skip a commit it can't test, which is useful when some commits in the range don't build. This only works if your test command reports failures honestly through its exit code. A wrapper that pipes into tee without pipefail will mark every commit good. Also watch for a test that didn't exist in older commits: pytest then exits with a usage or no-tests error, and bisect counts that as bad. Keep a copy of the test outside the repo, or wrap the command so it exits 125 in that case.
This is also a good job to hand to a coding agent. It's mechanical, it runs locally, and its result can be checked. Give the agent the failing test ID, the good and bad refs and the test command, and ask it to report the first bad commit with its diff. It must not change code during the search. You then read that diff yourself before deciding anything.
Keeping history readable when agents write the commits
Blame and bisect are only as useful as the history they read. Agent-written changes tend to be broader than hand-written ones. A small request can come back as a fix plus a rename plus a reformat across several files. A few habits keep that searchable:
- Separate mechanical changes. Ask agents (and people) to put formatting and renames in their own commits. Then list those commits in a
.git-blame-ignore-revsfile and rungit config blame.ignoreRevsFile .git-blame-ignore-revs, so blame skips straight past them. - Mark agent-assisted commits. Many teams add a
Co-authored-by:trailer when an agent wrote the change.git log --grep="Co-authored-by"then filters for them, which helps when you suspect a generated change. - Write the "why" into the message. Agent commit messages default to describing what changed, which the diff already shows. A line in your agent instruction file asking for the reason and the linked issue makes blame far more useful later.
- Keep test edits visible. If an agent changed an assertion in the same commit as the code it tests, that's worth a closer look in review. It's the pattern that makes a real regression look like an intentional change.
A lightweight branch-and-review routine helps with all of this, and I describe mine in a GitHub workflow for part-time developer testers.
From commit hash to bug report and regression test
Once you've confirmed the commit, you have a precise explanation of what changed. The bug report needs the hash, the relevant diff hunk, the failing test and assertion, and expected versus actual behaviour. With the commit identified, the "can you reproduce it?" round trip mostly disappears. There's more on structuring that in turning a failing test run into a useful bug report.
Then write the regression test that guards the contract the commit broke, with the hash in a comment:
# Regression: a3f9c12e renamed 'order_id' to 'id' in the orders response,
# which broke downstream consumers. Guard the field name explicitly.
def test_order_response_keeps_id_field(api_client):
response = api_client.post("/orders", json={"item": "widget", "qty": 1})
body = response.json()
assert "id" in body
assert "order_id" not in body
When someone later asks why that assertion exists, the hash answers it.
The habit is simple: ask what changed before you ask why it fails. If you'd like to practise this alongside building a real API suite in VS Code with Copilot, my API testing courses cover the workflow end to end.