top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

TestNG : Where test automation made easy!!

TestNG is an open-source Java testing framework, inspired by JUnit and NUnit. 'NG' in TestNG stands for "Next Generation". Before TestNG, though we have JUnit, we were struggling with handling sequencing, grouping and parametrizing in test cases for different scenarios.


To over come such problems and be a handy tool specific for testing, TestNG was introduced. This is a platform that is a combination of programs, compilers, features, tools and more. It provides an environment where you can execute automated test scripts.


Why do you need TestNG with Selenium WebDriver for automation testing?


As Selenium do not have any built-in hardware or framework for generating test reports, you require the help of an external framework such as TestNG to fulfill the purpose of generating test reports and simplifying testing requirements like functional, regression , end-to-end testing and more.


Basic functionalities of TestNG framework are

  • Create and Execute Test Scripts

  • Generate a Test Report

  • Generate Logs

  • Read and write Test Data

Advantages of TestNG over JUnit:

  • TestNG Annotations are used to create test cases easily

  • Test cases can be grouped, prioritized and executed

  • Supports parameterization

  • Supports data-driven testing using Data Providers

  • Can generate HTML test reports of the results representing like number of test cases ran, failed or the number of test cases skipped

  • Supports integration with Maven

  • Supports parallel execution

  • Logs can be generated

Installation and downloading TestNG framework:

  • In the Eclipse IDE go to Help and then to eclipse marketplace.

  • In the marketplace search for TestNG.


  • If TestNG is not installed in your Eclipse, instead of ‘Installed’ button you will see ‘install’. Click on install and your TestNG framework will be installed in your Eclipse.

  • Post-installation, restart your Eclipse, verify whether you can see options for creating a TestNG class by right clicking the on project in the project explorer and see option of it.


What are TestNG Annotations?


An annotation is a tag that provides information about the method, class, and suite. It helps to define the execution approach of your test cases and the different features associated with it. They are written above their respective method and prefixed with an at “@” symbol. We can place an Annotation anywhere on the editor because it’s automatically connected to the method.


Below are the major annotations used:


@BeforeSuite: This executes even before any of the member in the suite is executed.

@BeforeTest: This executes before first @Test annotated method runs.

@BeforeClass: This executes even before the first method in the class begins.

@BeforeMethod: This executes before every @test annotated method.

@Test: Marks a class or a method as a part of the test.

@AfterMethod: This executes after every @test annotated method.

@AfterClass: This executes after all the test methods in the current class have ran.

@AfterTest: A method which is marked with this annotation will be executed when all @Test annotated methods are completed the execution.

@AfterSuite: This executes at the end of all executions done in the suite.


Talking about the execution order of these annotations, they execute in the below order:


@BeforeSuite -> @BeforeTest -> @BeforeClass -> @BeforeMethod -> @Test -> @AfterMethod -> @AfterClass -> @AfterCTest -> @AfterSuite


All other annotations run only once, whereas the @BeforeMethod and @AfterMethod run for each @Test method.


Example for the annotations is as follows

Output for above code is


@DataProvider Annotation:


The @DataProvider annotation is used to define a method that returns an array of objects. Each object in the array represents a set of test data.


@DataProvider(name = "testData")

public Object[][] testData() {

return new Object[][] {

{ "John", 25 },

{ "Mike", 30 },

{ "Alice", 20 } };

}


Data Provider with parameters:


You can also provide parameters to the data provider method using the @DataProvider annotation. The parameters can be passed to the test method using the @Test annotation.


Example of @DataProvider annotations is as follows

Output of above program is as follow:


Groups in TestNG:


One of the key features of TestNG is the ability to group test cases and execute them based on their grouping.


TestNG allows developers to group test cases by using the @Test annotation with a parameter called "groups". By assigning a test case to one or more groups, developers can later execute all test cases in a particular group or exclude test cases from execution based on group membership.


Here's an example of grouping test cases in TestNG:


In this example, we have three test cases - test1, test2, and test3 each assigned to one or more groups. Test1 is assigned to both "group1" and "group2", test2 is assigned to "group1", and test3 is assigned to "group2".


To execute test cases based on their groups, we can use the "groups" attribute in the TestNG XML file:

Output of groups will be will be as follows:


Assertions and reports in TestNG:


TestNG is a popular testing framework for Java that provides a variety of features for creating and running tests. Assertions and reports are two important features of TestNG that help in ensuring the quality and reliability of the software being tested.


Assertions are statements that are used to verify the expected results of a test case. They are used to compare the actual value of a test result with the expected value. If the actual value matches the expected value, the test case passes, and if not, the test case fails.


TestNG provides several types of assertions, such as


assertTrue()

assertFalse()

assertEquals()

assertNotEquals()


These assertions help in validating the behavior of the software being tested and in identifying defects.


Reports are used to provide information about the test results. TestNG provides several types of reports, based on HTML, XML and emailable reports. These reports help in identifying the tests that are passed or failed, time taken for each test, and overall status of the test suite. Reports can also be customized to provide additional information such as screenshots and logs to help in debugging failed test cases.


TestNG provides several built-in listeners that can be used to generate reports automatically. These listeners include the TestListenerAdapter, ITestListener, and IReporter. TestNG also provides the ability to create custom listeners to generate reports with specific requirements.


In summary, assertions and reports are two important features of TestNG that help in ensuring the quality and reliability of the software being tested. Assertions help in validating the behavior of the software, while reports provide information about the test results.


Here is an example of TestNG program that demonstrates the use of assertions and reports:


Output of assertions and reports is as follows

In this program, there are two test methods - testAddition() and testSubtraction(). Each of these methods performs a simple arithmetic operation and compares the actual result with the expected result using an assertion (Assert.assertEquals()). If the assertion fails, an error message is printed to the console.


In addition to the assertions, the program also uses the Reporter.log() method to generate a report for each test method. The report indicates whether the test has been passed or failed.


When this program is executed using TestNG, it will generate a default HTML report that shows the test results. The report will indicate the number of tests passed or failed, the time taken to execute each test and any error messages generated by failed assertions.


Below is an example of html report


Prioritize in TestNG:


In TestNG, we can prioritize the order in which tests are executed using the priority attribute. The tests with lower priority values will be executed first, and then the higher priority tests will be executed in ascending order.


Here's an example of how to use the priority attribute in TestNG:

In this example, we have three test methods, testMethod1, testMethod2 and testMethod3. testMethod1 has the lowest priority value of 1, so it will be executed first, followed by testMethod2 with a priority of 2, and finally testMethod3 with a priority of 3.


If we run this test class using TestNG, we should see the following output:


Parallel execution TestNG:


TestNG allows us to run tests in parallel to save time and increase efficiency. There are two ways to achieve parallel execution in TestNG:


Parallel tests at the class level:

In this approach, TestNG runs the test methods of each class in parallel. We can specify the number of threads to be used for parallel execution using the thread-count attribute in the suite tag of the testng.xml file.


In this example, we have specified that the test suite should run tests in parallel at the class level with two threads. So, module1 and module2 will be run in parallel with each thread of its own.


Note that parallel execution may cause interference between test methods, so we need to ensure that our tests are designed to run independently without any dependencies on other tests. Additionally, we need to make sure that our test environment can support parallel execution, especially in terms of resource usage such as CPU, memory, and network bandwidth.


In this output we can see the time, when the test cases are executed parallelly in two different classes.


127 views1 comment
bottom of page