Why AI Agents Fail at Refactoring Your Messy Tests
The real bottleneck in our current rollout of AI coding assistants isn't some magical missing LLM capability or a lack of prompt engineering talent; it is the absolute disaster that is our codebase's "world" (and by world, I mean the terrifyingly fragile state of our test suites). During our last sprint, we actually tried letting an agent tackle some refactoring work, only to watch it hit a wall immediately—not because the logic was complex, but because our test setup is essentially a pile of incoherent plumbing that makes zero sense to anything not human. Most of our existing tests are built on these massive, hand-assembled blocks of data construction where you have customers, orders, and shipping fields just floating around in a void, and the agent sees this pile of unrelated nouns but has absolutely no idea what the actual business scenario is supposed to be. We have mistakenly treated test setup as just "getting data into a shape" when in reality, every time you write a test, you are making a claim about what the world looks like at that specific moment (a claim that is usually impossible to audit for security or logic errors if it's buried in ten lines of manual object construction). If you are manually stitching objects together, you are basically telling the AI, "Assume this weird, contradictory, and likely non-compliant state exists," without ever actually explaining the rules governing that state. An agent doesn't arrive with a pre-installed understanding of our specific business logic; it is just sampling the patterns we have already foolishly established. If our test suite is just a collection of incoherent data blobs, the agent is going to spit out equally messy, unreliable code. We have to stop just "writing tests" and start building a coherent grammar using descriptive builders like
Next
Entra Agent ID permission management isn't a UI problem →
aCartReadyForCheckout() instead of manual object manipulation, because that is the only way to create a "world model" the AI can actually navigate without hallucinating a way to break our production environment. If we want to raise the ceiling on what these tools can do, we have to treat our test suite as a clear API for the state of the system rather than a dumping ground for random data.All Replies (3)
S
seedrandom
Novice
4d ago
Makes sense. I started adding more context to my test setup files and it helped the agent immensely.
0
L
Same here. My agents kept looping until I simplified the mock data in my test suites.
0
C
Still feels like a gimmick. Traditional unit testing is way more reliable than waiting for some agent to hallucinate a fix.
0