31
Mar

Understand failurePolicy in TestNG

If a TestNG method fails, subsequent methods are skipped. eg.

import org.testng.Reporter;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.lang.reflect.Method;

public class SampleTestClassWithMultipleMethods {
@BeforeMethod
public void beforeSampleTestClassWithMultipleMethods(Method method) {
if (“a”.equalsIgnoreCase(method.getName())) {
throw new RuntimeException(“throwing exception for SampleTestClassWithMultipleMethods”);
}
}

@Test
public void a() {
Reporter.log(getClass().getSimpleName() + “.a()”);
}

@Test
public void b() {
Reporter.log(getClass().getSimpleName() + “.b()”);
}
}import org.testng.Reporter;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.lang.reflect.Method;

public class SampleTestClassWithMultipleMethods {
@BeforeMethod
public void beforeSampleTestClassWithMultipleMethods(Method method) {
if (“a”.equalsIgnoreCase(method.getName())) {
throw new RuntimeException(“throwing exception for SampleTestClassWithMultipleMethods”);
}
}

@Test
public void a() {
Reporter.log(getClass().getSimpleName() + “.a()”);
}

@Test
public void b() {
Reporter.log(getClass().getSimpleName() + “.b()”);
}
}

Output:
——-

FAILED CONFIGURATION: @BeforeMethod beforeSampleTestClassWithMultipleMethods(public void com.TestNG2.practice.SampleTestClassWithMultipleMethods.a())
…………
SKIPPED: a

SKIPPED: b
….
===============================================
Default test
Tests run: 2, Failures: 0, Skips: 2
Configuration Failures: 1, Skips: 1
===============================================

If we wish to ignore failures during script execution, we can configure Testng.xml with configfailurepolicy=”skip”, with this all skipped tests are treated as “skipped”









OUTPUT:
——-
===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 2
Configuration Failures: 1, Skips: 1
===============================================

On configuring testng.xml with “configfailurepolicy=”continue”, once a failure is observed, TestNG continues execution other cases:

Output:
a()
b()

===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

Display failurePolicy for a TestNG method programatically:
———————————————————
for (XmlSuite.FailurePolicy c : XmlSuite.FailurePolicy.values())
System.out.println(c);

Configure failurepolicy:
————————

XmlSuite suite = new XmlSuite();
suite.setName(“Parallel Mobile Tests”);
suite.setParallel(ParallelMode.TESTS);
suite.setThreadCount(2);

suite.setPreserveOrder(true);
suite.setConfigFailurePolicy(FailurePolicy.CONTINUE);