Skip to content
Back to the blog
  • TDD
  • test driven development
  • red green refactor
  • testes unitários
  • qualidade de software
  • IA no desenvolvimento
  • vibe coding
  • mutation testing
  • clean code
  • workflow de desenvolvimento
  • AI code generation

My TDD Journey

From Chaos to Code That Works the First Time

I've lost count of the nights I spent debugging something a five-minute test would have caught. Every developer knows that feeling, but few admit it. My relationship with tests started like almost everyone's: write the code first, then fit tests in wherever I could. Until I found out the order could be flipped. In this post I explain how I went from a chaotic, reactive workflow to a process where the code works the first time: what made me try TDD, where I got stuck, the moment it clicked, and what I learned applying it day to day, including with AI.

How I Developed Before TDD

My workflow followed a pattern I saw everywhere: write all the code, refactor it for readability, and only then try to generate tests. The problem shows up as soon as you think about it. Testing finished code is much harder than it looks, because it goes through so many changes before the final version that mapping every scenario becomes guesswork.

And manual testing kept going, even after I wrote the automated tests. In practice, I had two validation flows running in parallel, and neither gave me confidence.

AI made the problem more visible

As generative AI for code became popular, this got even more obvious. The AI generated tests, but in a way that was far too permissive: it changed the test so it would pass against the existing code, instead of making the code adapt to the test. That created a false sense of safety, which only showed up when I ran mutation testing and saw mutants surviving all the time.

The cycle was always the same: code working on my machine, tests passing, and production blowing up. And me thinking the problem was me. That frustration is what pushed me to look for another way.

What Made Me Try TDD (and Where I Got Stuck)

The turning point came when I watched Akita talking about AI-assisted development. One idea of his stuck with me: vibe coding is bad, but AI with TDD becomes a game changer.

At first I pushed back. I only knew TDD by name. Writing a test before any code existed sounded absurd: how do you test something that doesn't exist?

But my frustration with bugs was bigger than my skepticism. So I started studying. I read about the Red-Green-Refactor cycle, looked at examples, and understood the theory. Even so, the first time I tried to apply it, I got stuck again.

The hardest part: granularity

What nobody explains clearly is how much scope to put into each TDD cycle. A whole screen? A micro function? A group of related functions?

I researched a lot and found nothing conclusive. The literature talks about "small units" but doesn't say what that means in practice. So I figured it out by testing:

  • Whole screens: didn't work. The scope was too big, the test was brittle, and any change broke everything.
  • Groups of functions: better, but I was still testing too many things at once.
  • Micro functions: this is where it clicked. Tests for specific things, like "try to delete a table without being an admin" or "try to list another user's data by using their ID directly". Small, focused, and independent.

This isn't necessarily the right answer for everyone. It's the one that worked for me, and it took me a while to get there. With granularity sorted out, I still had to run the cycle across an entire project.

Red, Green, Refactor: My First Cycles

My first TDD project was a personal one, built specifically to practice the methodology: an AI RPG Master using the Fate system. Here, what matters is what I learned.

The backend ended up with more than 500 unit tests, all following the TDD flow: test first, then the function. The test fails, the code passes, I refactor. And repeat.

Where AI fits into the cycle

My use of AI in this process isn't vibe coding. I've been using AI pragmatically for more than a year, with planning, a PRD, a deep understanding of the requirements, and specialized agents for code review, security, and accessibility. The AI executes what I think. I'm the one doing the thinking. I described this workflow in detail in my AI development workflow.

Learning TDD was no different. I built a TDD specialist agent that runs the Red-Green-Refactor cycle automatically. Each function is a new session with a clean context, because the functions need to be independent. I plan exactly how that function will work, run the cycle, and only then start wiring the functions together.

The cycle still doesn't feel natural

One important caveat: Red-Green-Refactor still doesn't feel natural to me. I'm still getting used to it, and I often feel slower than before. That perception changes over time, and what changed my mind was one specific test.

The Moment It All Clicked

When I finished the AI RPG Master backend, with its 500-plus unit tests, I still hadn't felt the value of it. I hadn't run the application. It was just a pile of tests passing in the terminal.

Then I ran an end-to-end test. It passed on the first try.

No refactoring afterwards, no finding a flaw here and another there, no adjusting along the way. That had never happened to me. I always found something to fix later, there was always a hidden bug waiting for the worst moment to show up.

That's when I realized it was worth it. And not just because it worked the first time, but because of the confidence it creates: knowing the code is covered, that every scenario was thought through before it existed, that the test is the anchor and the code is what adapts to it.

That's the memory I go back to when the cycle feels slow and the friction shows up. It also explains why, for me, TDD and AI go together.

TDD with AI: Why Vibe Coding Doesn't Work

The problem with vibe coding isn't AI. It's the lack of validation. When you ask AI for code without tests defining what's correct, you're trusting that the model understood your intent. And that rarely happens consistently.

I've seen AI change the same test over and over until it passed. And I saw the weakness when I ran mutation testing: mutants survived because the code wasn't well built, and the AI adjusted the test to fit the wrong code. When you flip that logic, the picture changes:

"Now I have a correct test that defines the expected behavior. If the AI-generated code doesn't pass the test, it's the code that has to change. Not the test."

How I keep AI in check

For this to work, I have to be strict with the agents. Each agent has a clear list of what it can and cannot do: "don't do this under any circumstances", "you only do this and nothing else". Without that discipline, AI starts generating random code and the TDD cycle loses its purpose.

Out of curiosity, I've looked into applications built with vibe coding and gained full access to more than one. That isn't an exception, it's the consequence of having no validation. A junior developer who understands the logic behind the code is already a step ahead. But it's TDD with AI that separates people who use the tool safely from people who just keep asking it for things.

After months of working this way, a few lessons became clear.

What I Learned About TDD in Practice

I've been using TDD for months on new projects, both personal and for clients. These are the four things that changed my view the most.

The code gets leaner

This surprised me. The green phase, where you write the minimum code needed for the test to pass, naturally prevents over-engineering. Since there's already a test stating what I expect, I write just enough for that scenario. The result is smaller, more direct functions with high quality. Code that does exactly what it needs to, no more and no less.

Feeling slower at first is normal

At first, TDD feels slower. That's common: you're learning a new methodology, breaking old habits, and thinking differently.

My advice is to document the process. Write down how many refactors you used to do, how long it took to implement a feature, and how many bugs showed up later. Then compare with your numbers using TDD. With data in hand, it becomes clear that you ended up with less work, even if it initially felt like more effort.

Industry studies point the same way. In a study of three teams at Microsoft and one at IBM, researchers from Microsoft Research, IBM, and NC State saw pre-release defect density drop by 40% to 90% compared with similar projects that didn't use TDD. In exchange, the teams reported a 15% to 35% increase in initial development time. In my experience, that time comes back with interest in the debugging you no longer have to do.

TDD isn't for everyone, and that's fine

This is an unpopular opinion, but I believe it: TDD shouldn't be mandatory. It's a personal choice.

There are several methodologies: TDD, Spec Driven Development, and others. Each person should use the one that works best for them, as long as the result meets the team's standards. Forcing someone to follow a methodology limits their potential.

What I do advocate is that everyone try different methodologies. I came from Spec Driven Development, and it worked well, but I felt stuck. When I tried TDD, I realized it was the best fit for the way I work. The only way to know what works for you is to experiment.

In legacy systems, little by little

I get this question a lot. The answer is: gradually. I didn't set out to apply TDD to the company's legacy systems. But when a small task comes in that fits well, I apply TDD to it and make sure that specific part works.

Over time, that builds confidence to change existing code, because you know the tests protect the expected behavior. That's what makes me consider moving from the manual approach to TDD more broadly.

Conclusion

My TDD journey wasn't linear. I started skeptical, got stuck on granularity, felt slower, and then watched an end-to-end test pass on the first try. What TDD gave me wasn't just code that works. It was confidence to refactor, safety to change things without fear, and leaner code, because the methodology itself keeps me from building more than I need.

If you want to start, the path is simple:

  • Pick a small project, preferably a personal one.
  • Write a single test before any code and watch it fail.
  • Write the minimum code to make it pass, refactor, and repeat.
  • Document the process and compare it with your previous workflow.

Don't expect it to feel natural or fast at first. Compare the numbers and see for yourself whether it makes a difference.

References

Matheus Mierzwa

AI Enablement Lead & Frontend Tech Lead

I write about what I build and lead in practice.