Software QA FYI - SQAFYI

Test driven development

By: robertp



Test driven development is an important and valued part of agile practices. In test driven development (in short TDD) developers first write tests for new functionality (e.g. a class) and later they implement it. The order is crucial and I will explain later why.

Let’s assume that the developer needs to implement new class PathCalculator which calculates distance between 2 points. The developer has a general idea about the class, that he wants to implement, and could start implementation of it method by method. If he is smart and the problem is not very complicated, he will do it without big problem. Then the class can be integrated into the existing application structure and the new version of the system can be tested. This was easy but many issues can arise if the problem is more complicated:

the developer may have a vague idea about how this class should be accessed from outside (not to mention how to implement it!)

after the code for the class is finished, the developer could find out that one or more important methods are missing or the existing methods miss parameters (or something else) to integrate it successfully into existing application structure after integration, the new functionality misbehaves or even breaks other functionality for some unknown and hard to trace reason

All of these problems could have be more-or-less avoided (actually TDD is not a silver bullet which solves all problems) if TDD was used. Basics of TDD

In TDD we first write one or more tests for the new class to find out the how it should be accessed from outside and how it should behave in certain situations. We should specify the usage in typical situation and in certain border cases. Of course, it is impossible to catch all such cases but we should at least try. Let’s look at the implementation of it using Java and JUnit.

package com.example;

import org.junit.Test;
import static org.junit.Assert.*;

public class PathCalculatorTestCase {

private static final double DELTA = 0.001;

@Test
public void test1() {
PathCalculator pathCalc = new

PathCalculator(PathCalculator.Type.EUCLIDAN);
double distance;





distance = pathCalc.getDistance(1, 1, 6, 7);

assertEquals(distance, Math.sqrt(5 * 5 + 6 * 6), DELTA);



distance = pathCalc.getDistance(1, 1, 1, 2);
assertEquals(distance, 1.0, DELTA);

}

@Test
public void test2() {
PathCalculator pathCalc = new
PathCalculator(PathCalculator.Type.EUCLIDAN);
double distance = pathCalc.getDistance(7, 8, 7, 8);
assertEquals(distance, 0, DELTA);
}

}


At the moment our code does not compile at all because we are missing class PathCalculator. The most important thing is that we defined how the new class should behave and we decided on its interface. Let’s add a stub for PathCalculator class with empty methods:

package com.example;
public class PathCalculator {
enum Type {
EUCLIDAN,
MANHATTAN
}

public PathCalculator(Type type) {
// TODO:
}

public double getDistance(int x1, int y1, int x2, int y2) {
// TODO:
return 0;
}


}

Running tests with Maven

In order to run the tests just create own project directory, put PathCalculatorTestCase class in project directory under src/test/java/com/example/PathCalculatorTestCase.java, put PathCalculator class in project directory under src/main/java/com/example/PathCalculator.java and finally add pom.xml file in the project directory with following contents:

Full article...


Other Resource

... to read more articles, visit http://sqa.fyicenter.com/art/

Test driven development