Site icon TestingDocs.com

TestNG @Test Annotation Attributes -2

In this tutorial, we will look into more test annotation attributes. This post is a continuation of the earlier post here:    TestNG @Test Annotation Attributes

invocationCount

The number of times this method should be invoked. The default value is 1.

So both the below code snippets are equivalent.

@Test(invocationCount=1) 
 public void testMethod1()
{ 
 System.out.println("count :" + i++); 
 } 


@Test 
public void testMethod2()
{ 
 System.out.println("count :" + i++); 
 }

 

Let's see an example to run a test method for 3 times using invocationCount. Below is the code snippet.
package com.testingdocs.testng.sample;

import org.testng.annotations.Test;

public class TestAnnotationExample
{
 int i = 1;
 @Test(invocationCount=3) 
 public void testMethod()
 { 
   System.out.println("count :" + i++); 
 } 
  
}

 

Run output:

count :1
count :2
count :3

PASSED: testMethod
PASSED: testMethod
PASSED: testMethod

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

The test method runs for 3 times as specified in the invocationCount attribute.

 

invocationTimeOut

The maximum number of milliseconds that the total number of invocations on this test method should take.  This annotation will be ignored if the attribute invocationCount is not specified on this method.If it hasn’t returned after this time, it will be marked as a FAIL.

If you are authoring test class in eclipse and if you make a typo in the attribute , just hover on the attribute  and eclipse would suggest quick fixes as shown below:

 

threadPoolSize

The size of the thread pool for this method.  The method will be invoked from multiple threads as specified by invocationCount. Note that this attribute is ignored if invocationCount is not specified.

dataProvider

The name of the dataProvider for this test method.

dataProviderClass

The class where to look for the data provider.  If not specified, the dataProvider will be looked on the class of the current test method or one of its superclasses. If this attribute is specified, the data provider method needs to be static on the specified class.

For dataProvider example here: TestNG DataProvider Basic Example.

alwaysRun

If set to true, this test method will always be run even if it depends on a method that failed.  This attribute will be ignored if this test doesn’t depend on any method or group.

expectedExceptions

The list of exceptions that a test method is expected to throw. If no exception or a different than one on this list is thrown, this test will be marked a failure.

expectedExceptionsMessageRegExp

If expectedExceptions was specified, its message must match the regular expression specified in this attribute.

singleThreaded

If set to true, all the methods on this test class are guaranteed to run in the same thread, even if the tests are currently being run with a parallel option. This attribute can only be used at the class level and will be ignored if used at the method level.

 

Exit mobile version