There have been lately lots of posts on junit group about JUnit next release (4.0 - based on a single phrase poured inside an interview). Finally, two days ago somebody watching Kent's testing presentation on Agitar, pulled the curtains and let us know that the code is already in CVS on a special branch. I felt a kind of enthusiasm hearing this, as JUnit was almost frozen for a long time. I have checked out the sources, and after a small digging I have found a set of interesting points that determined me to do a comparison between and the next JUnit (and in a way these represent a good validation of our ideas).
Now let me step through the new things to come in the next version and do a comparison with what TestNG is already offering (for detailed documentation of TestNG please consult the official site):
TestNG has introduced annotations (JDK1.5 annotations and respectively javadoc-based annotations) for marking the important pieces of your tests (
@Testfor test methods,
@Configurationfor configuration methods -
@ExpectedExceptionsfor exception testing,
@Factoryfor test factories and
@Parameterfor passing parameters to tests.
JUnit 4 biggest change is the adoption of annotations (I will present them later, throughout this entry).
TestNG has introduced 4 different levels of configuring a test: suite, test, class and method (in the form of
before/afterTestMethod), from which JUnit 3.x supported only suite and method levels.
JUnit 4 is bringing the
@AfterClassannotations for marking before/after class configuration methods.
A test method in TestNG is defined using the
@Testwhose definition is:
JUnit 4 is introducing a
From the above definitions, we see that in TestNG a test may be enabled/disable, feature present in JUnit 4 in the form of the annotation
@Ignorefor disabling a test method.
In TestNG, you can provide a timelimit after which the test method if not finished is considered failed: timeOut.
@Testannotations contains a
On the same TestNG
@Testdefinition you can see that you can provide the number of times you want the test method invoked.
JUnit 4 is bringing
RepeatedTestdecorator , that will allow to run repeatedly a test.
TestNG tests are run inside independent thread in order to allow parallelism and also the above mentioned timeOut feature (see docs).
In JUnit 4 this is a new decorator
ActiveTestSuite that runs each test in a separate thread and waits untill all threads have terminated (javadoc).
An interesting feature introduced by TestNG was the
@ExpectedExceptionannotation that allows an automatic test for exceptions.
In JUnit 4
@Testannotation definition you can see the
Class extends Throwable> expected() default None.class;.
TestNG is providing two kinds of listeners allowing to extend the functionality:
ITestListener(acknowledge upon test method events) and
ISuiteListener(triggered upon suite events).
We can find the
ITestListenersynonim in JUnit:
TestListener(triggered upon test method events).
test categorization & filtering (<- seems like a runner issue) @Category(short, integration) @Test(category=windowsOnly).
Update: thanks to Kent's clarifications I have to correct the above information about
ActiveTestSuite. These are available since JUnit 3.x and they are included in JUnit 4 just for backward compatibility.