TestNG Tutorial | Selenium with TestNG | Annotations |

TestNG Selenium WebDriver

TestNG stands for Test Next-Generation inherited from JUnit. TestNG plays an important role while developing an automation Framework. TestNG is a testing framework with various features as grouping of test cases, Report, annotations, parameterization, data provider, TestSuite, etc. It will help you in many ways to automate your sites in a simple way. All the features of Junit are included in TestNG. You have control over your test cases and their execution through TEstNg. TestNG tests unit testing, functional, integration, end-to-end testing. Multiple test cases can be executed in a single click at a time by using TestNG.

Advantages –

Default Reporting, Annotations, Support for Data-Driven Testing — @DataProvider

You can install TestNG in your Eclipse IDE in two ways:

  1. Jar Installation :

Here, in this method, you can simply download and install a jar file in your project. You will get the jar from https://mvnrepository.com/ this site. Here you can search for TestNg and select the version as you want. After Download you need to install it in your project. For this, first, go to eclipse and right-click on your project where you want to install the jar. Go to Build Path → Configure Build Path → Go to Libraries → Add External Jars → Select downloaded jar→ Open→ Apply→ OK. Once refresh the project so the changes will be displayed.

  1. Plugin Installation:

Here, in this method, user can directly install TestNG plugin in Eclipse. To install TestNG plugin you need to go to Eclipse window’s Help Menu → Eclipse Marketplace → window will open. here you can search for TestNg plugin in Search box and click Go. → You will appear TestNG plugin click on Install. Here sometimes it takes time for installation. Click on Confirm. Here, may ask to restart the PC. Go for it and you are good to go with TestNG.

To check whether it is installed or not go to Window → Preferences → search for TestNG → If it is installed then, you will able to see TestNg there otherwise not.

The main difference between these two methods is that Jar installation applies to only one associative project, and Plugin Installation will apply to all projects in your associated workspace that mean no need to install jar every time you need TestNG.

As like regular test cases here, there is no need for main() function to run the test cases. TestNG itself acts as a Java compiler. TestNg Runner will test the test case. TestNg needs all its steps in a form of a method.

Annotations describe the functionality to be performed. Annotations declare with ‘@’ symbol follows by annotation name. By default, methods execute in a alphabetical order or if you want any specific order to be followed by Test cases then you can give priority as parameter.

There are multiple Annotations provided by TestNg:

  1. @Test: It tells the test to be execute or a method to be execute.
  2. @BeforeSuite: Method with @BeforeSuite annotation runs before all the tests in the current test suite.
  3. @AfterSuite: Method with @AfterSuite annotation runs after all the tests in the current test suite.
  4. @BeforeTest: Method with @BeforeTest annotation runs before execution of all the test cases inside that folder or method.
  5. @AfterTest: Method with @AfterTest annotation runs after the execution of all the test cases

Inside that folder or method.

  1. @BeforeClass: Method with @BeforeClass annotation runs before the execution of the first test method in a class.
  2. @AfterClass: Method with @AfterClass annotation runs after execution of all the test methods in the current class.
  3. @BeforeMethod: Method with @BeforeMethod annotation runs before every test method in a class.
  4. @AfterMethod: Method with @AfterMethod annotation runs after every test method in a class.
  5. @BeforeGroups: Method with @BeforeGroups annotation runs before the execution of test cases in that group.
  6. @AfterGroups: Method with @AfterGroups annotation runs after the execution of test cases in that group.

Basically, Priority is the sequence in which test cases to be executed. In TestNG

By default test cases executes in alphabetical order. If user needs to change the sequence then by using Priority he can change.

Syntax: @Test(Priority = 2)

Let’s see in example:

package testTestNG;

import org.testng.annotations.Test;

public class Demo

{

@Test(priority=3)

public void Test1()

{

System.out.println(“This is Test 1”);

}

@Test(priority=1)

public void Test2()

{

System.out.println(“This is Test 2”);

}

@Test(priority=2)

public void Test3()

{

System.out.println(“This is Test 3”);

}

}

Here is the output of the above program:

To run the TestNG test cases, right-click on class or on eclipse window and click TestNG Test. It will show you the result in the console. It gives you results as Total tests run, Failures test cases, Skips test cases, list of passed test cases, etc.

Description as in name gives you the description or information about your test case. It will show results in Console and in HTML report. It helps to understand what this test case is about and what it’s work.

Ex. @Test

Public void demo(description=” This test case runs demo.”)

{

System.out.println(“This is the demo test case.”)

}

You can add multiple parameters by passing comma(, ) in between two parameters.

That’s all for this post. We will share new thoughts on this again.

Visit here- https://techieshub9.com/ for more updates..

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
TechiesHub9

TechiesHub9

Blogger, Software Development, Software Testing