When To Test Or Write Code First In Web Development

How to decide between Test-driven development and write code first

Published:
Last modified:

Overview

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:

  1. Write a test that fails
  2. Make the test pass changing the code, without breaking other tests
  3. 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:

Scenario#TDDCode first
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.

Rails Tutorial Book
Do not write test code for code that it is likely to change, like detailed HTML.

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.

References

Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus

    Articles

    • When To Test Or Write Code First In Web Development

Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.

Powered by SimpleIT Hugo Theme

·