preface

TestNG Vs JUnit: What’s the Difference?

This article focuses on Junit and TestNG. These features can help developers decide which framework to use for development unit testing. TestNG and Junit are both testing frameworks for unit testing. Because TestNG is a testing framework inspired by Junit and Nunit, the two are very similar. TestNG has some additional features compared to Junit. This makes TestNG more powerful than Junit.

Similarities and differences

First, look at some of the similarities between TestNG and Junit4. The following table illustrates the features supported by Junit and TestNG, respectively. This table provides a quick comparison of the two, which helps us quickly determine which technique is best suited for unit testing.

Compare the item Junit4 TestNg
annotations support support
Test suite support support
Ignore the test support support
Abnormal test support support
Timeout handling support support
Ignore the test support support
Parametric test support support
Relying on the test Does not support support
As you can see from the table above, TestNg is the same as Junit except for one or two features.

annotations

Both Junit and TestNG support annotations, and their annotation names look similar. For example, TestNG uses annotations @beforeMethod, @afterMethod and Junit uses @before, @after. For timeout annotations, both use @test (timeout=1000). The following table provides more details.

The serial number instructions TestNg Junit4
1 Test annotation @Test @Test
2 The logic that needs to be executed before the first test method in a unit test class executes @BeforeClass @BeforeClass
3 The logic that needs to be executed after all the test methods of a unit test class are executed @AfterClass @AfterClass
4 Logic that needs to be executed before each test method is executed @BeforeMethod @Before
5 Logic that needs to be executed after each test method is executed @AfterMethod @After
6 Annotations to ignore tests @Test(enable=false) @Ignore
7 Annotations for exceptions @Test(expectedExceptions = ArithmeticException.class) @Test(expected = ArithmeticException.class)
8 Timeout annotations @Test(timeout = 1000) @Test(timeout = 1000)
9 Logic that is executed before tests in all test suites @BeforeSuite There is no
10 Logic that is executed after tests in all test suites @AfterSuite There is no
11 Execute before the test begins @BeforeTest There is no
12 Execute after the test has started @AfterTest There is no
13 Execute before the first test method of the test group is called by reflection @BeforeGroups There is no
14 The last test method of the test group is executed by reflection call @AfterGroups There is no

Test suite

Suites are typically used to perform multiple tests together. Both TestNG and Junit can create suite tests. However, TestNG works better with suites that test many different test methods. This can be shown in the following code.

Use JUnit for suite testing

Junit uses a defined class approach for suite testing.

package guru99.junit;		
import org.junit.runner.RunWith;		
import org.junit.runners.Suite;		

@RunWith(Suite.class)				
@Suite.SuiteClasses({				
    SuiteTest1.class,			
    SuiteTest2.class,			

})		

public class JunitTest {		
// This class remains empty,it is used only as a holder for the above annotations		
}
Copy the code

Use TestNG for suite testing

TestNG uses XML files to bind all test methods. The following XML file describes a TestNg test suite.

<! DOCTYPEsuite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="My test suite">
<test name="testing">
<classes>
<class name="com.guru99.SuiteTest1" />
<class name="com.guru99.SuiteTest2" />
</classes>
</test>
</suite>
Copy the code

Invalid test case

Skip specified unit tests when using a different approach. Here is the sample code.

Junit

Use the @ignore annotation in Junit to skip unit tests. The following code snippet:

@Ignore
public void method1(a) 
{
	System.out.println("Using @Ignore , this execution is ignored");
}
Copy the code

TestNG

Use the @test (Enabled =false) annotation on TestNg to skip the specified unit tests. The following code snippet:

@Test(enabled=false)
public void TestWithException(a)
{  
	System.out.println("Method should be ignored as it's not ready yet");
}
Copy the code

Abnormal test

TestNg and Junit both support exception testing. An exception test is used to verify that an exception is thrown correctly in a test.

Using Junit

@Test(expected = ArithmeticException.class)  
public void divideByZero(a) 
{  
	Int i = 1/0;
}
Copy the code

Using TestNG

@Test(expectedExceptions = ArithmeticException.class)  
public void divideByZero(a)
{  
Int i = 1/0;
}
Copy the code

timeout

This feature is implemented in TestNG and Junit. Timeout is used to terminate a unit test that has been executed for longer than a specified time in milliseconds.

Using junit

@Test(timeout = 1000)  
public void method1(a)
{  
	while (true);  
}
Copy the code

Using TestNG

@Test(timeOut = 1000)  
public void method1(a)
{  
	while (true);  
}
Copy the code

Parameter test

Junit provides a relatively simple and easy-to-understand way to test, called parametric testing. TestNG and Junit both support parameter testing, but they define parameters differently. The following are explained one by one.

Using junit

The @runwith and @parameter annotations provide Parameter values for unit tests. The @parameters annotation is used to return a list of parameter values. The parameter values in the annotations are passed in as values for the class constructor.

@RunWith(value = Parameterized.class)
public class JunitTest{
    
    privateint number;
    
    public JunitTest6(int number)
 {
    this.number = number;
     }

     @Parameters
    public static Collection<Object[]> data() 
{
       Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4}}; returnArrays.asList(data); }@Test
    public void parameterTest(a)
 {
    System.out.println("Parameterized Number is : "+ number); }}Copy the code

Using TestNG

In TestNg, use XML files or @DataProvider annotations to provide parameters for unit tests. In TestNg, the @parameters annotation is used to state that the method needs Parameters to be tested. The data used as parameters is provided in TestNG’s XML configuration file. Using this approach, we can duplicate a test case with different sets of parameters and get different results. The test case

public class Test1 {

    @Test
    @Parameters(value="number")
    public void parameterTest(int number)
	{
    	System.out.println("Parameterized Number is : "+ number); }}Copy the code

XML configuration file:

<! DOCTYPEsuite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="My test suite">
<test name="testing">
    
<parameter name="number" value="2"/>    

<classes>
<class name="com.guru99.Test1" />
</classes>
</test>
</suite>
Copy the code

conclusion

We compared TestNg and Junit in detail and saw similarities except for parametric tests and dependency tests. Simply put, we can use any one of these for unit testing based on flexibility and requirements.

Junit refers to Junit4Copy the code