Software Testing

TestNG @Test Annotation

TestNG @Test Annotation

@Test is the basic annotation of the TestNG framework. We can mark a class or a test with this annotation as part of TestNG tests. If we apply the @Test at the class level all the public methods inside the class are marked as test methods for the TestNG tests. Test annotation has a lot of attributes that we can use along with the annotation, as shown in the below picture.

@Test

We will discuss some things about Test annotation in this post.

TestNG @Test Annotation Attributes

Import Test annotation in a test class.

Hover on the @Test annotation and import as shown.

 

Import TestNG annotation

 

description

The description of the method. The string will appear in the HTML report and on the standard output if verbose >= 2.

Code snippet:

package com.testingdocs.testng.sample;

import org.testng.annotations.Test;

public class TestAnnotationExample
{
  @Test(description="SampleDescription")
  public void sampleTestName()
  {
    System.out.println("Sample Test");
  }

}

 

Run output:

PASSED: sampleTestName
SampleDescription

===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================

groups

The list of groups the class/method belongs to.

@Test(groups="group1")
 public void test1()
 {  
  System.out.println("Group1 test");
 }

 

Consider reading : TestNG Test Groups

enabled

Specifies whether methods on the class/method are enabled or disabled.

dependsOnGroups

The list of groups the method depends on.  Every method member of one of these groups is guaranteed to have been invoked before this method.  Furthermore, if any of these methods were not a SUCCESS, this test method would not be run and would be flagged as a SKIP.

dependsOnMethods

The list of methods the method depends on.  There is no guarantee on the order in which the methods depended upon will be run, but you are guaranteed that all these methods will be run before the test method that contains this annotation is run.  Furthermore, if any of these methods was not a SUCCESS, this test method would not be
run and will be flagged as an SKIP.

If some of these methods have been overloaded, all the overloaded versions will be run.

timeOut

The maximum number of milliseconds the test should take. It will be marked as a FAIL if it hasn’t returned after this time.