How can I change the status of the test which is skip before method

364 views
Skip to first unread message

Merve Yaldiz

unread,
Aug 3, 2017, 9:31:23 AM8/3/17
to testng-users
I do some processing for pre-preparation in the @before method. When any of these actions fail, the test case is skipped. Where can I set this test case status as a fail? When I came to the @aftermethod, I set the test result to fail but my test status skip. 

When I debug, the Test Listener Adapter objects are already set as skip so result looks skipped  when reporting. How can I solve this problem ?How can I change the test result before the listener objects are set to final ? 

Can I change the test result before the result of the listener objects is set

@AfterMethod(alwaysRun = true)
public void cleanUp(ITestContext context) {

for(ITestResult result : context.getSkippedTests().getAllResults()){
result.setStatus(ITestResult.FAILURE);

                     // I already set test result as a FAILURE 
context.getFailedTests().addResult(result, result.getMethod());
context.getSkippedTests().getAllResults().remove(result);
result.getTestContext().getSkippedTests();
 
}
}

SKIPPED: helloTest
         

===============================================
    Default test
    Tests run: 1, Failures: 0, Skips: 1
    Configuration Failures: 2, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Failures: 2, Skips: 0
Configuration Failures: 2, Skips: 0
===============================================

[[TestHTMLReporter passed=0 failed=0 skipped=1], org.testng.reporters.JUnitXMLReporter@59d4cd39, org.testng.remote.strprotocol.RemoteTestListener@4b34fff9, [TextReporter passed=0 failed=0 skipped=1], [ISBTestListenerAdapter passed=0 failed=0 skipped=0], org.testng.TestNG$ExitCodeListener@16aa8654, [TestListenerAdapter passed=0 failed=0 skipped=1]]

public static void runTestListeners(ITestResult tr, List<ITestListener> listeners) {
    for (ITestListener itl : listeners) {
      switch(tr.getStatus()) {
        case ITestResult.SKIP: {
          itl.onTestSkipped(tr);
          break;
        }
        case ITestResult.SUCCESS_PERCENTAGE_FAILURE: {
          itl.onTestFailedButWithinSuccessPercentage(tr);
          break;
        }
        case ITestResult.FAILURE: {
          itl.onTestFailure(tr);
          break;
        }
        case ITestResult.SUCCESS: {
          itl.onTestSuccess(tr);
          break;
        }

        case ITestResult.STARTED: {
          itl.onTestStart(tr);
          break;
        }

        default: {
          assert false : "UNKNOWN STATUS:" + tr;
        }
      }
    }
  }

Thank you for your help


Reply all
Reply to author
Forward
0 new messages