Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
December 14, 2016

TestNG Test Annotation





The most important annotation of TestNG is Test annotation. This annotation marks a method or class as TestNG test. It you apply at class level then it will mark all the public methods present inside the class as test methods. It had lot of attributes which you can use along with the Test annotation, which will enable you to use the different features provided by the TestNG unit test framework.

Below are the list of attributes which we can use along with the Test annotation:

AttributeDescription
alwaysRun
Takes a true or false value. If set to true this method will always run even if it's depending method fails.
dataProvider
The name of the data provider, which will provide data for data-driven testing to this method.
dataProviderClass
The class where TestNG should look for the data-provider method mentioned in the dataProvider attribute. By default it's the
current class or its base classes.
dependsOnGroups
Specifies the list of groups this method depends on.
dependsOnMethods

Specifies the list of methods this method depends on.
description
The description of this method.
enabled
Sets whether the said method or the methods inside the said class should be enabled for execution or not. By default its value is true.
expectedExceptions
This attribute is used for exception testing. This attribute specifies the list of exceptions this method is expected to throw.
groups
List of groups the said method or class belongs to.
timeOut
This attribute is used for a time out test and specifies the time (in millisecs) this method should take to execute.

Will learn about all these attributes in the coming blogs.

We already seen sample tests using Test annotation on methods, will see how can we use Test annotation at class level in the below example:


import org.testng.annotations.Test;

@Test
public class ClassLevelTestAnnotation
{
    public void firstTest()
    {
        System.out.println("In First Test");
    }
    
    public void secondTest()
    {
        System.out.println("In Second Test");
    }
    
    public void thirdTest()
    {
        System.out.println("In Third Test");
    }
}

Run the above code using below xml configuration file:

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

Output for the above code is:

TestNG Test Annotation output Image

We can observe from the above is, we did not added the Test annotation to any of the methods in the class. Instead, we have put the Test annotation at the class level, then this will be applicable to all the public methods in the class.



Share this post: on Twitter on Facebook

Enabling and Disabling TestNG Test TestNG Exception Test

Related Posts

Excel to DataProvider

TestNG

Read data from Excel to DataProvider in Selenium

Running TestNG programmatically

TestNG

Running TestNG Tests Programmatically

EXECUTING ONLY FAILED TESTS

TestNG

Executing Only Failed Tests in TestNG

Capture Screenshot for Failed Tests

TestNG

Capture Screenshot for Failed Tests in TestNG

Preserve Order in TestNG

TestNG

Preserve Order in TestNG

PRIORITIZING TESTS

TestNG

Prioritizing Tests in TestNg

EXECUTE MULTIPLE XML FILES

TestNG

Execute Multiple XML files in TestNG

CUSTOM REPORTER IN TESTNG (1)

TestNG

Custom Reporter in TestNG

CUSTOM LOGGER IN TESTNG

TestNG

Custom Logger in TestNG

ASSERTIONS

TestNG

TestNG Assertions

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2025