How to use TestNG Annotation Attributes

Last updated on Nov 28 2021
Vidhya Prakesh

Table of Contents

How to use TestNG Annotation Attributes

While writing the test cases within the TestNG, you would like to say the @Test annotation before the test method.

@Test
public void testcase1()
{
System.out.println("This is testcase1");
}

In the above code, we’ve specified the @Test annotation before the test method, i.e., testcase1().

We can also explicitly specify the attributes during a @Test annotation. Test attributes are the test specific, and that they are specified at the proper next to the @Test annotation.

@Test(attribute="value")
public void testcase2()
{
System.out.println("This is testcase2");
}

Some of the common attributes are described below:

1 69

  • description
  • timeOut
  • priority
  • dependsOnMethods
  • enabled
  • groups

description

It is a string which is attached to the @Test annotation that describes the knowledge about the test.

Let’s understand through an example.

package com.tecklearn;
import org.testng.annotations.Test;
public class Class1
{
@Test(description="This is testcase1")
public void testcase1()
{
System.out.println("HR");
}
@Test(description="This is testcase2")
public void testcase2()
{
System.out.println("Software Developer");
}
@Test(description="This is testcase3")
public void testcase3()
{
System.out.println("QA Analyst");
}
}

In the above code, we’ve added the outline attribute in every test. The “description” attribute provides information about the test.

Depends On Methods

When the second test method wants to be hooked in to the primary test method, then this might be possible by the utilization of “dependOnMethods” attribute. If the primary test method fails, then the dependent method on the primary test method, i.e., the second test method won’t run.

Let’s understand through an example.

First case: When one value is passed during a parameter.

package com.tecklearn;
import org.testng.annotations.Test;
public class Class1
{
@Test
public void WebStudentLogin()
{
System.out.println("Student login through web");
}
@Test
public void MobileStudentLogin()
{
System.out.println("Student login through mobile");
}

@Test(dependsOnMethods= {"WebStudentLogin"})
public void APIStudentLogin()
{
System.out.println("Student login through API");
}
}

We know that the TestNG executes the test methods in alphabetical order so, within the above program, APIStudentLogin() will execute first. However, we would like WebStudentLogin() method to be executed before the execution of the APIStudentLogin() method, so this is able to only be possible through the “dependsOnMethods” attribute. within the above program, we’ve specified “dependsOnMethods” attribute in an APIStudentLogin() test method and its value is “WebStudentLogin” which suggests that WebStudentLogin() method are going to be executed before the APIStudentLogin() method.

Output

2 68

In the above output, MobileStudentLogin() runs before the WebStudentLogin() method as TestNG runs the test methods in an alphabetical order.

Second case: When multiple values are passed during a parameter.

package com.tecklearn;
import org.testng.annotations.Test;
public class Depends_On_Groups
{
@Test(dependsOnMethods= {"testcase3","testcase2"})
public void testcase1()
{
System.out.println("This is test case1");
}
@Test
public void testcase2()
{
System.out.println("This is test case2");
}
@Test
public void testcase3()
{
System.out.println("This is test case3");
}
}

In the above code, testcase1() depends on two methods, i.e., testcase2() and testcase3(), which suggests that these two methods are going to be executed before the testcase1().

Output

3 67

priority

When no ‘priority’ attribute is specified then the TestNG will run the test cases in alphabetical order. Priority determines the sequence of the execution of the test cases. The priority can hold the integer values between -5000 and 5000. When the priority is about , rock bottom priority test suit will run first and therefore the highest priority test suit are going to be executed last. Suppose we’ve three test cases and their priority values are -5000, 0, 15, then the order of the execution are going to be 0,15,5000. If priority isn’t specified, then the default priority are going to be 0.

Let’s understand through an example.

package com.tecklearn;
import org.testng.annotations.Test;
public class Fruits
{
@Test
public void mango()
{
System.out.println("I am Mango");
}
@Test(priority=2)
public void apple()
{
System.out.println("I am Apple");
}
@Test(priority=1)
public void watermelon()
{
System.out.println("I am Watermelon");
}
}

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

Output

4 61

enabled

 

The ‘enabled’ attribute contains the boolean value. By default, its value is true. If you would like to skip some test method, then you would like to explicitly specify ‘false’ value.

Let’s understand through an example.

package com.tecklearn;
import org.testng.annotations.Test;
public class Programming_languages
{
@Test
public void c_language()
{
System.out.println("C language");
}
@Test(enabled=false)
public void jira()
{
System.out.println("JIRA may be a testing tool");
}
@Test
public void java()
{
System.out.println("JAVA language");
}
}

In the above code, the worth of the enabled attribute in jira() test method is fake , so this method won’t be invoked.

Output

5 56

groups

The ‘groups’ attribute is employed to group the various test cases that belong to an equivalent functionality.

Let’s understand through an example.

package com.tecklearn;
import org.testng.annotations.Test;
public class Software_Company
{
@Test(groups= {"software company"})
public void infosys()
{
System.out.println("Infosys");
}

@Test
public void technip()
{
System.out.println("Technip India Ltd");
}
@Test(groups= {"software company"})
public void wipro()
{
System.out.println("Wipro");
}
}

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<!-- Test -->
<!-- Suite -->

Output

6 47

timeOut

If one among the test cases is taking an extended time thanks to which other test cases are failing. to beat such situation, you would like to mark the test suit as fail to avoid the failure of other test cases. The timeOut may be a period of time provided to the test suit to completely execute its test suit .

Let’s understand through an example.

package com.tecklearn;
import org.testng.annotations.Test;
public class Timeout_program
{
@Test(timeOut=200)
public void testcase1() throws InterruptedException
{
Thread.sleep(500);
System.out.println("This is testcase1");
}
@Test
public void testcaes2()
{
System.out.println("This is testcase2");
}
@Test
public void testcase3()
{
System.out.println("This is testcase3");
}
}

In the above code, inside the testcase1() method, we’ve Thread.sleep(500) which suggests that the testcase1() method are going to be executed after 500 milliseconds, but we’ve provided timeOUT attribute with the worth 200 means the testcase1() are going to be failed after 200 milliseconds.

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<!-- Test -->
<!-- Suite -->

Output

7 39

The above screen shows that one test suit is failed and other test cases are passed.

So, this brings us to the end of blog. This Tecklearn ‘How to use TestNG Annotation Attributes’ blog helps you with commonly asked questions if you are looking out for a job in Selenium and Automation Testing. If you wish to learn Selenium and build a career in Automation Testing domain, then check out our interactive, Selenium Certification Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details

https://www.tecklearn.com/course/selenium-training-certification/

Selenium Certification Training

About the Course

Tecklearn’s Selenium Certification Training enables you to master the complete Selenium suite. The Selenium Training is designed to train developers and manual testers to learn how to automate web applications with a robust framework, and integrate it within the DevOps processes of an organization. This Selenium Certification Training will also help you master important concepts such as TestNG, Selenium IDE, Selenium Grid, Selenium WebDriver, etc. Get hands-on experience on widely used automation frameworks such as Data-Driven Framework, Keyword-Driven Framework, Hybrid Framework, and Behavior Driven Development (BDD) Framework. Throughout this online Instructor-led Selenium Certification Training, you will be working on real-life industry use cases.

Why Should you take Selenium Certification Training?

  • The average salary of a Selenium Test Automation Engineer is $94k per year – Indeed.com.
  • Automation Testing Market is expected to grow at a Compound Annual Growth Rate (CAGR) of 18.0% in the next three years.
  • Global software testing market to reach $50 billion by 2020 – NASSCOM. Selenium tool supports more browsers and languages than any other testing tool.

What you will Learn in this Course?

Getting started with Selenium

  • Introduction to Selenium testing
  • Significance of automation testing
  • Comparison of Manual and Automation Testing
  • Installation of Java JDK, JRE and Eclipse

Setting the environment in Eclipse for Selenium

  • Java Introduction
  • Creating a Java function and executing
  • Concepts of Java
  • Properties File
  • Reading Data from Excel File
  • Database Connection
  • Hands On

Advantages of Selenium automation testing

  • Selenium Features
  • Concept of Selenium Integrated Development Environment
  • Understanding of the Selenium IDE features
  • Addition of Script assertions and general commands
  • Deploying the first Selenium Script
  • Sample project IDE
  • Recording Selenium test case
  • Hands On

Selenium Web driver Automation

  • Architecture of Selenium Web Driver
  • Download and installation
  • Creating a Java function using Selenium and execution
  • Hands On

Deploying Web Drivers for scripting

  • Getting the HTML source of Web Element
  • Table and Form Elements
  • Firebug extension and Fire Path installation
  • Advance User Interactions and Cross Browser Testing
  • Hands On

Deep dive into Selenium Web Driver

  • Action Commands
  • Web Table / Date Picker
  • How to Implement Switching Commands in WebDriver
  • Alerts
  • Frames
  • Hands On

Switching Operations in WebDriver using Window

  • Selenium Webdriver Wait
  • Implicit wait, Explicit wait
  • Deploying searching elements using the link text, name, using XPath
  • Calendar
  • Hands On

Introduction to TestNG Framework

  • Introduction to TestNG
  • Advantages of TestNG
  • Installing TestNG on Eclipse
  • Rules to write TestNG
  • TestNG Features
  • Annotations
  • Grouping
  • Sequencing: Prioritization and Dependency
  • Enable/Disable a test case
  • Parameterization: Using Xml file and DataProvider
  • Parallel Testing & Cross Browser Testing
  • TestNG Report: HTML Report, Console Report, XML Report

JUnit Operations and Test Framework

  • Annotations, Methods in JUnit
  • Junit Test Suites, ANT Build and JUNIT reporting
  • Types of Test Automation Framework
  • Module Based Testing Framework
  • Data Driven Testing Framework
  • Keyword Driven Testing Framework
  • Hybrid Driven Testing Framework
  • How to implement Testing Framework in Project

Object Repository

  • Understanding of Object Repository
  • Learning sample scripts using object repository
  • Page Object Modelling
  • Page Factory

JavaScript Functions

  • Autosuggestion
  • Headless Browser
  • Sikuli
  • XPath

Got a question for us? Please mention it in the comments section and we will get back to you.

 

 

 

 

 

0 responses on "How to use TestNG Annotation Attributes"

Leave a Message

Your email address will not be published. Required fields are marked *