top of page
Writer's pictureStalin and Nija Bose

Learn About TestNG Framework

Introduction

TestNG is a user-friendly automation framework that overcomes the drawbacks and limitations of the java unit.

TestNG framework eliminates the limitations of the older framework by providing more powerful and flexible test cases with help of easy annotations, grouping, sequencing and parametrizing.


TestNG Framework

  • TestNG is a testing framework designed for unit testing, nowadays it is used for every kind of testing.

  • It is an open-source framework which is inspired by the Java platform (JUnit) and NET platform (NUnit).

  • It is introduced as a brand-new automated framework whose functionalities make it more powerful and easier to use.

Why Use TestNG with Selenium?

One of the drawbacks of Selenium is that it does not have a proper format for the test results. By using TestNG framework in Selenium, We can:

  • Generate the report in a proper format.

  • Include the number of test cases run; tests passed,ed, and skipped in the report.

  • Group test cases by converting them to testing.xml.

  • Use invocation count and execute multiple tests without using loops.

  • Perform cross browser testing.

  • Easily understand annotations.

The TestNG Framework Structure








































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. Below are the major annotations used:

@Test– This is the root of TestNG test cases. In order to use TestNG, all methods should be annotated with this annotation. Below is an example:









TestNG Annotations

Following is the list of the most useful annotations in TestNG:


@Test

The annotation notifies the system that the method annotated as @Test is a test method

@BeforeSuite

The annotation notifies the system that the method annotated as @BeforeSuite must be executed before executing the tests in the entire suite

@AfterSuite

The annotation notifies the system that the method annotated as @AfterSuite must be executed after executing the tests in the entire suite

@BeforeTest

The annotation notifies the system that the method annotated as @BeforeTest must be executed before executing any test method within the same test class

@AfterTest

The annotation notifies the system that the method annotated as @AfterTest must be executed after executing any test method within the same test class

@BeforeClass

The annotation notifies the system that the method annotated as @BeforeClass must be executed before executing the first test method within the same test class

​@AfterClass

The annotation notifies the system that the method annotated as @AfterClass must be executed after executing the last test method within the same test class

@BeforeMethod

The annotation notifies the system that the method annotated as @BeforeMethod must be executed before executing any and every test method within the same test class

@AfterMethod

The annotation notifies the system that the method annotated as @AfterMethod must be executed after executing any and every test method within the same test class

@BeforeGroups

The annotation notifies the system that the method annotated as @BeforeGroups is a configuration method that enlists a group and that must be executed before executing the first test method of the group

​@AfterGroups

The annotation notifies the system that the method annotated as @AfterGroups is a configuration method that enlists a group and that must be executed after executing the last test method of the group


Here is an example where I have used Annotations in my code.



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

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

TestNG Assertions

Assertions are used to perform various kinds of validations in the test cases, which in turn helps us to decide whether the test case has passed or failed. We consider a test as successful if it runs without any exception.

Here, we would discuss two types of assertions.

Hard Assertions: We call general assert as Hard Assert, A hard assertion does not continue with execution until the assertion condition is meet.Hard assertions usually throw an Assertion Error whenever an assertion condition has not been met. The test case will be immediately mark as Failed when a hard assertion condition fails.


The disadvantage of Hard Assert – It marks the method as fail if assert condition gets failed and the remaining statements inside the method will be aborted.To overcome this we need to use Soft Assert. Let’s see what is Soft Assert.


Soft Assertion: A soft assertion continue with the next step of the test execution even if the assertion condition is not meet. Soft Assertion is the type of assertion that does not throw an exception automatically when an assertion fails unless it is asked for. This is useful if you are doing multiple validations in a form, out of which only a few validations directly have an impact on deciding the test case status.

TestNG Assertions

Like JUnit, TestNG provides multiple level assertions to validate your actual results against your expected results. Few of the commonly used assertions are:

  1. assert True– This assertion verifies whether the defined condition is true or not. If true, it will pass the test case. If not, it will fail the test case.

Assert.assertTrue(condition);


Here is the example where 'AssertTrue' method is used to check the code.









2. assert False– This assertion verifies whether the defined condition is false or not. If false, it will pass the test case. If not, it will fail the test case.


Assert.assertFalse(condition);


3.assertEquals– This assertion compares the expected value with the actual value. If both are the same, it passes the test case. If not, it fails the test case. You can compare strings, objects, integer values etc. using this assert.


Assert.assertEquals(actual,expected);


4.assertNotEquals: This is just opposite to what assertEquals does. If actual matches the expected, the test case fails, else the test case passes.


Assert.assertNotEquals(actual,expected,Message);


Importance of XML file in TestNG Configuration


In TestNG, we can define multiple test cases in a single class whereas, In Java, we can define only one test in a single class in the main() method. In Java, if we want to create one more test, then we need to create another java file and define the test in the main() method.

Instead of creating test cases in different classes, we recommend to use TestNG framework that allows to create multiple test cases in a single class.

we can create multiple test cases with the help of @Test annotation

Testng.xml file looks as shown below,





Advantages of TestNG over JUnit

  1. Advantages of TestNG over JUnit to run out same test case with multiple set of test.

  2. It provides default reports which are not provided if your using JUnit

  3. It has higher level of customization which enables the user to customize test case execution so that the user can run his script when something happens.

  4. Test cases can also be grouped easily as compared to JUnit.

  5. The Annotations used in TestNG are very simple and easy to understand.


Conclusion

TestNG makes automated tests more structured, readable, maintainable, and user-friendly. It provides powerful features and reporting. We implemented and executed our TestNG test script using annotations and assert statements.

Thanks for reading!!








99 views

Recent Posts

See All
bottom of page