r/ClaudeCode • u/leo_heo • 1h ago
Discussion do you let the agent edit existing tests
on our side an agent changing an old test needs a reason written in the task first. otherwise a green run doesnt tell me much
do you lock old tests, or just read the test diff closely in review?
1
Upvotes
•
u/AutoModerator 1h ago
Hey! Thanks for posting to r/ClaudeCode
While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.
For help, project discussions, tips, and general chat, join the ClaudeCode Discord.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/YouthRealistic7125 Vibe Coder 55m ago
Neither tbh. They're not locked, and I can't read code so reading the diff wouldn't help me much
For the payment code I run mutation tests at night. It breaks the code on purpose and checks if the tests notice, and it fails if they catch under 83%. So a watered down test should show up there. And a new test has to fail on the old code first
We did have an exception slip into a test once, it stayed green while our site chat was blocked for a day. Now if a commit touches tests, a script looks for exceptions like that. We don't ask for a written reason though
2
u/zzunkie 43m ago
We don't lock old tests and just check the test diff in review. We had a hand-maintained encode/decode test stay green after a bunch of new request cases were added, so we changed it to compare its list against the enum declaration itself.
1
u/aaronsuns 21m ago
A cheap middle ground is CODEOWNERS on the test directories, so any change to an existing test file requires a named human reviewer even when the agent opened the PR. You can also have the agent put test edits in their own commit, which makes the reason-in-the-task check easy to match against the diff. Neither replaces mutation testing, but both make it harder for a weakened assertion to merge unnoticed.
1
u/powleads 11m ago
I wouldn't lock them completely. Sometimes the old test is wrong, or the behaviour is meant to change, and a hard lock just pushes the agent into odd workarounds in the real code.
I'd let it edit but make the edit loud. Show the test diff on its own, and stop the run if lines have been removed or an assert got looser. Your written reason then has something to be checked against. Adding new tests can go through with much less fuss.
1
u/RodrixLabs 9m ago
I'd split it by what the agent is doing. For a bug fix or refactor, the existing tests are the spec, so I put a line in CLAUDE.md that it must not modify them. If it thinks a test is wrong, it stops and explains why instead of editing. For a deliberate behaviour change, I tell it up front which tests are expected to change.
Enforcement matters more than the rule, though. A PreToolUse hook on Edit/Write that matches your test paths can block the edit or force a confirmation, so the agent can't quietly loosen an assertion mid-run.
Then I confirm that any new or changed test fails against the old code before trusting a green run. A test that passes both before and after the change isn't checking anything.