wiki:UnitTestsJunitBackground

Unit Tests and JUnit - Background

Introduction

This note describes some aspects of unit tests in general, and JUnit in particular, that will be considered when deciding a test environment for the Proteios 2 project.

Unit Tests

Unit tests are used to test a distinct "unit of work", which in Java often is a single method. The tests are expected to observe the following behaviour:

  • Each unit test should run independently of all other unit tests.
  • Errors should be detected and reported test by test.

A unit test typically calls the method to be tested using known input data, and checks the method's response against the desired output, whereupon the unit test returns a simple pass/fail response.

Due to the simple nature of unit tests, there tend to be a large number of them if a program should be tested thoroughly. It is therefore common to use a special framework to perform the unit tests. Such a framework is normally desired to fulfill the following requirements:

  • It should be easy to define which unit tests will run.
  • Ideally it should be possible to automate running of unit tests.

The Value of Unit Tests

Major software projects normally use a code revision system for managing the main code during development. This allows the individual developers to ensure that changes they have made to the code are compatible with the main code base, before the changes are committed to the latter. A standard minimal requirement for this compatibility check is that the code compiles without errors. Running unit tests successfully to check compatibility before code changes are committed to the main code base, adds further confidence that the latter is in a good state of health. Bugs found early in development are easier to track, and therefore easier to fix. However, unit tests are not a replacement for functional or integration testing, where the collaboration of software components are also investigated.

JUnit

Following the design of a framework called SUnit for unit tests for Smalltalk developed by Kent Beck, he and Erich Gamma in 1997 created a similar framework JUnit for Java. JUnit allows the running of unit tests to be automated to a high degree, making it possible to run a full set of tests after changes have been made to the source code. This is accomplished by use of Java's ability for reflection and introspection. Specifically, all methods for unit tests are assumed to have names of the type "testXXX", where "XXX" is the name of the method that is tested (capitalization standards in method names are normally followed, e.g. method "testAddHandler()" is the unit test for method "addHandler(...)"). JUnit supplies a number of useful methods for checking the expected output with the actual, possibly allowing for a specified margin of error. Test reports are standardized, and both GUI- and text- based versions of the test runner environment exist.

The Case for JUnit

If you desire to use unit tests in your Java project, it is assumed that some sort of testing framework is needed to automate the tests. Is JUnit the framework of choice? Alternatives exist, but an inspection of e.g.  http://opensourcetesting.org/unit_java.php shows that JUnit has very strong support (a lot of the tools available for Java unit tests were JUnit extensions). Eclipse has integrated support for JUnit, not just for running the unit tests, but also for creating the test methods. This fact and that the framework has a large user base that has collected experience concerning its use for a number of years ("official" Java was released in 1995, only two years before JUnit), talks in its favor.

References

  1. Information regarding unit test goals and JUnit has been taken from "JUnit in Action" by Vincent Massol with Ted Husted (2004), Manning Publications Co., ISBN: 1-930110-99-5.
  2. JUnit home page can be found at  http://junit.org.