How to decide between Test-driven development and write code first
- Published: August 8, 2016
The optimal solution when testing a web application while developing is a mix between Test-driven development and write code first.
Test Driven Development code concept is to write tests before implementations:
- Write a test that fails
- Make the test pass changing the code, without breaking other tests
- Refactor the code, verify that tests pass
TDD methodology makes you write a lot of code for simple tasks and can often make you lost focus on what you are trying to develop.
A balanced approach is to use the TDD methodology in certain cases and write first code in others, or sometimes to skip tests at all, these are some hints on how to choose between them:
|Is the test very short or simple?|
|Don't knowing the desired behaviour yet?|
|Does it has something to do with the security model?|
|Fixing a bug?|
|Are you refactoring?|
Whenever a bug is found, write a test to reproduce it and protect against regressions, then write the application code to fix it.
In a MVC approach
For controllers and models write tests first (TDD), then integration tests.
For views that we know are constantly changing and are not so prone to errors, skip testing.
As always, it ultimately depends on each case, there aren’t solutions that can be always applied, it ends up being a mix between trial and error to see what gives you the best balance between being productive and generating that sense of confidence in your code that writing tests gives, a lot of practice is required to achieve a smooth integration of writing tests and code.