Improving Unit Testing with SmartInspect
Introduction
Extending Unit Tests
Generating Log Reports
Conclusion
Introduction
This article uses the JUnit unit testing framework and the SmartInspect Java library to demonstrate the generating of log reports in unit tests. A slightly modified example of the original JUnit distribution is used to show the benefits of using SmartInspect in this way. Although this article and the related classes are specific to Java, the general concepts should also be adaptable to other unit testing frameworks.
For the purpose of this article I wrote a custom junit.framework.TestCase
class which is called SmartInspectTestCase. This class handles the
creation of the necessary log objects and automates the logging of the
beginning, ending and occurred exceptions of tests. In order to make use of
these features, test cases need to inherit from this class. The required
JUnit extension classes used in this
article and the following example can be downloaded here.
Extending Unit Tests
I demonstrate the generating of log reports with the well-known
junit.samples.SimpleTest example included in the
Junit distribution. The basic
concept of JUnit is to have
a set of tests bundled in a so called test case. A
test case is represented by a class which inherits from
junit.framework.TestCase and the tests are normal methods
which begin with the prefix "test".
public class SimpleTest extends TestCase
{
public void testAdd() {
double result = 2 + 3;
assertTrue("Addition failed", result == 6);
}
public void testDivisionByZero() {
int zero = 0;
int result = 8 / zero;
}
...
public static void main(String[] args) {
junit.textui.TestRunner.run(SimpleTest.class);
}
}
As you can see, it couldn't be much simpler. The junit.textui.TestRunner
extracts all valid tests from the SimpleTest class and executes them
accordingly. In order to use SmartInspect now to generate log reports of your
unit tests you just need to apply the following simple modifications:
public class SimpleTest extends SmartInspectTestCase
{
public void testAdd() {
double result = 2 + 3;
assertTrue("Addition failed", result == 6);
}
public void testDivisionByZero() {
int zero = 0;
getSession().logWarning("Checking division by zero");
int result = 8 / zero;
}
...
public static void main(String[] args) {
junit.textui.TestRunner.run(SimpleTest.class);
}
}
As you can see, the most important modification of the original example
is the change of the super class. Instead of deriving directly from the
junit.framework.TestCase class, this piece of code uses my
SmartInspectTestCase class. This enables the tests to generate
detailed log reports, which leads us to the next chapter.
Generating Log Reports
When executed, the example above generates a detailed log report.
This report automatically contains the beginning, ending and occurred
exceptions of every test. In addition to these information, you
can also add log statements by yourself to improve the log even
further (as shown in the testDivisionByZero method).
SmartInspect used for generating log reports in unit tests
(larger image)
As you can see in the screenshot above, most properties of the log
objects in the SmartInspectTestCase class will be adjusted to
the current execution context. The name of the underlying session is
automatically synchronized with the name of the current test, for
example. Among other things, this leads to easy filtering in the
SmartInspect Console. You can now, for instance, display the logging
information of the tests independently from each other (as shown in
the screenshot).
Conclusion
After reading this article you should now have an overview over
how to improve unit tests with SmartInspect. Just by deriving from
a different java.framework.TestCase class, normal
JUnit unit tests are now able to
generate detailed log reports. If you have any further questions about
this topic do not hesitate to contact me at
tg@gurock.com.
UnitTesting.zip (2 KB)