What is TestNG in Selenium?

TestNG in selenium is a testing framework inspired from J Unit and N Unit. In TestNG-NG stands for Next Generation. TestNG is designed to cover all categories of tests: unit, functional, end-to-end, integration, etc. Testng.xml is a file that contains the test configuration, and it allows us to organize test classes and define test suites and tests.

TestNG in Selenium

Some functionalities that make it more powerful and easier to use testng in selenium, such as:

1) Annotations.

2) Run our tests in arbitrarily big thread pools with various policies available (all methods in their own thread, one thread per test class, etc…).

3) Flexible test configuration.

4) Support for data-driven testing (with @DataProvider).

5) Support for parameters.

6) Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc…).

7) Default JDK functions for runtime and logging (no dependencies).

8) Dependent methods for application server testing.

Advantage of TestNG in Selenium

1. It generates the HTML reports for implementation.

2. Test Cases Can be Grouped & Prioritized more easily.

3. Parallel testing is possible.

4. Data parameterization possible

Installation of TestNG in Eclipse

Steps 1: Navigate to Help-> Eclipse Marketplace.

Steps 2: Type “TestNG” in “Find” test box and click on “Search” icon. It will search and show you TestNG.

Steps 3: If TestNG is installed, it will show you “Installed” else “Install”. Click on “Install” and it will be installed.

Steps 4: Click on “Confirm” button.

Steps 5: Click on radio button of “I accept the term of the licence agreement.” and click on “Finish” button.

Steps 6: You will get below warning message. Click on “Install anyways”.

Steps 7: Restart Eclipse.

We are done with the installation of TestNG Plugin in Eclipse now.

Creating a TestNG class in Eclipse:

Steps 1: – Select the menu File-> New->Other-> TestNG or Create a plain java class.

Steps 2: -Create methods.

Steps 3: -Add annotation as “@Test” each of the methods.

Steps 4: -Run as a TestNG suite.

Note: –

A class is called TestNG class if it has annotations provided by TestNG. TestNG provides an annotation called “Test” which we need to use for any method. When we add this “Test” annotation, we don’t Require to include “public static void main (String [] args)” method in our class. Main method is starting point of execution of a java program. But when We add “Test” annotation with methods, JVM run those methods directly. Method count always depends upon @Test annotation. Not on method Count.

Whenever, we execute TestNG Class, we get 3 types of results-

1) Java Console Result

2) TestNG Console Result

3) HTML Report For Execution

TestNG Test Case Priority: –

In TestNG “Priority” is used to schedule the test cases. When there are multiple test cases, we want to execute test cases in order. Like First we need to execute a test case “Registration” before login. In order to achieve, we use need to add annotation as @Test(priority=??). The default value will be zero for priority. If we don’t mention the priority, it will take all the test cases as “priority=0” and execute. Priority should be an integer value.

It can be negative, zero or positive number. If we write it is decimal, we must need to cast it into integer. TestNG will execute test methods from lowest to highest priority. Lower priorities will be scheduled first. Test methods without priority will have default priority of Zero and execution sequence will be decided based on ASCII value [A= 65, Z=90 & a=97, Z=122].

TestNG ignore default priority based on ASCII if priority value is provided. If we pass duplicate priority to test methods. In that case, TestNG will decide priority based on ASCII value. We can create a TestNG class with some test methods with priority and some without priority in same class [Sequence-> – Ve Priority-> 0 Priority-> some without priority -> +Ve Priority]. We cannot pass priority to methods through testing.xml.

What is the Sequence of Execution of all TestNG annotations? The sequence of execution of all annotations in TestNG is as follows: @BeforeSuite @BeforeTest @BeforeClass @BeforeMethod @Test @AfterMethod @AfterClass @AfterTest @AfterSuite.

Please note:

1) A testng xml can have single <suite> tag.

2) You can have multiple <test> tags in a <suite> tag.

3) You can have single <classes> tag in a <test> tag.

4) You can have multiple <class> tag inside a <classes> tag.

5) You can have multiple test method (@Test annotated) names inside <methods> tag inside a <class> tag

@BeforeSuite -This annotated method will run before the execution of all the test methods in the suite.

@AfterSuite-This annotated method will run after the execution of all the test methods in the suite.

@BeforeTest– This annotated method will be executed before the execution of all the test methods of available classes belonging to that folder.

@AfterTest– This annotated method will be executed after the execution of all the test methods of available classes belonging to that folder.

@BeforeClass– This annotated method will be executed before the first method of the current class is invoked.

@AfterClass– This annotated method will be invoked after the execution of all the test methods of the current class.

@BeforeMethod– This annotated method will be executed before each test method will run.

@AfterMethod– This annotated method will run after the execution of each test method.

@BeforeGroups– This annotated method run only once for a group before the execution of all test cases belonging to that group.

@AfterGroups– This annotated method run only once for a group after the execution of all test cases belonging to that group.

Use of Invocation Count in TestNG: –

By using of invocation Count, we can execute a test multiple time. Invocation count determines how many times test will execute.

The keyword invocationCount = <int> is required.

@Test (invocationCount = 10)

ThreadPoolSize: –

The threadPoolSize attribute tells TestNG to create a thread pool to run the test method via multiple threads. With thread pool, it will greatly decrease the running time of the test method.

@Test (invocationCount=3, threadPoolSize=3)

ThreadCount: –

It’s the number of tests that are run at the same time.

Create a Suite in TestNG: –

Suite is a collection of test cases. In order to create a suite in TestNG, right click on a project or package. In the context menu go to TestNG and Select “Convert to TestNG”. By default, the suite name will be TestNG.xml. [We can change the suite name also] and then click on finish. In order to execute TestNG suite, right click on suite.xml file(Name of suite file) and run as TestNG Suite.

Enable and Disable Test Cases in TestNG: –

If the enabled=true then the test will execute and if enabled=false then the test will not execute.

Program: –

public class EnableDisableEx1 {

@Test

public void createAccount () {

System.out.println (“create Account”);

}

@Test(enabled=true)

public void updateAccount () {

System.out.println (“update Account”);

}

@Test(enabled=false)

public void deleteAccount () {

System.out.println (“delete Account”);

} }

Output: –

create Account update Account

Enable and Disable Test Cases in TestNG By Using Testing.xml file: –

Disable and enable particular tests in a class using include and exclude tags from testng.xml file.

Groups In TestNG: –

TestNG allows us to perform sophisticated groupings of test methods. Using TestNG we can execute only set of groups while excluding another set. This gives us the maximum flexibility in divide tests and doesn’t require us to recompile anything if we want to run two different sets of tests back-to-back.

Groups are specified in testng.xml file and can be used either under the or tag. Groups specified in the tag apply to all the tags underneath. We group test cases based on modules name, suite name etc. We group test cases in Smoke, Sanity, Regression suites etc and whenever required we execute those suites.

Suppose if we need to execute test cases of module “UserAccess”, we find out test cases which belong to group “UserAccess” and run it. we can decide easily what to run and not to run.

Program: –

public class GroupingEx1 {

@Test (groups= {“smoke”,”regression”,”UAT”})

public void createAccount () {

}

@Test (groups= {“smoke”,”UAT”})

public void updateAccount () {

}

@Test (groups= {“regression”,”UAT”})

public void deleteAccount () {

}}

XML File

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE suite SYSTEM “https://testng.org/testng-1.0.dtd”>

<suite name=”Suite”>

<test thread-count=”5″ name=”Test”>

<groups>

<run>

<include name=”smoke” ></include>

<exclude name=” regression “></exclude>

<exclude name=” UAT”></exclude>

</run>

</groups>

<classes>

<class name=”org.testNGSuitEx.GroupingEx1″ />

</classes>

</test> <!– Test –>

</suite> <!– Suite –>

We will execute the group “Smoke” which will execute the test methods which are defined with group as “Smoke”

How To Execute Groups In TestNG Without Using TestNG XML:-

When we assign group or groups to test methods in TestNG, to include or exclude groups to run we use TestNG xml file. We can run groups without using TestNG xml file and can include, exclude groups easily compare to updation in TestNG xml file. Suppose we created two packages and one class in each package. Each class contains three methods which belong to different groups.

Now we will run these groups without creating testing. Xml file. Steps are as follows:

Step 1: Navigate to Run-> Run Configurations:

Step 2: Click on TestNG

Step 3: Select project:

Step 4: After selecting project, you will see “Groups” under Run header. Click on radio button of “groups”.

Step 5: Click on “Browse” button, you will see all group names there from all packages of project:

Step 6: Select groups which you want to run and click on Ok button:

Step 7: Click on Apply – > Run

Parameter in TestNG: –

TestNG allows us to create parameterized methods in a TestNG class. A TestNG class may contain more than one @Test annotated methods and every test method may accept zero or more parameters. We can pass those parameters as a whole at “suite” level. Parameters is a great functionality as it eliminates the need of hard coded data within script. Using testng.xml we can provide data to parameters.

Parameter’s annotation is used to read parameters from testng.xml for methods in a testng class. Attribute names provided in Parameter’s annotations must be same as parameters name provided in testng.xml. Attributes are mapped in order as it appears in Parameter’s annotation with method arguments. For example: First attribute in Parameter’s annotation is mapped to first attribute in method and so on.

Suppose you need to login to your application with different users with different roles. You cannot create multiple methods for each user with role type. Instead of that you can create a parameterized methods which will accept user type and login as required.

You may need to connect different databases and each database may have different credentials. This can be achieved easily using parameterized methods. Please note we must need to run from testng.xml file otherwise we will get injection exception again. Terms Parameters and parameter in TestNG.

Parameters” is an annotation which allows us to create parameterized methods in a TestNG class.

parameter” is a tag which is used to pass values to parameterized methods of a TestNG class.

Parallel Execution in TestNG: –

Parallel testing or parallel execution is a process of running the test case parallelly rather than one after the other. Running the tests in parallel reduces the overall execution time. Using the parallel execution in TestNG, we can allow multiple threads to run simultaneously on the test case providing independence in the execution of different components of the software.

Parallel Execution of Classes in TestNG

TestNG provides an ability to run test classes in parallel. By using parallel execution of classes, each class will be started and executed simultaneously in different threads.

<suite name=”My suite” parallel=”classes” thread-count=”5″>

Parallel Execution of methods in TestNG

TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.

<suite name=”My suite” parallel=”methods” thread-count=”5″>

Parallel Execution of test Tag in TestNG

TestNG will run all the methods in the same <test> tag in the same thread, but each <test> tag will be in a separate thread.

This allows you to group all your classes that are not thread safe in the same

<test> and guarantee they will all run in the same thread while taking advantage of TestNG using as many threads as possible to run your tests.

<suite name=”My suite” parallel=”tests” thread-count=”5″>

Data Provider in TestNG: –

Data Provider is an annotation to mark a method as data provider which provides data as an array of array of Objects which can be used for test or configuration method of a testng class to run on. We can execute a test method for multiple set of data using Data Provider which is not possible though Parameter’s annotation where we pass values from testng.xml.

If we are using Data Provider, we do not require testng xml to run it as we need to do in case of Parameters annotation. You need to use it as a normal annotation with any name (Optional). For E.g. @Test(dataProvider=”testData”) on top of method signature.

A DataProvider method can have a name so that it can be used to supply data to test methods by just mentioning its name. If we do not provide name to a DataProvider method, we can call it with method name on which it was used. Return type of DataProvider annotation is an array of array of objects.

Other Popular Articles

How to use AutoIt in Selenium?

Leave a Comment