Red Green Repeat Adventures of a Spec Driven Junkie

Test first - Write Code that is Easy to Test

When developing software, when writing the code first then tests afterwards, team members always come to me asking for help, specifically:

I am having a hard time creating tests for the code I wrote.

šŸ¤¦

Yes, this is a sign of a code first, test second style.

When writing code first, you will write the initial spark of code that leads you to the solution. You layer on more code as you add more manual test cases. You keep iterating until completion.

Then what?

You start writing tests for the solution you built up with manual tests.

The thing is: you wrote the code with manual tests. How can you ā€œautomateā€ manually designed tests??

With great effort.

It usually is better to just start all over again.

The problem with starting over again? Try forgetting the answer to a problem you just solved. Will you actually find a different way there?

Save yourself (and your manager!) headaches by just testing first. Code produced with tests first will always be easy to test.

The first challenge will be to:

  1. write the test
  2. writing code that is testable
  3. writing code to make the test pass

When you code first, test second - step two is always neglected and becomes a gulf between the code you produced and the tests you want to write for that code.

Thatā€™s why I insist on test first. At an organizational level, test first keeps your workflow simple, at a code level, by testing first your code will always be easy to test.