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 (
@Test
for test methods,@Configuration
for configuration methods -setUp/tearDown
,@ExpectedExceptions
for exception testing,@Factory
for test factories and@Parameter
for 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/afterSuite
,before/afterTest
,before/afterTestClass
andbefore/afterTestMethod
), from which JUnit 3.x supported only suite and method levels.
JUnit 4 is bringing the@BeforeClass
and@AfterClass
annotations for marking before/after class configuration methods. -
A test method in TestNG is defined using the
@Test
whose definition is:
JUnit 4 is introducing a@Test
annotation too:
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@Ignore
for disabling a test method.
In TestNG, you can provide a timelimit after which the test method if not finished is considered failed: timeOut.
JUnit@Test
annotations contains atimeout
.
On the same TestNG@Test
definition you can see that you can provide the number of times you want the test method invoked.
JUnit 4 is bringingRepeatedTest
decorator [1], 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 decoratorActiveTestSuite
[1] that runs each test in a separate thread and waits untill all threads have terminated (javadoc). -
An interesting feature introduced by TestNG was the
@ExpectedException
annotation that allows an automatic test for exceptions.
In JUnit 4@Test
annotation definition you can see theClass expected() default None.class;
. -
TestNG is providing two kinds of listeners allowing to extend the functionality:
ITestListener
(acknowledge upon test method events) andISuiteListener
(triggered upon suite events).
We can find theITestListener
synonim 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
RepeatedTest
and ActiveTestSuite
. These are available since JUnit 3.x and they are included in JUnit 4 just for backward compatibility.
2 comments:
Find I could not find RepeatedTest in JUnit4.2 also.
Is it TODO for JUnit?
--Afroz
I wrote a bunch of test classes ( almost 15 each with over 15 test methods) and then i thought I will write a test suite. Only to find out that Junit4.0 has no test suites :(. The work around is tedious when I have to add the method to include junit4.0 in all the 15 classes. Is there some other better way
Post a Comment