无论测试是否为@Disabled,如何在每个@test之后运行代码块?

时间:2019-05-28 06:08:59

标签: java junit5 extent

基本上,我想在“范围测试”报告中添加一个条目。我知道我可以使用@AfterEach,但是我发现Junit5中的@Disabled Test无法执行@AfterEach代码块。

我尝试使用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'变量,我想运行测试类中的代码块,以向我的范围报告中添加一个条目,指出测试用例被跳过。

2 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,则只需对测试应用TestWatcher即可解决问题。通过 JUnit 5 ,您可以使用@ExtendsWith 注释。完整的示例为:

@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

进行了测试

答案 1 :(得分: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());
    }

}