JUnit 4 vs. TestNG vs. __ Test

Source: Internet
Author: User
Tags testng

http://fuxueliang.com/java/2013/06/26/junit-4-vs-testng--comparison/

These two days are reading a book, Java test new technology TestNG and advanced concepts, the author is the founder of TestNG, learned a lot about the knowledge of TestNG, read an article basically put this book several views are embodied, then I directly translated the original, so as to save myself summary. These two days without the authorization of the original author may be published this weekend, did not pass the permission to translate other people's articles should indeed not be a glorious thing, etc. Original link JUnit 4 Vs testng–comparison, thank you very much for writing a good article, but Mr. Mkyong wrote a lot of articles, often search the first article Google is always his. If there is a translation problem, please take a brick.

————————————————————————————————————————————

Junit 4 and TestNG are both very popular unit test frameworks in Java. Two frameworks are very similar in functionality. which is better. Which framework should we choose in the Java project?

The following figure compares the functional features of JUnit 4 and TestNG.

Annotation Support

The implementations of Junit 4 and TestNG are very similar in terms of annotations.

Characteristics JUnit 4 Testng
Test annotations @Test @Test
The test suite needs to execute before executing @BeforeSuite
The test suite that needs to be executed after execution @AfterSuite
What needs to be done before the test @BeforeTest
What needs to be done after the test @AfterTest
Executes before the first method of any group belonging to a test method is called @BeforeGroups
Executes after the last method of any group to which a test method belongs is called @AfterGroups
Executes before the first test method call for the current class @BeforeClass @BeforeClass
Executes after the last test method call for the current class @AfterClass @AfterClass
Each test method needs to be executed before @Before @BeforeMethod
Each test method needs to be executed after @After @AfterMethod
Ignore @ignore @Test (Enbale=false)
Expected exception @Test (expected = arithmeticexception.class) @Test (expectedexceptions = arithmeticexception.class)
Timeout @Test (timeout = 1000) @Test (timeout = 1000)

The differences in annotation between JUnit 4 and TestNG are mainly as follows: in JUnit 4, if we need to use @beforeclass and @afterclass in front of the method, then the test method must be a static method. TestNG is more flexible in the method definition section, and it does not require similar constraints. 3 additional Setup/teardown levels: Kits and groupings (@Before/aftersuite, @Before/aftertest, @Before/aftergroup). For more information, please see here.

JUnit 4

@BeforeClass public
static void Onetimesetup () {
    //One-time initialization code   
	SYSTEM.OUT.PRINTLN (@ Beforeclass-onetimesetup ");
}

Testng

@BeforeClass public
void Onetimesetup () {
    //One-time initialization code   
	SYSTEM.OUT.PRINTLN (@ Beforeclass-onetimesetup ");
}

In JUnit 4, the naming of annotations is rather confusing, such as before, after and expected, and we are not quite sure how the annotations before the method have before and after, and so do expected. TestNG in this respect to do a lot better, the annotation used beforemethod,aftermethod and expectedexception, such a name is very good understanding. Exception Test

Anomaly testing means that what should be thrown in a unit test is reasonable, and this feature has been implemented in two frameworks.

JUnit 4

@Test (expected = arithmeticexception.class) public  
void Divisionwithexception () {  
	int i = 1/0;
}

Testng

@Test (expectedexceptions = arithmeticexception.class) public  
void Divisionwithexception () {  
	int i = 1/0;
}
Ignore Test

Ignoring the test means that the unit tests which can be ignored, this feature has been implemented in two frameworks.

JUnit 4

@Ignore (' Not Ready to Run ')  
@Test public
void Divisionwithexception () {  
	System.out.println () Ready yet ");
}

Testng

@Test (enabled=false) public
void Divisionwithexception () {  
	System.out.println ("The method isn't ready yet");
Time Test

Time Test means that if a unit test runs longer than a specified number of milliseconds, the test terminates and is marked as a failed test, a feature that has been implemented in two frameworks.

JUnit 4

@Test (timeout = 1000) public  
void Infinity () {while  
	(true);  
}

Testng

@Test (timeOut = 1000) public  
void Infinity () {while  
	(true);  
}
Suite Test

The suite test is to combine several unit tests into a single module, and then run, which has been implemented in two frameworks. However, it is implemented in two different ways.

JUnit 4

@RunWith and @Suite annotations are used to perform suite tests. The following code shows the need for JunitTest1 and JunitTest2 to execute after JUNITTEST5 is executed. All declarations need to be done inside the class.

@RunWith (Suite.class)
@Suite. suiteclasses ({
    junittest1.class,
    junittest2.class
})
Public Class JunitTest5 {
}

Testng

Executing a suite test is done using an XML file configuration. The following XML file allows TestNGTest1 and TestNGTest2 to execute together.

<! DOCTYPE Suite SYSTEM "Http://beust.com/testng/testng-1.0.dtd" >
<suite name= "My test suite" >
  < Test Name= "Testing" >
    <classes>
       <class name= "Com.fsecure.demo.testng.TestNGTest1"/>
       <class name= "Com.fsecure.demo.testng.TestNGTest2"/>
    </classes>
  </test>
</suite >

TestNG can do better in this piece, using the concept of a group, each method can be assigned to a group, can be grouped according to functional characteristics. For example:

This is a class with 4 methods, 3 groups (METHOD1, METHOD2 and METHOD4)

@Test (groups= "method1") public
void TestingMethod1 () {  
  System.out.println ("Method-testingmethod1 ()");
}  

@Test (groups= "method2") public
void TestingMethod2 () {  
	System.out.println ("Method-testingmethod2 ()") ;
}  

@Test (groups= "method1") public
void Testingmethod1_1 () {  
	System.out.println ("Method-testingmethod1_1 ()");  

@Test (groups= "METHOD4") public
void TestingMethod4 () {  
	System.out.println ("method-testingmethod4 ()");
}

The following XML file defines a unit test for a group that executes only methed1

<! DOCTYPE Suite SYSTEM "Http://beust.com/testng/testng-1.0.dtd" >
<suite name= "My test suite" >
  < Test Name= "Testing" >
  	<groups>
      <run>
        <include name= "method1"/>
      </run>
    </groups>
    <classes>
       <class name= "Com.fsecure.demo.testng.TestNGTest5_2_0"/>
    </classes>
  </test>
</suite>

With the concept of grouping, integration testing can be more powerful. For example, we could just perform tests in all tests where the group name is databasefuntion. Parametric testing

Parametric testing means passing multiple parameter values to a unit test. This feature is in JUnit 4 and testng. The two frameworks are then implemented in a completely different way.

JUnit 4

@RunWith and @Parameter annotations are used to provide parameter values for unit tests, @Parameters must return to the List, and parameters are passed as arguments to the constructor of the class.

@RunWith (value = parameterized.class) public
class JunitTest6 {
 
	 private int number;
 
	 public JunitTest6 (int number) {
	    this.number = number;
	 }
 
	 @Parameters public
	 static collection<object[]> data () {
	   object[][] data = new object[][] {1}, {2}, {3 }, {4}};
	   return arrays.aslist (data);
	 }
 
	 @Test public
	 void Pushtest () {
	   System.out.println ("Parameterized number is:" + number);
	 }

It has a number of limitations in its use; we must follow JUnit's way of declaring parameters, which must be initialized to the members of the class for testing through the parameters of the constructor. The parameter type returned must be List [], the data has been limited to string, or it is a raw value.

Testng

Use an XML file or @dataprovider annotation to provide parameters to the test.

XML file Configuration parameterization test

Simply declare the @parameters annotation on the method, and the data for the parameter will be provided by the TestNG XML configuration file. After doing so, we can reuse a test case using a different dataset or even a different result set. In addition, even end users, QA or QE can provide their own data for testing using XML files.

Unit Test

  public class Testngtest6_1_0 {

   @Test
   @Parameters (value= "number") public
   void Parameterinttest (int number ) {
      System.out.println ("Parameterized number is:" + number);
   }

  }

XML file

<! DOCTYPE Suite 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.fsecure.demo.testng.TestNGTest6_0 "/>
    </classes>
  </test>
</suite>

Parametric testing @DataProvider annotations

It can be convenient to initialize data using an XML file, but tests occasionally require complex types, and a string or original value is not fully satisfied. TestNG's @ dataprovider annotation allows you to better map complex parameter types to a test method to handle this situation.

@DataProvider can use vectors, String, or Integer-type values as arguments

@Test (Dataprovider = "data-provider-function") public
void Parameterinttest (Class clzz, string[] number) {
   SYSTEM.OUT.PRINTLN ("Parameterized number is:" + number[0]);
   SYSTEM.OUT.PRINTLN ("Parameterized number is:" + number[1]);
}

This function would provide the Patameter data
@DataProvider (name = "Data-provider-function") public
object[][ ] Parameterinttestprovider () {return
	new object[][]{
	   {vector.class, new string[] {"Java.util.AbstractList", " Java.util.AbstractCollection "}},
	   {string.class, new string[] {" 1 "," 2 "}},
	   {integer.class, new string[] {" 1 ", "2"}}}
	;

@DataProvider as an object's argument

P.S "Testngtest6_3_0" is a simple object that uses the get and set methods.

@Test (Dataprovider = "data-provider-function") public
void Parameterinttest (Testngtest6_3_0 clzz) {
   SYSTEM.OUT.PRINTLN ("Parameterized number is:" + clzz.getmsg ());
   SYSTEM.OUT.PRINTLN ("Parameterized number is:" + clzz.getnumber ());
}

This function would provide the Patameter data
@DataProvider (name = "Data-provider-function") public
object[][ ] Parameterinttestprovider () {

	Testngtest6_3_0 obj = new Testngtest6_3_0 ();
	Obj.setmsg ("Hello");
	Obj.setnumber (123);

	return new object[][]{
    	{obj}}
	;
}

TestNG's parameterized tests are very friendly and flexible to use (whether it's XML configuration or the way annotations are in the class). It can use many complex data types as values for parameters, and there are no restrictions. As shown in the example above, we even can pass with our own object (TESTNGTEST6_3_0) for parameterized test dependency testing

Parametric testing means that the method of testing is dependent on the part of the method to be executed before execution. If an error occurs on a dependent method, all child tests are ignored and are not marked as failures.

JUnit 4

The JUnit framework focuses primarily on quarantine for testing and does not support this feature for the time being.

Testng

It uses Dependonmethods to implement the functionality of dependent tests, as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.