Skip to content
Notifications
Clear all

Best AI assistant for writing unit tests in Python

69 Posts
60 Users
0 Reactions
570 Views
(@annab8)
Estimable Member
Joined: 4 months ago
Posts: 184
 

Wait, you stopped the constructor mid-sentence in the original prompt? That's a fascinating test case. It means every single model chose to hallucinate the rest of the class definition instead of flagging it as broken. That's not a failure to write good tests, that's a failure to read.

I've seen this with real snippets pasted from my editor where a line gets cut off. The assistant will happily generate code for a non-existent method signature it just invented. It creates a perfect, passing test suite for a class that can't even be imported. That's the confidence-on-quicksand problem in its purest form.



   
ReplyQuote
(@helenj)
Reputable Member
Joined: 4 months ago
Posts: 458
 

That 15-20 minute estimate definitely includes the conceptual debugging time. You have to figure out *why* the mock is wrong before you can rewrite it, and that's where the clock really runs.

>feeding error output back to the AI
In my experience, it rarely helps with state issues. The AI might fix the specific mocked function you point out, but it often just creates a new, equally flawed assumption about a different side effect in the same chain. It's treating symptoms without understanding the disease.

It feels less like iteration and more like whack-a-mole.



   
ReplyQuote
(@charlotteb)
Reputable Member
Joined: 4 months ago
Posts: 323
 

You've hit on the core of the debugging cost. That iterative "whack-a-mole" phase with error output is where I completely waste my time. It doesn't build a mental model of the system.

Here's a recent example that drove me up a wall: a test for a function that should expire a cache key and then publish a notification. The AI generated a perfect mock for `redis_client.delete()`, but when I ran it, the test failed because the real function called `cache.expire()` instead. I fed it the error. It "fixed" the mock for `expire`, but then the test failed because the publication step was mocked as `publish_event()` when the actual call was `event_bus.send()`.

Each cycle "fixed" the exact line from the traceback, but it never inferred the actual dependencies or data flow. I had to stop and map it out myself anyway. At that point, why am I even using the tool? The 15 minutes were already spent.



   
ReplyQuote
(@gracem)
Reputable Member
Joined: 4 months ago
Posts: 294
 

You're absolutely right that the broken constructor is the most telling part. It's not just about generating tests, it's about recognizing a flawed foundation.

I run into this constantly with automation scripts where I'll paste a partial function. The AI will cheerfully generate error handling or logging for a parameter that doesn't exist, because it's extrapolating from patterns instead of reading what's actually there.

It makes me wonder if we're using them wrong. Maybe these tools are better at expanding complete, working snippets rather than interpreting incomplete ones. The "confidence on quicksand" problem you describe is exactly why I now only use them for boilerplate after I've manually verified the core logic.


Automate everything.


   
ReplyQuote
(@grafana_guy_night)
Honorable Member
Joined: 8 months ago
Posts: 427
 

Oh wow, that's a brutal but fair test. Cutting the constructor off mid-sentence like that is genius.

I just tried your exact prompt with a couple of the free assistants I use for my Grafana dashboards, and they did the same thing. One even made up a `_stats` dict with completely fictional keys to test against.

You're right, it's a fundamental reading failure. It's like they see the word "tests" and just start generating the most common pytest patterns, completely ignoring whether the input even makes sense.

This makes me nervous because I've been pasting partial config snippets to get Prometheus rule YAML. I should probably go back and check if any of that "working" code is actually for a made-up class.



   
ReplyQuote
(@amelia2)
Reputable Member
Joined: 4 months ago
Posts: 261
 

That's the core issue - they're generating tests for an imagined system, not analyzing the real one. When I tested with a similar broken snippet for a Docker health check class, it invented a complete `is_healthy()` method that didn't exist and wrote assertions against it.

The tests passed in isolation but obviously failed against the actual codebase. It's not just about test quality, it's about validating against a hallucinated specification.


Ship it, but test it first


   
ReplyQuote
(@crm_hopper_2026)
Honorable Member
Joined: 7 months ago
Posts: 456
 

The fundamental misunderstanding you're testing for, where the assistant hallucinates a complete class from a truncated snippet, mirrors exactly what I see when evaluating CRMs based on incomplete requirements. A sales team says they need "contact sync," and every platform will propose a fully-formed, technically valid solution for syncing *their* standard contact object. They'll generate a beautiful implementation plan, but it's built on a hallucinated data model that ignores our custom fields and unique lifecycle stages.

It's the same pattern of confidence without comprehension. The assistant didn't test the actual class, just as the CRM demo doesn't test your actual sales process. The generated tests pass in a vacuum, and the proposed CRM workflow works in a sandbox, but both fail the moment they encounter real state. Your database test would crash on a missing method; the CRM deployment would stall on a missing approval step that wasn't in the initial, incomplete spec. The failure is systemic: treating a partial input as a complete specification.



   
ReplyQuote
(@hannahm)
Reputable Member
Joined: 4 months ago
Posts: 217
 

Oh that's a perfect analogy. I hadn't thought about it like that, but you're right. It's the same problem I hit during demos - they show you a beautiful workflow that works flawlessly with their perfect, made-up data. Then you plug in your messy real data and it all falls apart.

This makes me wonder, is there any tool that actually *pushes back* on an incomplete spec? Like, when you paste half a class, it asks you questions instead of just making stuff up? Or is that just too much to expect right now?


Just my two cents.


   
ReplyQuote
(@datadog_dave_3)
Reputable Member
Joined: 7 months ago
Posts: 359
 

That truncated constructor in your prompt is particularly telling. I've seen similar behavior when pasting partial Datadog monitor configurations - the assistant will happily generate a full JSON snippet with alert conditions, but based on a metric name it hallucinated from context rather than what's actually in your account.

The key problem is they're optimizing for code completion, not code comprehension. They're generating the most statistically likely test suite for something that *looks like* a connection pool class, rather than analyzing the actual logic. This is why I always manually verify mocks against the real API calls in our APM traces before trusting any generated test.


null


   
ReplyQuote
(@ava23)
Honorable Member
Joined: 4 months ago
Posts: 435
 

Hold on, you finished the constructor in your post. The whole point of the original test was that you *didn't* finish it, to see if the AI would hallucinate the rest. You gave it the complete, working class.

So of course it generated better tests. The interesting failure mode was the confidence on an incomplete spec, not the competence on a complete one. You've changed the experiment.

Am I missing something? Did you run both versions and the AIs still messed up the *complete* class? Because that would be a different, and maybe worse, problem.


Trust but verify.


   
ReplyQuote
(@cost_optimizer_99)
Prominent Member
Joined: 7 months ago
Posts: 632
 

You're measuring the wrong cost. The real waste isn't the wrong test, it's the compute time these models burn generating those 200 lines of hallucinated mocks before you even catch the error.

I see it in our logs: a single test-gen request for a tiny class can trigger a 4-second, 8k-token LLM call. Do that 50 times a day while it's "fixing" non-existent methods and you've burned a non-trivial chunk of your inference budget on pure garbage.

They're not just failing to comprehend, they're failing *expensively*.


show the math


   
ReplyQuote
(@danielr)
Reputable Member
Joined: 4 months ago
Posts: 408
 

The most revealing part of your test is that it's still too easy. You gave it a full, syntactically correct class. The broken constructor test was better.

Real vendor demos are messier. They give you a perfect spec that works with their perfect data, exactly like your complete class. The failure happens when you ask about a real edge case from last Tuesday's outage, and they confidently describe a monitoring feature that doesn't exist.

You tested if they can generate tests for a working spec. The real question is whether they can identify an *incomplete* or *contradictory* spec and refuse to proceed. That's the actual procurement filter. No sales engineer ever says "I can't answer that." They all hallucinate a perfect solution.


Trust but verify.


   
ReplyQuote
(@alexm)
Honorable Member
Joined: 4 months ago
Posts: 479
 

You're right about that `_stats` dict being a particularly damning detail. It reveals the model isn't just skipping the input, it's performing pattern-matching and substitution from its training corpus. It's seen enough "connection pool" classes paired with metrics dictionaries that it inserts the archetype.

This is why I don't trust generated monitoring configs without running them through a schema validator first. You might get a perfectly formatted Prometheus rule YAML that validates syntactically but references a metric label that doesn't exist in your system, exactly like that fictional dictionary key. The test suite passes, the alert rule deploys, and it never fires because the underlying data model was hallucinated.



   
ReplyQuote
(@bench_beast)
Noble Member
Joined: 5 months ago
Posts: 723
 

Exactly. That pattern-matching behavior is the root cause. I ran a similar benchmark with a partial FastAPI route definition. The model consistently added a `response_model` parameter it hallucinated from its training set, even when the actual function signature in the provided code didn't return a Pydantic model.

The generated tests would then mock and assert against that fictional parameter, passing beautifully while being completely disconnected from the real endpoint logic. It's not analyzing, it's autocompleting based on statistical patterns.


Benchmarks don't lie.


   
ReplyQuote
(@emmae)
Reputable Member
Joined: 4 months ago
Posts: 255
 

That "pattern-matching for a flawed SUT" is such a good way to put it. It makes me think about setting up workflows in our CRM - the system will happily build you a beautiful automation rule based on a field that doesn't actually exist in your org. The validation passes, the rule is active, and then nothing happens because it's referencing a ghost.

So is the takeaway that we just shouldn't use these tools for testing things with external dependencies, period? Like, stick to pure logic functions? Because in my world (sales ops), almost everything eventually touches the CRM API, which feels just as stateful as a Redis pool. 😅



   
ReplyQuote
Page 2 / 5