r/SoftwareEngineering • u/MacroProcessor • Sep 07 '25
How do you actually use and/or implement TDD?
I'm familiar with Test-Driven Development, mostly from school. The way we did it there, you write tests for what you expect, run them red, then code until they turn green.
I like the philosophy of TDD, and there's seemingly a lot of benefits--catches unexpected bugs, easier changes down the road, clear idea of what you have to do before a feature is "complete"--but in actuality, what I see happening (or perhaps this is my own fault, as it's what I do) is complete a feature, then write a test to match it to make sure it doesn't break in the future. I know this isn't "pure" TDD, but it does get most of the same benefit, right? I know that pure TDD would probably be better, but I don't currently have the context at my work to be able to cleanly and perfectly write the tests or modify existing tests to make the test match the feature exactly. Sometimes it's because I don't fully understand the test, sometimes it's because the feature is ambiguous and we figure it out as we go along. Do I just need to spend more time upfront understanding everything and writing/re-writing the test?
I should mention that we usually have a test plan in place before we begin coding, but we don't write the tests to fail, we write the feature first and then write the test to pass in accordance with the feature. Is this bad?
The second part is: I'm building a personal project that I plan on being fairly large, and would like to have it be well-tested, for the aforementioned benefits. When you do this, do you actually sit down and write failing tests first? Do you write all of the failing tests and then do all of the features? Or do you go test-by-test, feature-by-feature, but just write the tests first?
Overall, how should I make my workflow more test-driven?
9
u/cihdeniz Sep 07 '25
TDD is a skill you develop over time. It can take months or even years to get truly efficient at it. But if you stick with it, the benefits will come.
Think of how hard it is for a child to learn reading and writing compared to an adult. If you learn TDD early in your career, the investment is much smaller.
Once it clicks, you start applying it everywhere you code. It doesn’t slow you down, in fact, it saves you a huge amount of debugging time. You also communicate better, because you naturally think about requirements and user needs first. You learn to build features incrementally and become more open to change at any point in development, which I see as a big advantage. And on top of all that, it’s just fun to refactor and design without fear.
People who say TDD is a luxury or not a good fit for every project are like those who drop out of school before learning to read and then blame the language for being too hard. Maybe they had a bad teacher, I don’t know.
If you can, find someone who practices TDD professionally and is eager to mentor. If you can’t, just give it a lot more time than a couple of weeks, it will pay off.
2
u/_nickvn Sep 09 '25
What I see coming back a few times in your post is the expectation that you write all the tests for a feature first and then implement it. That's not how it's supposed to work and a lot of people have the same misconception.
What you describe:
Is more like:
It is not something you go through once per feature. It's a cycle you go through many times for a single feature as you do this:
As things change, your tests will change with it. Sometimes you have to update your interface a bit to accommodate new features and you have to update tests you wrote before. Sometimes assumptions you made before turned out to be wrong and you remove tests. But mostly they can stay the same or only change a little bit.
Definitely avoid this, it is an ant-pattern. I'm not saying you should go in blind and start coding, but from what I can tell from your post, you're doing enough upfront analysis. As you experienced first-hand, it is usually either unproductive or impossible to understand everything upfront.
Once you have a pretty good idea of where you want to go, TDD allows you to figure out the details as you go.
23
u/serial_crusher Sep 07 '25
Most good ideas work in abstract, but fall apart if you try to turn them into dogma. This is one of those.
I think TDD is great for fixing bugs, but not for building new features. The black box already exists and has well-defined-enough inputs and outputs that you can say “when I do x, y happens instead of z”, then write a unit tests that ensures z happens when x, then go fix the code and keep running the test until it passes.
This is also more useful with complex integration tests than it is with small-grain unit tests imho. It’s for the kind of situation where you go “yeah this one line change should fix the bug”, but then you find out there’s actually 4 or 5 steps in a complex workflow that fail downstream from each other. The one line you fixed was just the first point of failure.
29
u/R10t-- Sep 07 '25
TDD in theory sounds smart. But in practice it’s just impractical. It’s a waste of time for me to go back and forth between my tests and code when I can look at the code and know exactly what needs to happen in order to make a change.
Personally I find TDD a waste of time
2
u/Euphoricus Sep 07 '25
I'm user and big proponent of TDD. To the point I can't imagine writing code without using TDD. When I try to write code without using TDD, I feel exposed and vulnerable. Not having the solid safety net of solid tests is extremely stressful and makes making any changes to the code feel like anything would break. Returning to code I know was done with TDD feels like I can do anything and be sure that I'm not making a mistake and braking anything.
From what you describe, you might have theoretical idea about TDD (write tests first, duh!) but not much practical experience and intuition on effects of actualy following TDD.
Absolutely not. Writing test first and seeing it fail is extremely important part of bulding a reliable suite of tests. I've seen multiple tests written after the code was "finished" and always, there were cases not covered and tests that didn't actually fail when they were supposed to. The key question you should ask yourself is "Can I refactor and extend this code without fear of breaking it and not having me (or someone else) spend multiple days manually re-testing the whole feature." In most cases of tests written after code, the answer would be clear "no". If that is true, then what even is point of tests, when they don't support extending your code and still having need to waste time manually re-test everything?
That is problem of quality of your tests and knowledge sharing in your team. This is also why XP includes Pair Programming. It greatly improves quality of the code and ensures knowledge is more broadly shared. You could try replacing it with code reviews, but that is exactly same issue as I described above. It just doesn't achieve the same results.
First, I cannot believe you cannot write the test. If you know what code to write, then writing a test to ensure that code does what you expect it to is not difficult. Even if it is first iteration of your code, having a test is possible.
What I feel you mean is that you don't want to "waste" time writing a test and then having to remove or rewrite it. You should think about how you feel about writing code you believe might have to be removed or heavily modified later. Is it really such a bad thing? I would argue that it is better to err on side of writing test and then having to rewrite it, than not writing test and ending with codebase without tests, or subpar tests.
There is technique that Dan North calls 'Spike and Stabilize' that optimizes this workflow. It allows writing a production "draft" of the code, to learn what is actually needed. And then throwing the code out and re-doing it with TDD. But this technique is high-level and requires strong maturity and technical expertise of the team and organization. Not something I feel from your description of your team.
10
u/AnnualAdventurous169 Sep 07 '25
I’m still tying to do it consistently myself, but as I see it, it’s not quite as you describe it. TDD is about building incrementally and iteratively. Even if you don’t know everything you can start, you can start with writing tests for things you do know, and learning the rest as you go. The TDD loop is Red-Green-Refactor. Part of the process is rewriting tests. TDD is also so supposed to help with help encourage better design. If something is difficult to write a test for its a smell that there maybe something that can be improved. And it encourages you to make those improvements as doing so will make your testing step easier.
In TDD you write a single failing first, not all of them. It’s test by test and generally not the even the whole function at a time. For example when I first started learning, i was taught as the first test to write a test for a function that does not exist, that fails. Write the stub for the function -> that passes. Then make small steps from there
1
u/I1lII1l Sep 08 '25
Death to TDD, long live SDD (spec driven dev)!!
3
u/ub3rh4x0rz Sep 07 '25
That's the really cool thing, you don't
3
u/brunoreis93 Sep 07 '25
No one does that.. just test after and you're good to go
3
u/failsafe-author Sep 08 '25
I love TDD. I practice it and have for over a decade.
I use red/green/refactor whenever it makes sense. Sometimes, however, when you are figuring stuff out and the plan isn’t clear, it just makes more sense to explore with code and write the tests after.
It’s a good practice, imo, but not to be religious about.
5
u/fearthelettuce Sep 07 '25
Step 1: go around and preach the gospel of TDD Step 2: look down upon everyone else for their primitive development practices
2
u/aLpenbog Sep 08 '25
I like the idea of TDD but I rarely use it. The main problem for me is that I work with a language without interfaces or function pointers/functions as arguments and a shared database on which multiple applications and people are working.
So if I want to use TDD, I can only use it with pure functions if I don't want to include some off switches in the production code and I don't want to put stuff in there just for this purpose.
I use it if I have complex pure functions which doesn't happen that often. Imo TDD is not that much about not having bugs but keeping things working which have been working before changing/refactoring something and mostly it is about design and thinking about the interface beforehand.
I do that even without TDD.
2
u/Lopsided_Judge_5921 Sep 08 '25
I write a little bit of test then write a little code, then add a little more to my tests and then add a little more code, when the feature is complete I then refactor, over and over until I feel it's simplified as much as possible. If I'm jumping on a new project my first test is `assert False`. You'd be surprised how much time it saved me debugging by making sure that test fails exactly like it should.
2
u/er824 Sep 08 '25
I find writing and thinking about the tests at the same time I write code helps me think more completely about the Contract I’m creating and think through corner cases that tend to get glossed over if I just write the code first.
I also think when you write the tests after the fact you end up with a much less complete test suite because there is a tendency to focus on the happy case.
2
u/IronSavior Sep 10 '25
TDD is great for certain things. If I'm having trouble conceptualizing a solution design, it can help to start by writing tests. That will focus my mind on the consumption side of the interface (rather than the implementation). Even if I'm not facing a block, this kind of thinking can often yield better designs.
Many years ago, I decided to commit fully to TDD for several weeks as an exercise and it was an absolute level up for me. I don't use it in a strict way since then, but I do prefer to at least outline my test suite before implementation.
It's a tool in the toolbox.
2
u/Intelligent_Part101 Sep 11 '25 edited Sep 11 '25
OP, your intuition informed by your experience is indeed correct. Ignore the hype about writing "failing" tests first. It was promoted by "consultants" on the internet whose record of writing successful software of any consequence is THIN. TDD is based on the idea that you can build your software out of little pieces that are each perfected and then joined together--the software architecture sort of "falls out" of this--somehow. Not how it actually works.
1
u/danielt1263 Sep 07 '25
Many times when I'm writing a "scenario" (as defined by cucumber, ie "given, when, then"), I know exactly how the code should look before I write a single line. In those cases, I don't bother with TDD (and if the logic is stupid simple, ie no decisions are being made, I don't bother even writing the test at all.)
Occasionally, I'm not entirely sure what the logic between the "when" and "then" should be exactly. These are the times when I start with the tests.
A while back, I finished a client project and then they informed me they wanted me to have 80% test coverage. I don't normally measure test coverage, but I found I had almost 36% coverage just from testing the bits I wasn't sure about. I then added tests for all the stupid simple logic and that got me up to 65%. So I rounded the tests out with a few integration tests for the last 15%.
1
u/seia_dareis_mai Sep 08 '25
A lot of this depends on how anal you are about it. You can describe the first behavior that you want to implement, write a test that validates it, implement it. Consider a date picker: *Input renders *Datepicker opens when input is focused *Datepicker accepts x format(s) if the user types it *Datepicker rejects x format *When date format is rejected, datepicker surfaces a form error *Datepicker selects date when clicked *Datepicker closes when date is selected *Datepicker prevents selection of dates in the past *Datepicker prevents typing of dates in the past (surfaces error)? *Datepicker displays error if user attempts to submit form with an empty field
All of those can be written before the implementation. Sometimes it's considering different inputs, writing a descriptive test title, then implementing.
Personally I make my intern Claude write the tests and review, these days.
1
u/TeeTimeAllTheTime Sep 09 '25
TurDD
1
u/nitkonigdje Sep 09 '25
Tdd as practice works.
I do find it more costly for your average n tier app. Like if you are doing a data entry app, which is like 80% of work out there, you really do not need TDD to design that shit. So in this case tdd brings only tests.
Which is nice but really not that big deal.
Try to enforce coverage on those, and you are spending more time than there is value to it.
Like 80% of development is low effort cheap as it gets low user count apps.
1
u/tadiou Sep 10 '25
Test first. Feature second. Test third, feature fourth.
Basically you write your first test, before you even write your first object/class/function. You say okay, we're going to have to return a value, because literally the most important thing you can start by doing is understanding what kind of value you're going to return. Your input and outputs are literally the most impactful thing you can work on in a feature.
Literally, sometimes I'm like, yeah, this is gonna be a collection of xyz. sometimes it's a map, sometimes it's a primative type. whatever. How do I transform what comes in to what i need to come out. Step by step.
That's the brilliance of TDD. I turn an set of integers into the correct list of records. I turn a string into a set of scalar values.
You're forced to look at how you're transforming things. You can say "okay, i'm returning the right shape, but the values are wrong, what test could I do that'd get me a little closer to that value".
Writing the individual tests then force you to cull, strengthen, and abstract a little more correctly, that you're reducing the surface area, and your job then becomes as a software engineer to figure out how the pieces get put together, you can better mock the values that are tested elsewhere because the implementation details have been resolved within that.
I'm such a huge fan that I only do it about 50% of the time.
1
u/PunkRockDude Sep 10 '25
Part of the point of TDD is to solve the problems that you are saying are the thing making it difficult. If your requirements are ambiguous you should solve that before accepting the work. It also forces you to consider test ability in your designs which you don’t get the benefit from later. Having said that I usually make my teams do it at the beginning of a project then let them figure it out later because it isn’t always helpful and sometimes a team matures into it and other times then nature out of it just depends. They always have to ship the code self testable so that part remains.
1
u/iOSCaleb Sep 10 '25
There’s nothing “test driven” about that. It’s just development that includes tests. The whole point of TDD is that you write a test first and in so doing you create a requirement. When your code passes the test you will have, by definition, met the requirement.
TDD seems like a great idea, but it’s not a panacea. You still have to define requirements, and that’s often the hard part.
1
u/[deleted] Sep 10 '25
TDD is great when fixing bugs. Add a test that should pass. Run it. See it fails. Fix problem. Get some kind of confirmation that you fixed right thing.
This of course requires that are some tests in place already.
1
u/arihoenig Sep 11 '25
Ideally, the QA team writes the unit tests before a single line of production code is written, then the development engineers work at getting all the tests to pass. Once the devs have all the tests passing , then, so long as the high level design documents that QA used to implement the unit tests are not bugged, you now have a set of code that is ready for integration.
1
u/Direct-Fee4474 Sep 11 '25
TDD's great when you're refactoring something old, when you're explicitly addressing a regression, or when you're "using TDD" to figure out how you want to interact with something new. "what should it feel like to interact with this? what data should it own? what's the api surface look like" but that's about it. But I wouldn't even call that TDD -- it's just "doing things the way a normal person would do things." TDD, when actually called TDD, usually only exists in its most extreme and near-religious incarnation, where it falls into the same category of things like DRY and the Gang Of Four patterns where it's strictly the domain of pedantic and insufferable asshats that wear toe shoes.
1
u/[deleted] Sep 21 '26
Personally I like to use TDD when creating templates that will be used in multiple important parts of the code and of course just generally when designing crucial parts of an application, like log in forms or payment integration.
-1
u/skibbin Sep 07 '25
Usually when people say TDD they mean Unit Test driven coding. I find that works well for logic, or where the expected output for a given input is well known.
Usually when people say BDD they mean automated browser tests, or feature functionality tests. I actually find these more useful for driving development as it helps me think about the user experience, the journey, or the product. It's implementation agnostic so I feel less like I'm writing the code and test together in my head at the same time.
Outside-In, Top-Down is a way of building things where you start with feature tests, then move on to code tests.
-5
u/Ab_Initio_416 Sep 07 '25
ChatGPT is excellent for quick, inexpensive, first-pass research.
My prompt to ChatGPT: In software development, TDD emphasizes writing tests first. Is there any evidence that is more effective?
ChatGPT responded:
Test-Driven Development (TDD) has been around for over two decades, and there’s been quite a bit of empirical research into whether it actually delivers on its promises. The evidence is mixed — some positive effects, some neutral, and some caveats.
<snip of thousands of characters>
Final Thoughts
You can ask it to produce an annotated reading list of over a dozen actual studies (all of which exist; I followed the links)
1
u/[deleted] Sep 08 '25
[removed] — view removed comment
1
u/[deleted] Nov 11 '25
[removed] — view removed comment
0
u/[deleted] Sep 15 '25
[deleted] — view deleted comment