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

User Friendly Test Next Generation- “TestNG”


What is TestNG?

TestNG is a user-friendly automation testing framework that overcomes the drawbacks and limitations of the java unit (Junit). TestNG is inspired by JUnit and NET platform (NUnit) which uses the annotations (@) and is designed to make end-to-end testing easy.


TestNG helps in generating a proper report where you can easily know how many test cases are passed, failed, and/or skipped. TestNG helps in executing failed test cases separately.


Example

  • Without TestNG: Suppose we have four test cases and four methods written for each test case (Assume that the program is written using the main method without using testNG). When we run this program first, three methods are executed successfully, and the fourth method is failed. Then to correct the errors present in the fourth method, now we want to run only fourth method because first three methods are anyway executed successfully. This is not possible with the main method.

  • With TestNG: The TestNG in Selenium provides an option, i.e., testng-failed.xml file in test-output folder. If you want to run only failed test cases means you run this XML file. It will execute only failed test cases.

Why Use TestNG with Selenium?

Default Selenium tests do not generate a proper format for the test results. Using TestNG in Selenium, we can generate proper test results.

Following are the key features of Selenium TestNG:

  • Generate the report in a proper format including the number of test cases runs, the number of test cases passed, the number of test cases failed, and the number of test cases skipped.

  • Multiple test cases can be grouped more easily by converting them into testng.xml file. In which you can make priorities which test case should be executed first.

  • The same test case can be executed multiple times without loops just by using keyword called ‘invocation count.’

  • Using testNG, you can execute multiple test cases on multiple browsers, i.e., cross browser testing.

  • The TestNG framework can be easily integrated with tools like TestNG Maven, Jenkins, etc.

  • Annotations used in the testing are very easy to understand ex: @BeforeMethod, @AfterMethod, @BeforeTest, @AfterTest

  • WebDriver has no native mechanism for generating reports. TestNG can generate the report in a readable format like the one shown below.


  • TestNG simplifies the way the tests are coded. There is no more need for a static main method in our tests. The sequence of actions is regulated by easy-to-understand annotations that do not require methods to be static.


  • Uncaught exceptions are automatically handled by TestNG without terminating the test prematurely. These exceptions are reported as failed steps in the report.


TestNG Annotations

An annotation is a tag that provides information about the method, class, and suite. It helps to define the execution approach of the test cases and the different features associated with it that aid in the creation of a robust testing framework. Annotations are written above their respective method and prefixed with an at “@” symbol.


Configuration and Test Annotations

In TestNG, we can use the following annotations to do the configuration for our test class, like setup / clean a database, preparing the dummy data, deploy / shut down the server etc. The following lists each configuration annotation:



The example below verifies the title of the Browserstack Home Page. The entire test case has been split into 3 parts.

1. Launching the browser will be the first step and hence it is included under the @BeforeTest Annotation.

2. Next is the actual test case which verifies the title and is therefore included in the @Test annotation.

3. Finally, the quit browser test case is considered under the @AfterTest annotation.


Code Example for the above scenario


On executing the code above, the output looks as follows:


@Parameters

This annotation is used to pass parameters to test methods. And we can pass the parameters values just once while per execution in TestNG.


Example



@DataProvider

This annotation is used for providing any data to the parameterization. Data Providers pass the different parameters on a single test in a single execution.

Example


@Factory

This annotation is used to mark any test method as a factory and returns the object (Object[ ]) that is used as TestNG classes by the TestNG file. @Factory defines and creates tests dynamically at runtime.

Example


In this example we have two different tests with two different data values. TestNG will scan for test methods in these classes and run those tests.


Combination of @Factory and @DataProvider


The @DataProvider feature can also be used with the @Factory annotation for creating tests at runtime. This can be done by declaring the @Factory annotation on a constructor of a class or on a regular method.

Example


Output


Some important attributes of @Test annotations


description

This attribute is a string which is attached to the @Test annotation that describes the information about the test.


Example


groups

This attribute is specifically used to group the different test cases that belong to the same functionality. In the code below, we have declared two Test method with groups attribute. So when want to execute only "Smoke Testcases" we can only run that specific group and get results.


Example:


timeOut

Here we specify the timeout value for the test method(in milliseconds) to execute. If the test takes more than the timeout value specified, the test terminates and is marked as a fail.


Example:

In the above code, we have Thread.sleep(300) which means that the testcase method will be executed after 300 milliseconds, but we have provided timeOut attribute with the value 200 means that the testcase will be failed after 200 milliseconds.


priority

This attribute allows to decide the execution of the method, We can schedule the execution of the method when to run.Lower priority methods execute first and priority work in descending order.When no 'priority' attribute is specified then the TestNG will run the test cases in alphabetical order.


Example:


In the above code, the default priority of mango() test method is 0, so it will be executed first. The watermelon() test method will run after mango() method as the priority of watermelon() test method is 2. The apple() test method has the highest priority, so it will be executed last.


dependsOnMethods

This attribute makes one test method dependent on another to execute. When the second test method wants to be dependent on the first test method, then this could be possible by the use of "dependOnMethods" attribute. If the first test method fails, then the dependent method on the first test method, i.e., the second test method will not run.

As we know Testng executes in alphabetical order, so now APIStudentLogin will execute first. But we want WebStudentLogin to execute first so this can be done only by using "dependsOnMethods" attribute. So now in above code WebStudentLogin executes first and then APIStudentLogin will be executed.

Example:


enabled

This attribute is used if you know that one of the functionality or flow is not working or there is some bug that is already reported and we are aware of that then TestNG provides a feature to safely skip that test case and execute all other test cases. By default, its value is true. If you want to skip some test method, then you need to explicitly specify 'false' value (Boolean Value).


Example:

In the above code, the value of the enabled attribute in ListCart() test method is false, so this method will not be invoked.


Conclusion

TestNG makes automated tests more structured, readable, maintainable, and user-friendly. TestNG is a testing framework that can make Selenium tests easier to understand and of generating reports that are easy to understand. Annotations are easier to use and understand.

40 views0 comments

Recent Posts

See All

Beginner Friendly Java String Interview Questions

Hello Everyone! Welcome to the second section of the Java Strings blog. Here are some interesting coding questions that has been solved with different methods and approaches. “Better late than never!”

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page