【问题标题】:JUnit5 - How to get test result in AfterTestExecutionCallbackJUnit5 - 如何在 AfterTestExecutionCallback 中获取测试结果
【发布时间】:2017-07-31 23:27:40
【问题描述】:

我编写 JUnit5 扩展。但是我找不到如何获得测试结果的方法。

扩展看起来像这样:

import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.TestExtensionContext;

public class TestResultExtension implements AfterTestExecutionCallback {
    @Override
    public void afterTestExecution(TestExtensionContext context) throws Exception {
        //How to get test result? SUCCESS/FAILED
    }
}

任何提示如何获得测试结果?

【问题讨论】:

标签: java junit junit5 junit5-extension-model


【解决方案1】:

我只有这个解决方案:

String testResult = context.getTestException().isPresent() ? "FAILED" : "OK";

看起来效果不错。但我不确定它是否在所有情况下都能正常工作。

【讨论】:

  • 这两种情况你都试过了吗?测试何时通过和失败?我认为这行不通。
  • 是的,我做到了。我试过通过测试。和测试,断言失败。
  • 更新后的版本现在可以使用,虽然不太确定它的可靠性。
【解决方案2】:

JUnit 中的失败会通过异常传播。有几个例外,表示各种类型的错误。

所以TestExtensionContext#getTestException() 中的异常表示错误。该方法无法操纵实际测试结果,因此根据您的用例,您可能需要实现TestExecutionExceptionHandler,它允许您吞下异常,从而改变测试是否成功。

【讨论】:

    【解决方案3】:

    正如其他答案指出的那样,JUnit communicates failed tests with exceptions,因此可以使用AfterTestExecutionCallback to gleam what happened。请注意,这很容易出错,因为稍后运行的扩展可能仍然无法通过测试。

    另一种方法是register a custom TestExecutionListener。不过,这两种方法都有点迂回。有一个跟踪 a specific extension point for reacting to test results 的问题,这可能是对您的问题最直接的回答。如果你能提供一个具体的用例,最好能转到#542 并留下评论来描述它。

    【讨论】:

      【解决方案4】:

      这对我有用:

      public class RunnerExtension implements AfterTestExecutionCallback {
      
          @Override
          public void afterTestExecution(ExtensionContext context) throws Exception {
              Boolean testResult = context.getExecutionException().isPresent();
              System.out.println(testResult); //false - SUCCESS, true - FAILED
          }
      }
      
      @ExtendWith(RunnerExtension.class)
      public abstract class Tests {
      
      }
      

      【讨论】:

      • 这对我有用,虽然不完全确定如何管理 ExentTest 实例的上下文
      【解决方案5】:

      您可以从org.junit.platform.launcher.listeners 使用SummaryGeneratingListener

      它包含MutableTestExecutionSummary字段,它实现了TestExecutionSummary接口,通过这种方式可以获取容器、测试、时间、故障等信息。

      您可以创建自定义监听器,例如:

      1. 创建扩展SummaryGeneratingListener的类
      public class ResultAnalyzer extends SummaryGeneratingListener {
          @Override
          public void testPlanExecutionFinished(TestPlan testPlan) {
              //This method is invoked after all tests in all containers is finished
              super.testPlanExecutionFinished(testPlan);
              analyzeResult();
          }
      
          private void analyzeResult() {
              var summary = getSummary();
              var failures = summary.getFailures();
              //Do something
          }
      }
      
      1. 通过创建文件注册监听器

      src\main\resources\META-INF\services\org.junit.platform.launcher.TestExecutionListener

      并在其中指定您的实现

      path.to.class.ResultAnalyzer

      1. 启用自动检测扩展,设置参数

      -Djunit.jupiter.extensions.autodetection.enabled=true

      就是这样!

      文档

      https://junit.org/junit5/docs/5.0.0/api/org/junit/platform/launcher/listeners/SummaryGeneratingListener.html

      https://junit.org/junit5/docs/5.0.0/api/org/junit/platform/launcher/listeners/TestExecutionSummary.html

      https://junit.org/junit5/docs/current/user-guide/#extensions-registration-automatic

      【讨论】:

      • 你能提供一些代码吗?您使用的是什么版本的 JUnit?已经两年了,也许 API 发生了一些变化 :)
      猜你喜欢
      • 1970-01-01
      • 2019-05-17
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      相关资源
      最近更新 更多