If I use Try catch, it displays in the TestNG report as ‘Pass’ for all the cases and i could not separate Fail/Pass cases, How to handle the Failed test cases in TestNG report?
check -> testNG soft asserts:
Can we use try catch block and write the test result in TestNg report?
yes when we use in catch softAssert. e.g. try-catch catching error and we report it in softAssert which does not stop test BUT fail it after.
example:
try {
// code to fail
} catch (Exception e) {
softAssert.assertTrue(false, "ScreenX: Failed to do some action:\n" + e.getMessage());
}
This Exception will be displayed in console right? how can we get it in testNG report as failed? , correct me if i am wrong,
@Aleksei I think you misunderstood my question, I would like to continue the test methods if any test method gets failed in between , for e.g.,
@Test
public void Test1(){
System.out.println("1st test passed");
}
@Test
public void Test2(){
System.out.println("2st test Failed!");
}
@Test
public void Test3(){
System.out.println("3rd test passed");
}
@Test
public void Test4(){
System.out.println("4th test passed");
}
here, I could see only first two tests in the report, like Test1 -> Passed, Test2 -> Failed
and other two tests were skipped.
I would like to run all the test methods even if any method gets failed due to any assertion or any other error. I don’t want to stop my test.
To generate report in html use e.g. Allure. To generate simple statistic yourself use iTestResult variable in afterMethod
Can you give me an example or sample code?
public void afterMethod(ITestResult iTestResult, ITestContext iTestContext) {
log(" ---------------------------");
log(" partially completed results");
log(" passed tests: " + iTestContext.getPassedTests().size());
log(" skipped tests: " + iTestContext.getSkippedTests().size());
log(" failed tests: " + iTestContext.getFailedTests().size());