|
Helping ordinary people create extraordinary websites! |
TestNG Makes Java Unit Testing a BreezeBy Filippo Diotalevi2005-03-22
TestNG quickstart A TestNG test class is a plain old Java object; you don't need to extend any special class or use any naming convention for test methods: You just use the annotation @Test to signal to the framework the methods of the class that are tests. Listing 1 illustrates one of the simplest tests possible for the utility class StringUtils. It tests two methods of StringUtils: isEmpty(), which checks a String for emptiness, and trim(), which removes control characters from both ends of a String. Note that the assert Java instruction is used to check error conditions. Listing 1. A test case for class StringUtils package tests; Before you can run the tests, however, you must configure TestNG using a special XML file, conventionally named testng.xml. The syntax for this file is very simple, and is presented in Listing 2. This file begins by defining a test suite, My test suite, composed of a unique test, First test, that is made by the StringUtilsTest class. Listing 2. Configuration file for TestNG <!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" > If this sample testng.xml file doesn't seem very useful (there's only one test class), the great news is that it is actually the only file you need to write to define your test suites. Remember the old days of JUnit? In those days, the definition of your suites was probably spread over several files: JUnit's TestSuites, property files, and, obviously, Ant build files. With TestNG, all the data you need is gathered in the testng.xml file. No more TestSuites, and a thinner build file. To run the test, you compile the class with javac and then invoke TestNG with the following command: java -ea -classpath .;testng.jar;commons-lang-2.0.jar com.beust.testng.TestNG testng.xml Here, the option -ea tells the JVM to handle assertions (and to raise an exception when an assertion fails); testng.jar and commons-lang-2.0.jar are the only two libraries required to run this example, and com.beust.testng.TestNG is the main class of TestNG. For all you lazy developers who have happily forgotten the cryptic syntax of java and javac, a useful Ant task is also available. Listing 3 presents, as an example, the Ant build file of the sample application distributed with this article. Note the definition of the testng task associated with the class com.beust.testng.TestNGAntTask, and its rather simple usage in the test target. Listing 3. Ant build file with TestNG task <project name="sample" default="test" basedir="."> If all has been done correctly, you should see the results of your tests in the console. Furthermore, TestNG creates a very nice HTML report in a folder called test-output that is automatically created in the current directory. If you open it and load index.html, you will see a page similar to the one in Figure 1. Figure 1. HTML report created by TestNG
Tutorial Pages: » Try this testing framework for its advances over JUnit » About the code » TestNG quickstart » Defining test groups » Configuration methods » Exception checking » Wrapping up » Resources First Published on IBM DeveloperWorks |
|