Summary
Everyone knows JUnit, the Java unit-testing framework. JUnit has some annoying specificities that make it unsuitable for complex unit testing involving grouping, asynchronous, and parallel testing. This article introduces TestNG, an alternative testing framework targeted at J2SE 5.0. (1,900 words; April 4, 2005)Page 1 of 2
Advertisement
estNG, written by Cedric Beust and Alexandru Popescu, is a light framework based on Java annotations (for J2SE 5.0) that allows you to design complex unit testing for J2SE 5.0 and J2SE 1.4. Why bother learning another unit-testing framework when you're already comfortable using JUnit? If you are interested in simplifying your unit-test cases, in leveraging J2SE 5.0 annotations to tag your test classes as well as being backward compatible with J2SE 1.4, in having out-of-the-box support for dependent methods and parallel and asynchronous testing, TestNG is the tool you are looking for.
This article starts with a description of the JUnit annoyances and introduces TestNG through numerous examples. A case study shows how to use TestNG for asynchronous testing.
JUnit annoyances
The JUnit testing framework has been around for a long time and is (hopefully) widely used by Java developers to unit test developed software. However, the framework has some annoying specificities, which the following sections describe.Multiple instantiations per TestCase
MultipleTestCaseinstantiations is a well-known issue. Thesetup()andteardown()methods are called before and after each test method; moreover, the test class that must extendTestCaseis also instantiated each time a test method is called. Although multipleTestCaseinstantiations might prove acceptable for simple test cases, what if you want to set up an object that is to be reused across more than one test method, for example, a Java Database Connectivity connection (or, for that matter, a Java Naming and Directory Interface (JNDI) context)?
PainOneTest.javaillustrates the problem:
public class PainOneTest extends TestCase {
/**
* PainOneTest
*/
public PainOneTest() {
System.out.println("PainOneTest");
}
/**
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
System.out.println("setUp()");
}
/**
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
System.out.println("tearDown()");
}
/**
* testMe
*/
public void testMe() {
System.out.println("testMe");
}
/**
* testYou
*/
public void testYou() {
System.out.println("testYou");
}
}This code produces the following output:
PainOneTest
PainOneTest
setUp()
testMe
tearDown()
setUp()
testYou
tearDown()The
TestSetupclass circumvents this issue, as illustrated below:
public class PainTwoTest extends TestCase {
/**
* Test
* @return
*/
public static Test suite() {
return new TestSetup(new TestSuite(PainTwoTest.class)) {
public void setUp() throws Exception {
System.out.println("setUp()");
}
public void tearDown() throws Exception {
System.out.println("tearDown()");
}
};
}
/**
* PainOneTest
*/
public PainTwoTest() {
System.out.println("PainOneTest");
}
/**
* testMe
*/
public void testMe() {
System.out.println("testMe");
}
/**
* testYou
*/
public void testYou() {
System.out.println("testYou");
}
/**
* main
* @param _
*/
public static void main(String []_) {
Test test = PainTwoTest.suite();
}
}This code produces:
PainOneTest
PainOneTest
setUp()
testMe
testYou
tearDown()The above solution introduces a
staticmethod, and all variables accessed by it must also be declaredstatic. Although I have nothing in particular againststaticmethods, you must take care when implementing test methods for concurrency access (sharingstaticobjects) andstaticinitialization (occurring only once per VM).Remember, when using Ant, the default behavior is to use the VM that started Ant to execute all the tasks; in the above scenario, you may well have to use the
forkattribute of the Java core task.Furthermore, multiple instantiations of the
TestCaseclass remain. Surely, if we want the test methods to be independent of each other, we would place them into differentTestCaseclasses. So why multiple instantiations? Multiple instantiations allow you to isolate independent methods within the sameTestCaseclass, which is not always the desired behavior. Finally, too much technical code is required for circumventing the multiple-instantiations scenario: I just want a test POJO (plain old Java object) class.How does TestNG handle multiple instantiations?
TestNG does not require static block initialization and has a flexible configuration scheme for handling test classes based on regular expressions and XML configuration files. TestNG does not instantiate the test class several times.Multithreaded support
GroboUtils is an addition to JUnit that supports multithreaded unit tests (since the JUnit framework lacks such support). N. Alex Rupp gives an overview of GroboUtils in "Multithreaded Tests with JUnit" (java.net, August 2003). Although very useful, GroboUtils is not to my taste as it requires too much glue code to handle multithreaded unit tests.How does TestNG handle multithreaded unit tests?
TestNG has multithreaded and parallel unit tests built in its core. You don't need to write specific code to handle multithreaded unit tests as they are just a configuration of TestNG.Why should I extend a specific class and prefix the methods with test?
I would rather tag any method than be obliged to prefix methods withtest, a task that JUnit requires. Admittedly, this is a minor point, but all IDEs now give you a view (like the Eclipse outline view) that helps you quickly browse your methods to find the one you want—that is, if they are not all prefixed with the same four letters. From a practical viewpoint, the Eclipse outline view is useless when you have dozens of test methods on a class that start with the same four letters.How does TestNG handle test method names?
TestNG uses Java annotations as specified by Java Specification Request 175 to tag methods for testing and grouping, and for expected exceptions, and so on.Introducing TestNG
TestNG is inspired by JUnit and also NUnit a unit-testing framework for .Net.TestNG introduces new functionalities to unit testing such as:
- Support for Java annotations (if unfamiliar with annotations, see sidebar "What Are Annotations")
- XML configuration file for test configuration
- No required class extension or interface implementation
- Support for dependent methods and groups
- Support for parallel testing
- Parameters for test methods
- Arbitrary number of invocations plus success rate
Step by step: Your first TestNG program
Let's get started with TestNG. The remaining sections of this article introduce you to the TestNG features, starting with a basic first unit test: how to configure it and run it.Here is a simple test class:
package org.jyperion.testng;
import com.beust.testng.annotations.*;
public class FirstTest {
@Configuration(beforeTestClass = true)
public void configure() {
System.out.println("configure");
}
@Test(groups = {"exec-group"})
public void exec() {
System.out.println("exec");
assert 1 == 2;
}
}Note that no interface or class is required for your test class. The
@Configurationannotation is the equivalent of the JUnit methodsetup(); you can annotate any method in your test class.@Configuration(beforeTestClass = true)means that the annotated method will be called just once before testing any method in the class. The@Testannotation is equivalent to the JUnit methods prefixed with the wordtest.Next comes the XML configuration test called
testng.xml:
Finally, TestNG's invocation:
java –ea com.beust.testng.TestNG testng.xmlNote:
-eaenables assertions.The following appears on the console:
configure
exec
[TestRunner] FAILED: org.jyperion.testng.FirstTest.exec
[TestRunner] REASON: java.lang.AssertionError
===============================================
TestNG Samples
Total tests run: 1, Failures: 1 Skips: 0
===============================================An HTML report is also created in a folder called test-output:
Figure 1. Your first TestNG HTML report. Click on thumbnail to view full-sized image.
An interesting feature of TestNG is the test setup mechanism enabled by using the
@Configurationannotation in four different scenarios:
- Run configuration method once before all tests
- Run configuration method once before every test
- Run configuration method once after every test
- Run configuration method once after all tests
Moreover, if you use TestNG's powerful grouping feature (described in subsequent sections), the
@Configurationmethods can have a determined execution order.TestNG DTD view
The configuration is defined by a DTD (document type definition). Figure 2 shows the schematic view.
Figure 2. TestNG DTD. Click on thumbnail to view full-sized image.
The test annotation
The@Testannotation is defined as:
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({java.lang.annotation.ElementType.METHOD,
java.lang.annotation.ElementType.TYPE})
public @interface Test {
public String[] groups() default {};
public boolean enabled() default true;
public String[] parameters() default {};
public String[] dependsOnGroups() default {};
public long timeOut() default 0;
}The
groupselement allows you to group methods and groups. By configuringtestng.xml, you can include or exclude groups using regular expressions:
public class GroupTest {
@Test(groups = {"group-one"})
public void init() {
System.out.println("init#group-one");
}
@Test(groups = {"group-two"}, dependsOnGroups = {"group-one"})
public void exec() {
System.out.println("exec#group-two");
}
@Test(groups = {"group-three"})
public void testMe() {
System.out.println("testMe#group-three");
}
}The corresponding
testng.xml:
Parameters
Parameters defined intestng.xmlcan be passed to the test methods at runtime:
public class ParameterTest {
@Test(parameters = { "jndi-name" })
public void lookup(String jndiName) {
System.out.println("lookup("+jndiName+")");
}
}The corresponding
testng.xml:
Note that parameters can be placed within the
suiteelement, within thetestelement, or within theclasselement, so that your parameter is available to all the classes in the suite, to all the classes in the test, or to only the class.The corresponding
testng.xml:
verbose="1"
parallel="true"
thread-count="10">
The element
parallelis set totrueto instruct TestNG to run the tests in parallel.thread-countspecifies the number of threads in the pool (if omitted, default is5).The HTML report in Figure 3 shows that the methods run chronologically and also presents the thread identifier used to execute a test method.
Figure 3. TestNG parallel report. Click on thumbnail to view full-sized image.
Figure 4 shows the threads associated with the test methods.
Figure 4. TestNG parallel report with thread information. Click on thumbnail to view full-sized image.
TestNG allows two running scenarios:
- Sequential run of tests (default)
- Parallel run of tests
To select which scenario to use, simply edit the
testng.xml.Another nice feature of TestNG is its support of
timeouts, which can be used in parallel and nonparallel modes using thetimeOutattribute in@Test.Ant task
TestNG is also shipped with an Ant task:
fork="yes"
classpath="${my.classpath}"
outputDir = "${basedir}/test-output">
Using this Ant task (which is part of the TestNG distribution), TestNG could be used for continuous integration, for example.
Support for J2SE 1.4
TestNG supports old-style javadoc-like tags for J2SE 1.4. I do not bother describing them here because they are not of much interest if you are testing J2SE 5.0 software. However, TestNG can be used for testing J2SE 1.4 applications.Extending TestNG
TestNG is available under the Apache Software License, and, as an open source project, can be extended to enable further development. The TestNG distribution comes with the source, and, in this section, I outline a way to register a listener that listens to events when TestNG executes tests. This approach resembles a SAX (Simple API for XML) implementation in that you are notified while TestNG runs the tests. I illustrate how to extend TestNG by implementing a PDF report generator using the fantastic iText library, released under MPL and LGPL.Note: Cedric Beust is currently designing a flexible plug-in framework that will allow developers to extend TestNG beyond JUnit's abilities.
To extend TestNG to generate a PDF report, you (currently) need to:
- Write a class that implements the
com.beust.testng.ITestListenerinterface- Register that class in the
initLoggers()method from thecom.beust.testng.TestRunnerclass- Rebuild the TestNG JAR
You can find the code for the PDF generator in the JyperionListener.java file; to see the result, open the JypTest.pdf file (both files are downloadable from Resources).
Case study: Price publishing and asynchronous testing
In this simple case study, we simulate sending prices to an electronic communications network (ECN) market (implemented by a Remote Method Invocation server) from a client using Java Message Service (JORAM—Java Open Reliable Asynchronous Messaging—from ObjectWeb is used as message-oriented middleware). This study illustrates how to use TestNG for testing asynchronous code (for more on the topic, read the Testing Asynchronous Code Weblog). Our ECN simulator sends the same price back to the client 90 percent of the time, acknowledging the market's price. It sends a slightly different bid and ask price 5 percent of the time and an "off" status for that quote another 5 percent of the time. The price publisher is a Java client that publishes messages using Java Message Service (JMS).The
TestPricePublisheris just another JMS client that usesPricePublisherto send prices and then waits for a price to return (usingwait(long)so a long delay can causeLongUpdateException). TheTestPricePublisheralso checks if the price received is identical to the one sent (if not, aWrongPriceExceptionis thrown) and if the price status is on (if not, anOffExceptionis thrown).The code for asynchronous testing (from
TestPricePublisher) follows below:
@Test(invocationCount = 20)
public void sendPrice() throws LongUpdateException, WrongPriceException, OffException, InterruptedException {
this.sendPrice("JYPERION", this.askPrice, this.bidPrice);
this.askPrice += 0.01;
this.bidPrice += 0.01;
synchronized (this) {
try {
long TIMEOUT = 100;
this.wait(TIMEOUT);
if (this.quote == null)
throw new LongUpdateException("No message received");
if (quote.getStatus() == PriceStatus.OFF)
throw new OffException("Quote " + this.quote.getISIN() + " is OFF");
if (!quote.isPriceConsistent())
throw new WrongPriceException("Wrong price for " + this.quote.getISIN());
} catch (InterruptedException e) {
throw new InterruptedException();
} finally {
this.quote = null;
}
}
}
/**
* @see javax.jms.MessageListener#onMessage(javax.jms.Message)
*/
public void onMessage(Message msg) {
try {
synchronized(this) {
this.quote = (Quote) ((ObjectMessage)msg).getObject();
LOGGER.info("Notifying waiting threads");
this.notifyAll();
}
} catch (JMSException e) {
e.printStackTrace();
}
}The corresponding
testng.xml:
parallel="true"
thread-count="10"
verbose="1">
The generated HTML report:
Figure 5. TestNG report for the price publisher. Click on thumbnail to view full-sized image.
If you run the tests more than once, TestNG keeps them in the generated HTML page.
What is next?
Cedric and Alexandru are currently working on:
- A pluggable module architecture for TestNG (so you can plug in your PDF generation engine in a standard way)
- An Eclipse plug-in for TestNG, see a preview in Figure 6
Figure 6. Eclipse plug-in preview. Click on thumbnail to view full-sized image.
Conclusion
This article described how to employ some new features for unit testing with TestNG. Among TestNG's capabilities, I would qualify its support for J2SE 5.0 annotations, flexible test configurations based on XML files and regular expressions, support for dependent test methods, and core multithreaded support as being the most important. TestNG shows that there is still room for improvement in unit testing. I am certain developers would find refreshing ideas in TestNG.One last thing, if TestNG is lacking a feature, you can argue your case in TestNG users' mailing list. If Cedric and Alexandru are convinced, most likely, this feature will be added to the next release. Could you be more agile?
你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=2572567