Thursday, January 28, 2010

Unit Tests vs Integration Tests

Let's make it quick and clear :

If a test is not executed on a regular basis, through the build process or continuous integration it is therefore not maintained and becomes a useless test. Maven users would recognize the over-used -DskipTests=true. You can delete it right away, because chances are that within no time it will fail.

Why would you not want to execute the test all the time ? Simply because what was thought to be a unit test is clearly not.

Then how to determine if a test is a Unit Test or an Integration Test ?

  • If a test needs data in the Database , it is an integration test
  • If a test needs a Spring configuration, it is an integration test
  • If a test needs a server to be up (Jetty or application server) it is an integration test
  • If a test is slow it is probably not a unit test.
  • ...
well you get the idea.

A unit test is testing one and only one class.

It does not mean integration test are bad, but they need to be executed in a different cycle from unit test: for example once the build is deployed on a server, etc...