How to rerun skipped test cases using TestNG?

21 views
Skip to first unread message

Kaveesh Gamage

unread,
May 9, 2024, 3:54:48 AMMay 9
to testng-users
0

I tried to re-run testng skipped test cases in the following way and I am getting 'Test ignored.' message. How to fix this issue using TestNG?


public class TestClass{
private static int counter = 5;

@Test(alwaysRun = true)
public void skipTest() throws IOException {
throw new SkipException("Skipping this test case intentionally");
}

@AfterSuite(alwaysRun = true)
public void afterSuite(ITestContext ctx) {
IResultMap skippedTests = ctx.getSkippedTests();

while (skippedTests.size() > 0 && (counter--) > 0) {
List<XmlInclude> includeMethods
= skippedTests.getAllMethods()
.stream()
.map(m -> new XmlInclude(m.getMethodName()))
.collect(Collectors.toList());
XmlClass xmlClass = new XmlClass(this.getClass().getName());
xmlClass.setIncludedMethods(includeMethods);

XmlTest test = ctx.getCurrentXmlTest();
List<XmlClass> classes = test.getXmlClasses();
classes.remove(0);
classes.add(xmlClass);

TestRunner newRunner = new TestRunner(new Configuration(),
ctx.getSuite(),
test,
false,
null,
new ArrayList<>());
newRunner.run();

Set<String> newPassed = newRunner.getPassedTests().getAllResults()
.stream()
.map(ITestResult::getName)
.collect(Collectors.toSet());
IResultMap passedTests = ctx.getPassedTests();
Iterator<ITestResult> iter = skippedTests.getAllResults().iterator();
while (iter.hasNext()) {
ITestResult result = iter.next();
if (newPassed.contains(result.getName())) {
result.setStatus(ITestResult.SUCCESS);
passedTests.addResult(result, result.getMethod());

//remove the test from list of skipped tests.
iter.remove();
}
}
}
}
}
Reply all
Reply to author
Forward
0 new messages