使用SkipException时如何避免不必要的异常信息?

时间:2018-06-27 12:49:03

标签: selenium exception testng

我试图通过抛出SkipException来了解如何跳过TestNG中的测试,因为它可以帮助您跳过尚未准备好的测试。但是显示的异常信息使报告混乱。有什么办法可以避免这种情况? 代码是:

import org.testng.SkipException;
import org.testng.annotations.Test;

public class SkipTest  {
@Test(expectedExceptions = SkipException.class)
public void skip() throws Exception{
throw new SkipException("Skipping");
System.out.println("skip executed");
}

除了异常并在方法声明中抛出异常无济于事。

3 个答案:

答案 0 :(得分:1)

System.out.println("skip executed");无法访问,如果您按以下方式修复测试,则不会显示任何异常信息

import org.testng.SkipException;
import org.testng.annotations.Test;

public class SkipTest  {
   @Test(expectedExceptions = SkipException.class)
   public void skip() {
      throw new SkipException("Skipping");    
   }
}

答案 1 :(得分:1)

如果您想将@Test方法标记为已跳过,而又仍然不想在报告中看到异常信息,那么就不要抛出TestSkipException

这是您的操作方式(您需要利用TestNG侦听器)

测试类如下所示

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

@Listeners(SkipMarker.class)
public class SampleTestClass {

  @Test
  public void passMethod() {}

  @Test
  public void skipMethod() {
    // Adding an attribute to the current Test Method's result object to signal to the
    // TestNG listener (SkipMarker), that this method needs to be marked as skipped.
    Reporter.getCurrentTestResult().setAttribute("shouldfail", true);
  }
}

这是TestNG侦听器的外观

import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;

public class SkipMarker implements IInvokedMethodListener {

  @Override
  public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {}

  @Override
  public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
    // Look for the signalling attribute from the test method's result object
    Object value = testResult.getAttribute("shouldfail");
    if (value == null) {
      // If the attribute was not found, dont proceed further.
      return;
    }
    // attribute was found. So override the test status to failure.
    testResult.setStatus(ITestResult.FAILURE);
  }
}

答案 2 :(得分:0)

如果您想跳过尚未准备好的测试,请使用@Test(enabled=false) annotation