Monday, July 19, 2010

Unit Tests - "Written Once and Forgotten Forever"

Unit test cases that are written once and forgotten for ever, with all the dataset/environmental dependencies in it. What is the importance of dataset/environment? Let's take an example, observe the test case below.

public void testEmpFinder () {
//Weird Test case for Fun!!!
String result = "JOHN";
//Passing employee id returns employee object.
//verify the name matches.

Employee emp = EmpFinder.find(1);
assertEquals(emp.getName,result);
}

What's wrong? The developer had made an assumption, that on querying with employeeid='1' will return employee with the name "JOHN". The data could be coming from a database table "EMPLOYEE". But It’s very evident this test case would fail if run on an environment where the employeeid='1' data doesn't exists. This makes the test cases obsolete the moment they are written.

No comments:

Post a Comment