Making Good Software

A blog by Alberto G (Alberto Gutierrez)

Written by Alberto Gutierrez

August 5th, 2009 at 5:13 pm

How to write good tests. Top 5 considerations to build software without defects.

with 8 comments

Testing was considered, (and still is considered in some companies),  the last stage of software development, developers would hope that they will build from scratch their applications without errors, so the testing phase would be just a formality where only minor errors would be expected, thankfully we are moving from that utopian parading to a new concept where testing is an integrated part in the development process. What follows is a few “best practices” to take the most from this new test paradigm.

1.- Test code is as important as production code.

One of the most common reasons to fail for teams starting TDD is that their test code is not clean, so it will eventually cost more to maintain the tests than the actual production code, to avoid this scenario, it is important to keep in mind that having clean test code is as important as having clean production code.

Reuse fixtures. If you need some data to be available for different tests, make it reusable and make it available in a single place.

Write you own asserts. If you are testing some data in 5 different scenarios, and to prove that everything works fine for each of the scenarios you need 5 different asserts, write your own assert that performs those other 5 ones and call it from your code instead of writing 25 different asserts.

In general, use the same principles you apply to write good production code: In this other post you can find some more directions to write good code: 10 commandments for creating good code.

For further reference, I highly recommend you to read the following book: xUnit Test Patterns: Refactoring Test Code

2.- Test as much as possible, test as soon as possible, test as often as possible.

As I said on my previous post, I am a convinced TDD developer, but maybe you are not, what is important is that no matter if you write the test before or after, you don’t go forward with new code until you have a test that proves that the code you just implemented works as expected. This attitude of testing as much as possible, as soon as possible and as often as possible will prevent you from entering new bugs into the system, and for this reason it will also make you more confident and productive.

3.- Test at different levels.

Don’t try to cover your application with tests that only cover both the UI and the backend, they take too long to write, are hard to maintain and slow to execute, it is also very difficult to test all the different elements of your application if you don’t separate them, the best approach is to test at different levels. Consider having at least the following set of tests:

Unit tests. Tests that check all your components by themselves, without taking into consideration their interaction with other components.

Acceptance tests. Specified by the product owner, they represent the minimum functionality that  the application has to deliver to be accepted from a business perspective.

Integration tests. These tests are not identified by the product owner in his/her acceptance tests but are developed either by QA or development to compliment them.

UI Tests. UI testing is slow, also the UI is likely to be changing during development and so do its respective tests, by keeping the UI tests separated from the backend tests we won’t be affecting them every time the UI changes.

4.- Testing as an integrated part of the development process. Continuous integration

Testing has to be an integrated part of the development process, something is not done until is tested, forget about having a different stage where someone else takes care about the testing, everyone is responsible and accountable for testing, from development to QA to the product owner. Ideally you will also apply continuous integration, which goes further of the testing scope, for more information on continuous integration check this excellent article from Martin Fowler.

5.- Use testing tools and frameworks.

Luckily there is a set of tools and frameworks developed lately to help us testing our applications, a selection of these tools are:

Fitnesse. A tool to create acceptance tests,  non technical people can write their own test cases.

xUnit. Framework to develop unit tests, integrated with the most popular IDEs and programming languages.

CodeCover. A code coverage plugin for Eclipse, useful to know what parts of the code we are testing, and what parts we are not.

Jira. A reporting and tracking tool, mostly use to track bugs.

Mercury. Popular testing suite, includes different tools for different types of testing, including UI testing.

Selenium. UI testing tool for browser applications.

Hudson. Continuous integration tool, used to ensure that the code is always ready to build and that it passes all the different tests.

8 Responses to 'How to write good tests. Top 5 considerations to build software without defects.'

Subscribe to comments with RSS or TrackBack to 'How to write good tests. Top 5 considerations to build software without defects.'.

  1. Test early, test often, test automatically! Good post!


    6 Aug 09 at 6:41 am

  2. […] How to write good tests. Top 5 considerations to build software without defects. Naja, without defects ist ein bisschen überzogen, aber trotzdem guter Artikel. […]

  3. […] How to write good tests. Top 5 considerations to build software without defects. | Making good softw…. in Coding. 0 Comments Feed for this Entry Trackback […]

  4. […] artículo How to write good tests nos deja 5 consejos para aprovechar al máximo este nuevo […]

  5. Hello Alberto, I’ve translated your excellent post : Comment écrire de bons tests. Top 5 pour construire des logiciels sans défaut., Regards, Fabrice.

    Fabrice Aimetti

    11 Aug 09 at 3:08 am

  6. […] 1)  How to Write Good Tests […]

  7. […] la página web Making Good Software nos dan 5 consejos para construir software sin […]

  8. […] páxina web Making Good Software dannos 5 consellos para construír software sen […]

Leave a Reply