Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit 4 vs TestNG - Update 2013 - 2014 [closed]

JUnit 4 and TestNG used to be comparable. What are the pros and cons of the two testing frameworks?

like image 333
arket Avatar asked Jan 03 '13 10:01

arket


People also ask

Which is better TestNG or JUnit?

Both Testng and Junit are Testing framework used for Unit Testing. TestNG is similar to JUnit. Few more functionalities are added to it that makes TestNG more powerful than JUnit.

Which of the following features are applicable for both TestNG and JUnit?

Parameterized Test This feature is implemented in both JUnit 4 and TestNG. However both are using very different method to implement it.

What is difference between JUnit & TestNG framework?

JUnit is an open-source framework used to trigger and write tests. TestNG is a Java-based framework that is an upgraded option for running tests. JUnit does not support to run parallel tests. TestNG can run parallel tests.


1 Answers

I was comparing TestNG and JUnit4 today, and the main advantage that I can point out with my limited experience in testing-frameworks is that TestNG has a more elegant way of handling parametrized tests with the data-provider concept.

As far as I can tell with JUnit4 you have to create a separate test class for each set of parameters you want to test with (ran with @RunWith(Parameterized.class)). With TestNG you can have multiple data-providers in a single test class, so you can keep all your tests for a single class in a single test-class as well.

So far that is the only thing that I can point out as a benefit of TestNG over JUnit4.

Intellij IDEA includes support for TestNG and JUnit out of the box. However, Eclipse only supports JUnit out of the box and needs a TestNG plugin installed to make it work.

However, a more annoying problem I ran into with TestNG is that your test classes need to extend PowerMockTestCase if you are using PowerMock to mock dependencies in your tests. Apparently there are ways to configure the object-factory your test framework needs to know about when using PowerMock via a special method, or via the testng.xml suite definition, but those seem to be broken at the moment. I dislike having test-classes extend test-framework classes, it seems hackish.

If you don't use PowerMock this is not an issue of course, but all in all I get the impression that JUnit4 is better supported.

like image 70
JeroenHoek Avatar answered Oct 20 '22 22:10

JeroenHoek