1

基本上我想在我的范围报告中添加一个“跳过测试”条目。我知道我可以使用@AfterEach,但是我看到 @AfterEach 代码块没有在 Junit5 中为@Disabled Test 执行。

我尝试使用 TestWatcher 接口并覆盖了 testDisabled 方法,如下所示:

package utils;

import java.util.Optional;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestWatcher;


public class MyTestWatcher implements TestWatcher {
                public static String testStatus;
                public static String testCaseName;
    @Override
    public void testAborted(ExtensionContext Context, Throwable throwable) {
                testStatus = "Aborted";
                testCaseName = Context.getDisplayName();
                System.out.println("Test Aborted: " + Context.getDisplayName());
                }

    @Override
    public void testDisabled(ExtensionContext Context, Optional<String> optional) {
                testStatus = "Skipped";
                testCaseName = Context.getDisplayName();  
                System.out.println("Test Skipped: " + Context.getDisplayName());
                }

    @Override
    public void testFailed(ExtensionContext Context, Throwable throwable) {
                testStatus = "Failed";
                testCaseName = Context.getDisplayName();   
               System.out.println("Test Failed: " + Context.getDisplayName());
}

    @Override
    public void testSuccessful(ExtensionContext Context) {
                testStatus = "Passed";
                testCaseName = Context.getDisplayName();
                System.out.println("Test Passed: " + Context.getDisplayName());
    }

}

基于上面 testDisabled() 方法中设置的“testStatus”和“testCaseName”变量,我想从我的测试类运行一段代码,在我的范围报告中添加一个条目,说明哪个测试用例是跳过。

4

2 回答 2

1

如果我正确理解了你的问题,你只需要申请TestWatcher你的测试来解决你的问题。使用JUnit 5,您可以使用@ExtendsWith annotation。完整的例子是:

@ExtendWith(MyTestWatcher.class)
class MyTest {

  @Test
  void test() {
    System.out.println("inside active test");
  }

  @Test
  @Disabled
  void testDisabled() {
    System.out.println("inside disabled test");
  }

}

带输出:

inside active test
Test Passed: test()
Test Skipped: testDisabled()

使用JUnit 5.4.2测试

于 2019-05-28T10:58:23.077 回答
0

通过从 MyTestWatcher 类初始化范围报告,我能够为跳过的测试添加报告。即使 ExtentReport 对象已经在我的测试类中初始化,当我再次尝试初始化(这将最终创建另一个 HTML 文件报告)时,我将以下代码添加到我的 ExtentManager 类中:

public static ExtentReports GetExtent(String env){
        **if (extent != null)
          return extent; //avoid creating new instance of html file**
        extent = new ExtentReports();       
        extent.attachReporter(getHtmlReporter());
        extent.setSystemInfo("Environment",env);
        extent.setAnalysisStrategy(AnalysisStrategy.SUITE);
        return extent;

我在 MyTestWatcher 类中的更新代码如下所示:

package utils;

import java.util.Optional;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestWatcher;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;




public class MyTestWatcher implements TestWatcher {
                public static String testCaseName;
                public static ExtentReports extent;
                public static ExtentTest report;

    static 
                {
                extent = utils.ExtentReportManager.GetExtent(System.getProperty("env"));
                report = extent.createTest("Skipped Tests", "Tests which are skipped are displayed here.");
                }
    @Override
    public void testAborted(ExtensionContext Context, Throwable throwable) {
                testCaseName = Context.getDisplayName();
                System.out.println("Test Aborted: " + Context.getDisplayName());
                }

    @Override
    public void testDisabled(ExtensionContext Context, Optional<String> optional) {
                testCaseName = Context.getDisplayName();  
                System.out.println("Test Skipped: " + Context.getDisplayName());
                report.createNode(testCaseName).log(Status.SKIP, "This test is marked as @DISABLED");
                }

    @Override
    public void testFailed(ExtensionContext Context, Throwable throwable) {
                testCaseName = Context.getDisplayName();   
               System.out.println("Test Failed: " + Context.getDisplayName());
                }

    @Override
    public void testSuccessful(ExtensionContext Context) {
                testCaseName = Context.getDisplayName();
                System.out.println("Test Passed: " + Context.getDisplayName());
    }

}
于 2019-05-29T05:27:16.320 回答