next up previous
Next: 1.8.2 Tests Should be Up: 1.8 Unit Testing in Previous: 1.8 Unit Testing in

1.8.1 Tests Should Check Themselves

First and foremost, the tests we write should be capable of checking themselves. Consider the following example of a testing method:

public void testCityEntryToString() {
  System.out.println(new CityEntry(...).toString());
}

This test checks the result of calling toString() on a CityEntry by printing the result to the console. The programmer must check this output and verify that it matches the intended result. This is a waste of programmer time and effort; it would be much more efficient to write test methods that checked themselves. For example, we could rewrite the above test as follows:

public void testCityEntryToString() {
  assertEquals("CityEntry[a,b,c]",
               new CityEntry("a","b","c").toString());
}
where we call an assertEquals method that compares the value of its arguments to ensure that they are equal. If they aren't equal, assertEquals throws an exception. This way, we know whenever we run this test that it passed unless we were notified with an error message.



Corky Cartwright 2002-08-09