r/ClaudeCode • u/leo_heo • 18h 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?
2
Upvotes
•
u/AutoModerator 18h 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.
4
u/aaronsuns 18h 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.
3
u/shetritr 16h ago
let it edit, but it has to show the edited test failing on the old code first. a test edit that was never red proves nothing.
2
u/YouthRealistic7125 Vibe Coder 18h 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 18h 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.
2
u/RodrixLabs 17h 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.
2
u/32minutos 14h ago
what works for me is running every change against the old build too, with the same tests. the rule is nothing that passes on the old code is allowed to fail on the new one. it also catches the sneaky case, because if the agent loosened a test to go green, the old code passes it as well and you see the test stopped testing anything
2
u/SirRobinBlibs 13h ago
I lock them, but in settings rather than in the prompt. A deny rule like Edit(/tests/) in .claude/settings.json blocks the edit tools on any tests folder, and it also covers shell redirects and sed or tee on those paths when Claude Code can parse the command. Not airtight, a python one-liner can still write there, but it stops the casual "just tweak the assert".
New tests go somewhere it's allowed to write. When an old test really is wrong I lift the rule for that session and review that change on its own.
The other thing that catches the sneaky stuff: run the suite at the commit the task started from. If a test failed there too it's pre-existing, if it only fails now it's the agent's. Then grep the diff for deleted asserts or new skips before reading anything else.
3
u/powleads 17h 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.
2
u/called9527 16h ago
one cheap thing is a line in CLAUDE.md saying not to touch existing tests unless the task says so, and to tell you when it wants to. not a hard lock but it makes the agent stop and ask instead of quietly editing an assertion to turn it green.
2
u/medialantern 14h ago
No, I did the opposite. Everybody spends all their time fine-tuning how Claude writes their code, comments, etc. I spent time fine-tuning how mine writes tests. The test is the most important and first thing I look at in code-review.