JoltrinDocs Secure agentsGitHub
Documentation menu
Docs / Agent security

testguard: stop agents from cheating on tests

An agent told to make a failing test pass will sometimes change the test instead of the code. testguard reads a git diff and fails when the tests got weaker. It works on any agent's change (Claude Code, Copilot, Codex, Cursor, Gemini CLI) because it looks at the diff, not at the agent.

It reads the diff only. A clean result means the tests were not weakened in that diff. It does not prove the bug was fixed.

Run it

sop-mcp-server testguard --base origin/master

Exit code 1 means at least one blocking finding. --json prints findings for scripts. --staged checks only what you are about to commit.

What it blocks

Finding Example
Test or case removed a func Test... or it(...) deleted
Test file deleted or renamed git mv foo_test.go foo_test.go.bak
Assertion removed two assert lines become one
Assertion edited, no code changed the expected value changed and nothing else did
Assertion that cannot fail assert True, expect(true).toBe(true), assert.True(t, true)
Test skipped or commented out t.Skip, .skip, xit, @Disabled, #[ignore], //go:build ignore
Test focus or expected failure .only, fit, fdescribe, xfail, expectedFailure
Validation removed from code a validate or raise ValueError line dropped to make a test pass
CI made to pass continue-on-error: true, || true, || exit 0, --no-verify
Test run narrowed --deselect, --ignore, testPathIgnorePatterns, --passWithNoTests, -run '^$'
Coverage threshold lowered fail_under or coverageThreshold goes down

What it lists for review

Edited assertions when code changed too, looser tolerances and timeouts, new lint or type suppressions (@ts-ignore, # noqa, //nolint), and new tests with no assertion.

What it misses

  • A test body emptied or cut short with an early return.
  • A test hidden inside a block comment.
  • An expectation changed to match a variable instead of a literal. A check such as if got != 3 is read as an assertion, so changing it to != -1 is caught.

Review those in the diff.

Make every agent go through it

  1. In a clone, run sop-mcp-server testguard --install-hook. It adds a git pre-commit check, and every agent commits through git.
  2. Run it in CI so git commit --no-verify does not get around it. The repository's testguard workflow does this on each pull request.
  3. With Claude Code, sop-mcp-server setup --apply also installs a hook that runs testguard when the agent says it is done and sends the weakened test back to be fixed.

There is no Copilot-specific integration. Copilot's changes are covered by the pre-commit hook and the CI check like any other author's.

How it is tested

The unit tests include one case for each cheat above and cases that must stay clean, such as moving a test between folders. A further test drives real git in a scratch repository, so rename and delete handling is checked against git's own output.

Edit this page on GitHub