GrandTestAuto in Two Minutes
Writing and running tests with GrandTestAuto (GTA) is very easy. Let's start with an example. Suppose that we are
developing a package pack. The first class to be written is A:
package pack;
public class A {
public A( String str ) {
//Constructor body ...
}
public String a() {
//Method body ...
}
public String b( String x ) {
//Method body ...
}
}
The unit test for this class needs to be in the package pack.test and must be
called ATest:
package pack.test;
public class ATest {
//Class body..
}
We need to have a test method for each of the methods in A and also for the
constructor:
package pack.test;
public class ATest {
public boolean aTest() {
//Test method body ...
}
public boolean bTest() {
//Test method body ...
}
public boolean constructorTest() {
//Test method body ...
}
}
In order to get GTA to run this test, we must add a new class to pack.test. For
now, let's just regard
this as a marker class. The class is extremely simple:
package pack.test;
import org.GrandTestAuto.*;
public class UnitTester extends CoverageUnitTester {
public UnitTester( GrandTestAuto gta ) {
super( gta );
}
}
Before we run the tests, we need to create a settings file that tells GTA where the classes are and how we
want the test results presented (in a file or just to the console). The following is typical of the contents of this
file:
CLASSES_ROOT=E:\\project\\classes
LOG_TO_CONSOLE=true
LOG_TO_FILE=true
If this file is called "Settings.txt", then the command to invoke GTA would be something like:
java -cp E:\project\classes;gta.jar; org.grandtestauto.GrandTestAuto Settings.txt
Running this would cause some logging statements to be printed to the console and more readable messages printed to
a file called "GTAResults.txt" (the name is configurable using the settings file). Assuming that the tests passed,
the file output would be something like:
ATest.aTest passed
ATest.bTest passed
ATest.constructorTest passed
>>>> Results of Unit Tests for pack: passed. <<<<
******* Overall Unit Test Result: passed. *******
The points to note so far are:
- the test classes are in a sub-package of that being tested
- there is a naming pattern for the test classes: "Test" is appended to the name of the class under test
- the test classes must have a public no-arguments constructor
- the test methods are public, return a boolean, and take no parameters
- there is be a class called UnitTester in the test package and it extends
grandtestauto.org.CoverageUnitTester
- GTA takes its operational parameters from a settings file and the name of this is passed as the single
argument to the main method.
Continuing with the example, suppose that we add a new public method
c to
A.
If we run our tests again, the result will be failure and a message that there is an untested method will be
printed. Suppose that
we add the test for
c and create a new class
B:
package pack;
public class B extends A {
public B( String str ) {
//Constructor body ...
}
public String a() {
//Method body ...
}
public String d( String x ) {
//Method body ...
}
}
If we invoke GTA at this point, our tests will fail because there is no test class for
B. The unit test
for
B must test precisely the methods of
B that
are defined in the
package of
B and are not already tested. That is, we must test the over-ridden
version of
a and also the new method
b. We do not need to
test the methods
inherited from
A (as these are tested in
ATest) or
those inherited from
java.lang.Object (as these are defined in another package, which we can assume is
either well tested by us,
or is beyond our control).
The use of
CoverageUnitTester is forcing us to be very thorough about our testing.
Points to note are:
- we must provide a test method for every accessible method in a public class
- we must provide a test class for every public class
- all the test methods in all the test classes get run automatically by GTA
- we do not need to write tests for methods that are tested by other test classes within the same test
package
- we do not need to write tests for methods that are inherited from outside the package under test.
This is enough information to get started with GTA. In the advanced tutorial we will see that protected methods must
and can be tested,
how to test multiple methods with the same name, and more on inherited methods.