'Next test(s) ignored when afterMethod fails
I want to continue test execution even if the afterMethod fails, is there any solution for that? I tried to use alwaysRun = true, but only before and after methods are executed, the tests are always ignored.
Here is a simple example (used assertion only for failing the test, imagine instead of assertion some logic which can fail), where the afterMethod fails and the rest tests are ignored.
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import static org.testng.Assert.assertTrue;
public class Test1 {
@BeforeMethod
public void setup() {
assertTrue(true);
}
@Test
public void test1() {
System.out.println("firstTest");
}
@Test
public void test2() {
System.out.println("secondTest");
}
@Test
public void test3() {
System.out.println("thirdTest");
}
@AfterMethod
public void clearData() {
assertTrue(false);
}
}
Solution 1:[1]
You could try to use soft assertions (see for example https://www.seleniumeasy.com/testng-tutorials/soft-asserts-in-testng-example)
Solution 2:[2]
You are using hard assertion. If you use softAssertion like below :
softAssertion.assertTrue(false);
In code :
@BeforeMethod
public void setup() {
assertTrue(true);
}
@Test
public void test1() {
System.out.println("firstTest");
}
@Test
public void test2() {
System.out.println("secondTest");
}
@Test
public void test3() {
System.out.println("thirdTest");
}
@AfterMethod
public void clearData() {
SoftAssert softAssertion= new SoftAssert();
softAssertion.assertTrue(false);
}
You should get this output :
firstTest
secondTest
thirdTest
PASSED: test2
PASSED: test3
PASSED: test1
===============================================
Default test
Tests run: 3, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================
Please see here for Reference
Also, If you use hard assertion in a @Test not in configuration annotation, your test cases would have run if there was any failure.
But you are intentionally failing the testng CONFIGURATION, so when a configuration fails in testng next or other configuration are gonna be skipped, You may be seeing this kind of output.
SKIPPED CONFIGURATION: @BeforeMethod setup
SKIPPED CONFIGURATION: @AfterMethod clearData
SKIPPED CONFIGURATION: @AfterMethod clearData
SKIPPED CONFIGURATION: @BeforeMethod setup
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JP13 |
| Solution 2 |
