All articles
Software Development2 min read

16. Write Tests For The Code You Want, Not The Code You Have

AI writes tests around how your code works today — baking in existing bugs. Test for the behavior you want instead, and let TDD safeguard your code's future.

Delft-blue tile reading "Write Tests For The Code You Want, Not The Code You Have"

For large projects that will be in production for a long time, tests are essential. You want to be sure that an update doesn't accidentally break something. Manual testing quickly becomes too time-consuming, while automated tests can provide feedback in seconds.

Still, writing tests isn't the most exciting work for most developers. Many (myself included) use AI to speed things up. It's quick and efficient — but there's a catch. AI typically writes tests based on how your code works right now. That means existing bugs are simply baked into the tests. Useful for regression, but not enough to guarantee quality.

A better approach is to write tests for how you want the code to behave. Define the expected behavior ("if I put in A, I should get B out"), and test not only the happy path but especially the exceptions and edge cases. Well-written tests aren't just a safety net — they're also a form of living documentation. They make it clear to everyone how the code is intended to work.

One step further is Test Driven Development (TDD): write the tests first, including all exceptions and edge cases, and only then implement the code. This way, your application is built directly around the right expectations. TDD is powerful, but it's not a dogma — sometimes it's more effective to experiment first and add the right tests later.

Equally important is finding the right balance between test types. Unit tests are fast and great for edge cases, but integration and end-to-end tests provide more confidence in the full workflow. Don't overdo it: testing everything often leads to slow test suites and redundant checks. Focus on behavior and business rules, not trivial implementation details.

AI can still play a valuable role as a testing assistant: it's great for generating a first draft, but you as the developer decide whether the test really adds value. In the end, it's about writing tests that safeguard the future of your code, not just the present.

In my current project, which involves complex business logic with many edge cases, I've applied TDD extensively. It gives me the confidence that the code works correctly in all scenarios. I'll share a more detailed article about this project soon.

👉 How do you approach testing: AI-generated tests, hand-written tests, or TDD?

Related articles