r/SoftwareEngineering • • Mar 10 '25

TDD on Trial: Does Test-Driven Development Really Work?

I've been exploring Test-Driven Development (TDD) and its practical impact for quite some time, especially in challenging domains such as 3D software or game development. One thing I've noticed is the significant lack of clear, real-world examples demonstrating TDD’s effectiveness in these fields.

Apart from the well-documented experiences shared by the developers of Sea of Thieves, it's difficult to find detailed industry examples showcasing successful TDD practices (please share if you know more well documented cases!).

On the contrary, influential developers and content creators often openly question or criticize TDD, shaping perceptions—particularly among new developers.

Having personally experimented with TDD and observed substantial benefits, I'm curious about the community's experiences:

  • Have you successfully applied TDD in complex areas like game development or 3D software?
  • How do you view or respond to the common criticisms of TDD voiced by prominent figures?

I'm currently working on a humorous, Phoenix Wright-inspired parody addressing popular misconceptions about TDD, where the different popular criticism are brought to trial. Your input on common misconceptions, critiques, and arguments against TDD would be extremely valuable to me!

Thanks for sharing your insights!

45 Upvotes

126 comments sorted by

View all comments

58

u/flavius-as Mar 10 '25 edited Mar 10 '25 • Diamonds are Forever More of this please

I'm not working on games, but complex finance and e-commerce software.

It works, but the problem is that the key word in TDD is not testing, it's everything else.

Tidbits:

  • definition of "unit" is wrong. The "industry standard" of "one function" or "one class" are utterly wrong
  • usage of mocks is wrong. Correct: all 5 types of test doubles should be used, and mocks should be used sparingly and only for foreign system integration testing
  • TDD is very much about design and architecture. Testing can be made easy with great design and architecture
  • red flag: if you have to change tests when you change implementation details, you have a wrong definition of unit and a wrong design and architecture due to that
  • ports and adapters architecture is a very simple architectural style. And it supports a good definition of unit just nicely

Without experience in game development, in P&A I imagine the application consists of the game mechanics, completely isolated from the display. A unit would be a single command. In business-centric application we would call that an use case.

The rendering etc would be adapters implementing the ports.

13

u/Aer93 Mar 10 '25

This is gold, this matches and reinforces my whole journey through TDD... why do you think popular content creators get this so wrong? I'm tired of seening popular streamers like Theo or ThePrimeagen getting it completely wrong and I feel bad for the people starting hearing their advice.

12

u/flavius-as Mar 10 '25

They're likely focused on content creation and don't have much time to deeply reflect on these nuances.

The software industry talks a lot about principles, but principles aren't everything. We can all agree and say we follow DRY, SOLID, KISS, and so on.

But principles alone are insufficient. These principles need to be organized into a hierarchy. When trade-offs are necessary, which principles do you prioritize? For instance, if you had to choose, would you value SOLID principles more than DRY, or vice versa?

Personally, I place the principle "tests should not need rewriting when code structure changes" very high in my hierarchy. This principle then shapes my interpretation of everything else related to testing, with other practices and ideas falling in line beneath it.

9

u/Aer93 Mar 10 '25

This matches pretty well with my team's experience:  "tests should not need rewriting when code structure changes" is very hight for us too. If we have test that change with the implementation, we usually discard them as soon as the implementation changes, we catalog them as implementationt test, which might have been useful for the person writing the code, but not worth maintaining as soon as the implementation slightly changes.

We tend to experience that the best test we have test the core interface of a given subsystem. Then we can run that test under different implementations, sometimes we even develop fake implementations which are useful for other tests and experimentation.

As an example, something as simple as a CRUD database interface. We have test that describes that simple interface that we decided defines all what we need for such a database, and the expected beahviour. The test are written at the interface level, so it's very easily to test different db . We even have a fake implementation that uses a simple dictionary object to store the data, but behaves exactly as we expect, and we can inject this one if needed for things (not an example of good design, just of versatility)

3

u/flavius-as Mar 10 '25

Well we would likely work very well together in a team then.

  1. When I have to rewrite tests, I first ask if requirements have changed and if yes, then changing the data of the tests is fine.
  2. The next check is whether the tests need changes because the boundary has changed (speaking of: unit testing is boundary testing). If yes, then the change is fine. These changes go to a backlog of "learnings" to check if we can go up an abstraction level and derive generic principles from that to prevent further design mistakes. Not all of these lead to learnings though.

Boundaries (contracts) tend to become stable over time, so that's generally fine.

3. If 1 or 2 don't kick in, I put that on a backlog of bad design or testing decisions because it's that's what they likely are. Depending on the outcomes of the analysis, those tests get rewritten or removed, or coupled with some refactoring.

→ More replies (6)

9

u/dreamsofcode Mar 10 '25

As a content creator, please take advice from content creators with a healthy dash of salt.

Software development is incredibly nuanced and there is no "right way" of doing things. Just different ways, each with their own pros and cons.

I agree it's a problem when people getting started in the field take advice from others as gospel. In reality I believe software development is about trying these different approaches as seeing what works for the individual, the team and the project.

→ More replies (1)

7

u/ThunderTherapist Mar 10 '25

This isn't really a new problem. We've had developer evangelists for years. They used to blog before they vloged. My 2 cents is they only really ever needed to produce hobby code. They're paid to promote the latest tool from their sponsor so they create a hello world or a simple crud app and that's as deep as they get. They don't have to battle with 10k other lines of legacy crap that's not well tested.

My other 2 cents is controversial opinions get better engagement and are easier to produce than nuanced balanced viewpoints. DHH is a great example of this. What a load of shit he talks and people love him.

→ More replies (1)

5

u/caksters Mar 10 '25

great points.

I am a mid-level engineer (around 5 yoe) and a big fan of TDD but I haven’t had enough practice with it.

it requires discipline and practice. Initially I made many mistakes with it by thinking that units if code are classes. Obviously this made my project code heavily coupled with the tests (when i refactor the code, i need to refactor the tests).

Later I realised, I need to capture the behaviour if the requirement. So the unit is a small unit of system behaviour rather than unit of code.

Another tricky part is to come up with a meaningful test initially. This requires to understand high level requirement if what I want my piece of code to actually do. This is a good thing of course, but often we as engineers like to start coding before we have understood the problem.

Obviously for fixing bugs TDD is great, because it forces you to come up with a way to replicate the bug in form if a test and then write a code to fix it.

From trial and error, I have found that when I am working in something new (my personal project), I like to develop a quick PoC. Once I got something working, then I know what I want my system to do. the. I can start a completely new project and follow more TDD approach where I write tests first and only then the code. However I would like to learn more about how I should practice TDD as I believe it has an immense potential when you have gained enough skill and confidence in it

16

u/flavius-as Mar 10 '25 edited Mar 10 '25 • Thanks

I'm glad you came to those realizations. Mapping your experiences to mine, yeah, it really seems you're on a good track. It's always cool when others figure this stuff out through actually doing it.

Regarding "TDD for bugs" - nah, TDD is absolutely key for feature development too. It's not just for cleaning up messes afterwards; it's about building things right from the start, properly designed.

What's been a game changer for me is data-driven TDD, especially when you combine it with really clean boundaries between your core domain and all the external junk. Seriously, this combo makes testing way easier and keeps things maintainable, especially when you're figuring out your testing boundaries.

Think about it – data-driven tests, they move you away from tests that break every time you breathe on the code. Instead, you nail down the contract of your units with data. And "units" isn't just functions or classes, right? It's use cases and even facades for complex bits like heavy algorithms – those are your units, your testing boundaries. Fixtures become more than just setup; they're like living examples of how your system behaves for these units. They're basically mini-specs for your use cases and algorithm facades - that's how you define your testing boundaries.

And Ports and Adapters, that architecture you mentioned? Gold for this. It naturally isolates your app core – use cases, algorithms, all that good stuff – from the chaotic outside world. This isolation lets you test your core logic properly, in total isolation, using test doubles for the "ports" to fake the outside. Makes tests way simpler and way more resistant to infrastructure changes. Data-driven TDD and Ports & Adapters? Perfect match. You can nail down and check use case behavior, even complex algo facade behavior, with solid data, within those clear testing boundaries.

So, yeah, all my unit tests follow the same pattern, aimed at testing these units - use cases and facades:

  • Configure test doubles with fixture data. Fixtures pre-program your dependencies for the specific unit you're testing. You literally spell out, in data, how external systems should act during this test. Makes test assumptions obvious, no hidden setup in your testing boundary.
  • Exercise the SUT with a DTO from fixtures. DTOs from fixtures = consistent, defined inputs for your use case or facade. Repeatable tests, test context is clear - you're testing a specific scenario within your unit's boundary.
  • Expected values from fixtures too. Inputs data-driven, outputs data-driven. Fixtures for expected values too. Makes test intent super clear, less chance of wrong expectations in your testing boundary. Tweak fixture data, tweak scenarios, different outcomes for your unit.
  • Assert expected == actual. End of the line, data vs data. Assertions are readable, laser-focused on the behavior of the use case or algo facade inside its boundary.

This structured thing, fixtures, Ports & Adapters focusing on use cases and facades as your testing boundaries – big wins:

  • Predictable & Readable Tests: Same structure = less brainpower needed. Anyone can get what a test is doing, testing a use case or facade. Fixtures, if named well, are living docs for your unit's behavior within its testing boundary.
  • Maintainable Tests: Data-driven, decoupled via test doubles and Ports & Adapters domain separation = refactoring becomes way less scary for use cases and algos behind facades. Code changes in your core? Tests less likely to break, as long as data contracts for your units at their boundaries are good.
  • Focus on Behavior: Data & fixtures = testing behavior of use cases and facades, not implementation details. Textbook unit testing & TDD, especially with Ports & Adapters, test different levels clearly as separate units.
  • Deeper Understanding: Good fixtures, data-driven tests for use cases and algorithm facades... forces you to really understand the requirements, the domain, inside those boundaries. You're basically writing down your understanding of how the system should act in a precise, runnable form for each unit.

Yeah, setting this up - fixtures, data-driven TDD, Ports & Adapters with use cases & facades as units - takes upfront work, no lie. But for long-term test quality, maintainability, everyone on the same page? Totally worth it, especially in complex finance and e-commerce. Clarity, robustness, testability across the whole system – crucial.

4

u/CabinDevelopment Mar 10 '25

Wow, your insight in this chain of comments has been a pleasure to read. I screenshotted every comment you made in this thread, and I never do that. Thanks for the good information.

Testing is an art and I’d imagine in the financial sector your skills are in high demand.

3

u/Mithrandir2k16 Mar 10 '25

You should write a book or series of blog posts. The way you concisely and understandably explained a lot of difficult to grasp things about TDD here is pretty impressive.

3

u/flavius-as Mar 10 '25

I have! The young and restless from reddit downvote great ideas into oblivion if it points to, say, my LinkedIn profile or my website.

2

u/Mithrandir2k16 Mar 10 '25

I wouldn't mind a link to your blog :)

2

u/flavius-as Mar 10 '25

Done. See my about link

→ More replies (2)

2

u/Aer93 Mar 10 '25

Definitely agreed! I was looking for some debate but I was not expecting someone with so much insight in the topic

2

u/violated_dog Mar 18 '25 edited Mar 18 '25

Ports and adapters is a pattern we are looking at refactoring towards. However, most articles we find only skim the surface with simple use cases. I’ve read and re-read Alastair’s original article on the pattern and while he mentions that there are no defined number of Ports you should implement, he typically only sees 2, 3 or 4.

This seems to oppose most other articles that have a Port per entity or DB table. Products, Orders, Customers, etc all end up with their own Repository Secondary Port. In practice, this would expand greatly in a more complicated scenario with hundreds of tables and therefore hundreds of Ports. You could collapse them into a single interface but that seems like a very large surface area goes against clean coding principles. Should a Secondary Port reflect all the related functionality a single Use Case requires (eg all DB queries across all tables used I. The use case), or all the related functionality an entire Application requires from an adapter across all Use Cases, or something else? This could come from my confusion around what an “Application” is and where its boundaries are.

So you have any thoughts around this? How many Ports do the systems you maintain have? It is reasonable to have one per table or entity?

Additionally, how you do define your Application. As eluded to above, I’m not clear on what an”Application” is in this pattern. Some articles reference an Application or “hexagon” per Use Case, while others define an Application that has multiple Use Cases and encapsulates all the behaviour your application exposes.

That latter seems more intuitive to me, but I’m not sure. Any thoughts on this? Would there be any flags or indicators that you might want to split your Application so you can reduce the number of Ports, and have your Applications communicate together? Would an Application reflect a Bounded Context from DDD or would you still keep multiple contexts within a single Application structure but use modules to isolate contexts from one another, integrating through the defined Primary Ports in each module.

I would appreciate any insights you might have on this. It could be a case of Implement it and see, but that could be expensive if we end up structuring things incorrectly up front.

2

u/flavius-as Mar 18 '25 edited Mar 18 '25

Glad you asked!

Most people are bastardizing whatever original authors say.

At the same time, authors are forced to synthesize their explanations in order to get 1 or 2 points across (say: per chapter). You would do the same because you don't have the time to write 12k pages like it's intel manuals. But people don't usually read carefully or engage with authors directly, they'd rather use proxies: like we are about to do.

So rambling off.

  1. Buy Alistair's book. It's a leaflet because it's such a simple and elegant architectural style.
  2. I don't like his terminology, but "Application" is for Alistair the domain model (95% certainty)
  3. A port is an interface or a collection of interfaces. You have some leeway to split, but fundamentally you should have a single port called Storage. That's basically all repository interfaces
  4. In the storage adapter, you implement all those interfaces
  5. In the test storage adapter: you implement test doubles for those interfaces. Side note: people who say that "when have ever your applications needed to change database" are... limited; a code base always has two database implementations: a productive one and one made of test doubles for testing
  6. See the prose:

Architectural styles like P&A are not meant to be mutually exclusive. They are mental toolboxes. From these mental toolboxes you pick the tools you need to craft your architecture for the specific requirements of the project at hand.

I default to a mixture of:

  • P&A
  • DDD
  • onion

MVC is usually an implementation detail of the web adapter. Nevertheless architecturally relevant (especially for clarifications during architectural discussions).

There are also various views of architecture: the physical view, deployment view, logical view, etc.

In my logical view, all use cases jointly form the outer layer of the domain model (I like this term more than "Application"). The same outer layer also contains other elements like value objects or pure fabrications like repository interfaces.

You might have another architectural structure in there like

  • vertical slices
  • bounded contexts

These are synonyms in my default go-to combination of styles and when that it the case, I call that a modulith (modular monolith) because in the logical view, each of those are like a microservice. Extracting one vertical slice and turning it into a microservice (for "scale") is an almost mechanical and risk free process.

If anything, a vertical slice / bounded context / microservice is in itself a hexagon.

What I just described is IMO the right balance of minimalistic design and future extensability. Making this structure requires about 1 click per element, because I'm not saying anything complicated: a directory here, a package there, a compilation unit somewhere else... all light and easy.

The single elephant in the room left is DDD. How is THAT light you might ask.

For me, DDD is the strategic patterns when we're talking about architecture. The tactical patterns are design, they're implementation details - mostly.

So the "only" thing I absolutely need to do to get DDD rolling is developing the ubiquitous language - that's it. If necessary, at some point I can introduce bounded contexts, but I like doing that rather mechanically: did I mention use cases? Well I just draw a big use case diagram and run a layout algorithm on it to quickly identify clusters of use cases. Those fall most likely within the same boundary. Sure, for 100-200 use cases you might need 1-2 weeks to untangle them, but traceability matrices in tools like Sparx EA help. The point is: it's a risk-free and mechanical process.

I hope this is enough information for you to start sailing in the right direction.

Good luck!

→ More replies (2)
→ More replies (5)

2

u/ByteMender Mar 10 '25

I was going to say something like this, but you said it so well that now I’m just standing here, nodding like an NPC in a tutorial level. Spot on, especially about mocks and the real meaning of 'unit' in TDD!

→ More replies (2)

2

u/SobekRe Mar 13 '25

I have been practicing TDD for almost 20 years and have nothing to add to this.

Well, maybe. The saddest thing I hear at stand up is “almost done, just finishing up my tests”.

→ More replies (1)

2

u/bluemage-loves-tacos Oct 17 '25

red flag: if you have to change tests when you change implementation details, you have a wrong definition of unit and a wrong design and architecture due to that

This is really, really important. For most of my career, when TDD has been used or talked about, it's not been focused on behaviour, but code. It's only more recently that the discussion of WHAT the test is for has really come up, and it's really changed my perspective. TDD has gone from kind of a chore, to a useful tool. I can start with a test, and that test should fail, because when I post to https://www.example.com/api/foo/bar, it's a 404. I don't have anything there. But the test will try and get that page, and it will check to see if all of the actions that should happen when I go there are triggered.

That means my next goal is to get that resource to exist, to accept a post, and to do something useful. When my test passes, I iterate and make the test expect the view do more things, and it fails again, because I've not implemented it yet.

And when I want to change the underlying architecture, the test doesn't change. The test tells me if I screwed up, or that I didn't.

TDD to help create outcomes, not implementations, is by far a more productive use IMO.

2

u/vocumsineratio Mar 10 '25

I call them "unit tests", but they don't match the accepted definition of unit tests very well -- Kent Beck

From my perspective - all of the confusion around "unit" testing is an own goal on the part of Beck et al.

It certainly doesn't help that, at the time, it was still unclear what (if any) useful meaning "unit testing" should have in an OO world (see Binder, 1999), but they should have done better.

For a time, there was an effort to inject "programmer test" into the discourse, but too little to late. Still later, "microtest" appeared, but it similarly hasn't obtained significant market share.

1

u/Aer93 Mar 10 '25

What's the definition of "unit" that you have arrived at after your experience?

4

u/flavius-as Mar 10 '25 edited Mar 10 '25
  • use case
  • and generally boundary elements
  • facade to a subsystem

I've described in detail in another reply.

→ More replies (1)

1

u/NonchalantFossa Mar 10 '25

I agree with pretty much everything except that small mocks can be used quite liberally if they don't implement behavior imo. For example, I have an object that doesn't have a setter for some fields, the fields shouldn't change once the setup is done.

For tests, I don't want to care about how the setup is done or how to create the chain of events that'll lead to a specific object. I just create a small mock object, give it the specific combination of attributes I need and we're on.

4

u/flavius-as Mar 10 '25

Congratulations, you've just described another double - a dummy - if I understood you correctly. It's certainly not a mock, it's one of the other doubles.

You might think you don't agree 100%, but in fact you are.

→ More replies (5)
→ More replies (8)