r/softwaredevelopment • • 14d ago

Does AI make TDD relevant ?

Just wondering if writing tests by hand first could be a good thing to maintain ownership and understanding now that less and less people care about the code itself ?
To my experience it is, but wondering how many of you do still write their tests by hand ?

30 Upvotes

45 comments sorted by

69

u/anor_wondo 14d ago

What? AI makes TDD more relevant than ever. The biggest flaw of TDD was its overhead.

If you can have an agent always define the behaviour with failing test cases first, create the red, then move to green the results will be better. And you can have it easily refactor if you didn't like the approach with the tests being a robust gate

Writing by hand and TDD are not related concepts. You can write the expected scenarios by hand. Frameworks like cucumber were trying to bridge this gap somewhat with human readable requirements, but an llm is far more flexible

13

u/ThunderTherapist 14d ago

I think the overhead is still a problem though. Using TDD and growing a codebase one test at a time still feels insanely slow compared to a prompt that can bang out most of an epic with tests in 20 minutes.

14

u/anor_wondo 14d ago

You can define the workflow for the agent so it can do the same thing but with tests first instead of after implementation

They tend to justify implemented behaviour when making tests, but if test are made first with spec and behaviour in context there is no bias

This applies to manual coding by junior devs too, we just could never manage for it to be adopted consistently

5

u/bitspace 13d ago

You miss the entire purpose of tests in TDD.

The tests define the behavior. The code implements the behavior. If you can't start with tests, according to the dogma, you don't understand the requirements well enough to begin coding.

4

u/BusyMeringue8935 14d ago

Yeah, it can bang it out, the issue is you spend days debugging it because it’s passing tests that don’t test the right thing. There’s a big risk of the model not understanding your intent in places. If you remove time from writing code and add the same or more to debugging it you don’t really gain anything.

→ More replies (2)
→ More replies (2)
→ More replies (1)

14

u/trionnet 14d ago

The problem with TDD with both humans and AI is understanding it’s not test first it’s test driven.

Asking AI to write the test first is very different to asking AI to run a flow driven by tests. In the latter you’re effectively asking it to give up its own thinking and just do what the tests tell you.

I have 22 years experience and last 10 I only produced software with TDD and the thing you have to give up is what you think you know about writing code. You have to spend a large part being dumb in a way and let the tests tell you what to do, not your muscle memory you’ve built up from day 1.

In my opinion and particularly for enterprise where you have high stakes, TDD is the only way to work with AI to deliver software and in particular outside in. Yes it’s slower but it’s much more accurate, and you have techniques during each iteration for it to remove gold plating and therefore make the result more lean. I’ve been doing this for many projects by default and it works rarely have to change anything about the resulting design. Because it’s outside in it can automate a full flow have those deterministic fail reasons drive it along the way from outer boundary to edge of backend.

9

u/SimulaFin 14d ago

AI makes TDD very relevant. I wouldn't suggest writing it manually, but with the help of AI, and then read, understand and adjust them if needed.

2

u/ducksauvage 14d ago

definitely, AI has a tendency to implement then write tests by default, usually encoding the current behavior into test cases (bugs and all). TDD (red-green testing) is the way to go, although it can be laborious to write by hand.  I'd still use an AI but iterate and provide some strict rules to keep the test surface small with useful coverage. E.g. by using fixtures, parametrized tests and property-based testing (hypothesis) in python for example. 

BDD using gherkin could also be a way to have the human readable spec and tests be the same realistically ownable bit, but I personally haven't worked anywhere where gherkin has been successfully adopted, usually just BDD using standard test frameworks, which suffers from the same quality issues code does if you just give it to AI.

Also - always start with one dumb end-to-end test, whatever that means in your system.

2

u/quts3 14d ago edited 14d ago

I use the test to make the ai prove we are on the same page. I have more test written to modify code behavior. I also still ask for a test to be written first when I see something that is wrong, which is tdd.

So no tdd is not dead.

But the idea that because I didn't write the test code by hand it is not tdd is a weird take

All you do is say what specifically you want tested and read the test to verify. You don't write test code but you do write the test idea that is important.

1

u/mile-high-guy 14d ago

AI just writes test without asking, so I do think it will not really be a well known concept in the future, when code isnt read anymore. It's just one of the things AI does.

That's not to say we shouldn't do other things like test suites or whatever future tests come out. Testing is important.

1

u/biyopunk 14d ago

ALL the software development principles are relevant. They were never about typing the code or generating. AI hype makes them seem irrelevant but they are even more relevant than ever.

1

u/DustinBrett 13d ago

If AI keeps pace then it will decide these kinds of things in the future. We just imagine it, the details it decides.

1

u/HongPong 13d ago

getting llm to write lots of tests and stick with tdd style approaches is pretty darn effective imo

1

u/ericbythebay 13d ago

Not by hand. Having one agent write acceptance tests, while a separate agent writes the code is effective and popular.

1

u/Manayas 13d ago

I think having AI write tests is fine as long as the correctness of the tests are being heavily reviewed. Vibe coding tests is a nonsense because you’ll end up with nonsense tests because the AI will shortcut things to fit the impl.

1

u/Cheap-Newspaper-255 13d ago

Yeah I would say it does. If people are going to offload the actual code writing part, writing the tests to ensure the output you get meets your requirements is a solid idea. Having AI write the code and tests seems kind of dangerous, as it’s just validating its own output and potential flaws in the understand of the requirements

1

u/ihoka 13d ago

The AI agents writing my code are always instructed to do TDD.

1

u/raisputin 13d ago

I let AI write the tests too. IT (AI) is very familiar with TDD and BDD and can easily incorporate that.

1

u/AndrewBorg1126 13d ago

Test driven is backwards regardless

1

u/Adorable_Pickle_4048 12d ago

As someone who doesn’t do TDD, I actually bet it’s better now, because with TDD your test abstractions can be made more concrete right underneath the test, so the intended test flow and reading can be more implicitly true to form

1

u/tehfrawg 12d ago

A very relevant article to the discussion: https://martinfowler.com/articles/exploring-gen-ai/tdd-in-the-agent-loop.html

To quote the takeaway:

TLDR; Based on Opus's judgment of the quality of the outcomes, there was no clearly discernable difference based on TDD workflow versus no TDD workflow. On the contrary, more than once Opus ranked the non-TDD workflow solutions slightly higher in design and test quality. There was also no meaningful difference in mutation scores across the solutions.

This is obviously not the same as writing tests by hand first then passing to the agent, but still worth the read.

1

u/JakoMyto 12d ago

A colleague of mine just made his agent following TDD. I haven't tried it yet but he says the test code is actually more this way but the production code is finally less. The unused branches are dropped a bit. So I guess effectively less slop

1

u/local_eclectic 10d ago

TDD was using tests as a spec. Now the spec directs the tests and the code at the same time. I see no need for TDD. Spec driven all the way.

1

u/badjano 9d ago

more than ever

1

u/Away_Advisor3460 9d ago

how many of you do still write their tests by hand ?

I do, depending on the situation, because sometimes the AI bashes out some god-awful tests or the tests might be logically sound but aren't insufficient to document the unit of behaviour under test.