TestNG is an Automation Testing framework in which NG stands for “Next Generation”. TestNG is inspired from JUnitand NUnit which uses the annotations (@) – a special form of syntactic meta-data added to Java source code for better code readability, but introducing some new functionalities that make it more powerful and easy to use.

Why use TestNG?

Using TestNG framework, we can generate a proper report that can easily identify how many test cases are passed, failed and skipped as well. Then, we can execute the failed tests separately.

For example suppose we have five test cases, one method is written for each test case and assume that program is written using main method without TestNG. When we run this program, three methods are executed successfully and the fourth method is failed. Then, we can run the fourth method separately by implementing certain changes on the code. It is not possible without using TestNG.

Also, TestNG provides an option i.e., testing-failed.xml file in test-output folder where we can run only failed test cases by executing this file.

Features of TestNG:

Features supported by TestNG are:

  • Annotations (@) – ‘Annotations’ are a predominant feature of TestNG that are simply a piece of instruction for the compiler that we apply to classes, methods or variables in our Java code.

Any class can be turned into a TestNG test class by just adding one TestNG annotations.

  •  @Test – is used to designate test methods in a class and can be applied to individual methods as,
public class ExampleMethodAnnotations {      
  @Test  
     public void checkSomething() {}  
     @Test 
      public void checkSomethingElse() {} 
      public void notATestMethod() {}  
 }

Test annotation can also be applied to a class, where all public methods of the class will be test methods, unless the methods are annotated by one of @BeforeXXX or @AfterXXX configuration annotations listed below.

@Test  
 public class ExampleClassAnnotation 
{    
   public void checkSomething() {}    
   public void checkSomethingElse() {}  
 }

Configuring Annotations

Configuration Annotations can be applied to methods for performing some action before/after specific events:

  • @BeforeSuite/@AfterSuite – Executes before/after any tests in test suite
  • @BeforeTest/@AfterTest – Executes before/after any tests in element which contains this class
  • @BeforeClass/@AfterClass – Executes before/after any tests in this class
  • @BeforeMethod/@AfterMethod – Executes before/after every test method in this class
  • @BeforeGroups/@AfterGroups – Executes before/after any tests in specified groups

The TestBeforeAfter are executed in the below order

public class TestBeforeAfter {  
     @BeforeSuite 
      public void beforeSuite() {}   
     @AfterSuite 
      public void afterSuite() {} 
      @BeforeTest 
      public void beforeTest() {} 
      @AfterTest  
     public void afterTest() {} 
      @BeforeClass 
      public void beforeClass() {}  
     @AfterClass 
      public void afterClass() {} 
      @BeforeMethod 
      public void beforeMethod() {} 
      @AfterMethod 
      public void afterMethod() {} 
      @Test  
     public void test1() {} 
      @Test 
      public void test2() {} 
  }
  • Supports data-driven testing – with the help of the annotation ‘@dataprovider’
  • Multi-Threading support – tests can run in multiple threads to speed up execution flow
  • Powerful execution model – without using any TestSuite, execution can be done
  • Test groups – test methods can associate with any number of groups
  • Test Priorities – tests can be prioritized based on requirement
  • Parameterized test methods and data providers – allows data to be supplied to a configuration method/test form any other method or from an XML file 
  • Parallel Execution  TestNG supports running tests in multiple threads. Using this feature, TestNG speeds up the execution of thread-safe tests. In default, all tests runs in a single thread. Usage of multiple threads can be done by using parallel and thread-out attributes.

Setup Maven dependency

Maven is a software project management and comprehension tool developed by Apache. We can add the Maven dependency in our pom.xml file as,

<dependency>  
  <groupId>org.testng</groupId> 
   <artifactId>testng</artifactId> 
   <version>6.11</version> 
   <scope>test</scope>
</dependency>

The latest version of Maven repository is available here. TestNG plugin may be downloaded and installed from Eclipse Marketplace while using Eclipse. 

Assertions

Asserts helps us to verify the test conditions and decide whether the test has passed or failed. A test is considered as successful ONLY if it is completed without throwing any exception. Assertions are further divided as:

  • SoftAssert – helps to collect all assertions throughout @Test method.
  • HardAssert – throws an exception immediately when assert statement fails and continues with the next test in the Test Suite.

Including assertions in TestNG the test functionality can be controlled with JDK version form or above 1.7.

Writing a Test Case

We just need to annotate test method with org.testng.annotations.Test annotation to write a test.

@Testpublic void givenNumber_whenEven_thenTrue() {    
assertTrue(number % 2 == 0);
}

Test Configurations

When writing test cases, we often need to execute some configurations before test executions and also some clean-up features at method, class, suite, and group levels.

@BeforeClass
public void setup() {  
  number = 12;
}
@AfterClass
public void tearDown() {    
number = 0;
}

Test Execution

Running test cases with Maven’s “test” command will execute all test cases annotated with @Test putting to a default test suite. 

<plugin>   
 <groupId>org.apache.maven.plugins</groupId>   
 <artifactId>maven-surefire-plugin</artifactId>   
 <version>2.19.1</version>    
<configuration>       
 <suiteXmlFiles>        
    <suiteXmlFile>     src\test\resources\test_suite.xml            </suiteXmlFile>      
  </suiteXmlFiles>   
 </configuration>
</plugin>

For multiple XML files covering all test cases, we can add all the files in suiteXmlFiles tag.

<suiteXmlFiles>   
 <suiteXmlFile>      
src/test/resources/parametrized_test.xml   
 </suiteXmlFile>   
 <suiteXmlFile>     
 src/test/resources/registration_test.xml   
 </suiteXmlFile>
</suiteXmlFiles>

To run the test standalone, we need to have TestNG library in classpath and compiled test class along with XML configuration file java org.testng.TestNG test_suite.xml

Parameterized Tests

Unlike other testing frameworks, TestNG allows to pass parameters to test methods. The snippet of code illustrates this idea:

@DataProvider [1]private static final Object[][] getMoney() {  
  return new Object[][] {        
{new Money(4, “USD”), new Money(3, “USD”), 7},    
  {new Money(1, “EUR”), new Money(4, “EUR”), 5},        
{new Money(1234, “CHF”), new Money(234, “CHF”), 1468}};
}
@Test(dataProvider = “getMoney”) [2]
public void shouldAddSameCurrencies(Money a, Money b,
         int expectedResult) {    
Money result = a.add(b);  
   assertEquals(result.getAmount(), expectedResult);
}

getMoney () method’s purpose is to provide data for actual test method – shouldAddSameCurrencies (). When we run this test, three tests will be executed running with US Dollars, Euros, Swiss Francs. This can be achieved in 2 annotations as,

  • dataProvider attribute of the @Test annotation informs TestNG asking a method,
  • getMoney () method to provide data

Reports

TestNG by default produces an HTML report and a TestNG specific XML file and compatible XML files with Ant JUnitReport task. This is handy for integrating into existing build frameworks, but lacks TestNG specific data if any and also possible to produce custom reports by implementing an IReporter reporter class. 

Tool Support:

TestNG is supported, out-of-the-box or via plug-ins, by each of the three major Java IDEs – Eclipse, IntelliJ IDEA, and NetBeans. Also, TestNG comes with a custom task for Apache Ant and is supported by the Maven build system.

 

Conclusion:

     On this entire overview of TestNG, we have discussed some of the important annotations and respective attributes that are frequently used by testers. However, there are more annotations that are used sometimes when working with groups in the project test script.

It is always advisable not to do project in test method, since it is used to write the core business logic to be tested. And finally make sure that, the system is equipped with Java 1.5 versions or higher to run the annotations.

 
Tags:
Share

Leave A Comment