简体   繁体   English

如何在Spring测试侦听器中获取TestNG ITestResult?

[英]How can I get TestNG ITestResult in Spring test listener?

I'm writing tests using Spring and TestNG. 我正在使用Spring和TestNG编写测试。 An example of a test class: 一个测试类的例子:

@SpringBootTest
@TestExecutionListeners(
        mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS,
        listeners = {TestListener.class}
)
public class BaseTest extends AbstractTestNGSpringContextTests
{
}

My TestListener class extends TransactionalTestExecutionListener so I have override methods for beforeTestClass(TestContext testContext), afterTestMethod(TestContext testContext) etc. 我的TestListener类扩展了TransactionalTestExecutionListener,所以我有beforeTestClass(TestContext testContext),afterTestMethod(TestContext testContext)等的重写方法。

My problem is that within afterTestMethod I need to be able to get the TestNG ITestResult or TestResult so I can do different things depending on test success, fail or skip etc. Is there any way I can access ITestResult or TestResult from a spring test listener? 我的问题是,在afterTestMethod中,我需要能够获取TestNG ITestResult或TestResult,以便可以根据测试成功,失败或跳过等来做不同的事情。我可以从弹簧测试侦听器访问ITestResult或TestResult吗?

There is no easy direct way of getting access to the ITestResult object of a test method that was executed because Spring doesn't seem to provide access to it. 没有简单的直接方法可以访问已执行的测试方法的ITestResult对象,因为Spring似乎没有提供对其的访问。

You can try doing something like this: 您可以尝试执行以下操作:

  • Build a utility method such that when given a Method object that represents a @Test method that was just executed, it would query the current ITestContext and find any ITestResult object whose Method object would match with the Method object that was provided. 构造一个实用程序方法,以便在给定代表刚刚执行的@Test方法的Method对象时,它将查询当前的ITestContext并查找其Method对象与所提供的Method对象匹配的任何ITestResult对象。
  • Have your listener implementation query this utility method to get access to the ITestResult object. 让您的侦听器实现查询此实用程序方法以访问ITestResult对象。

Here's how a sample implementation could look like: 这是一个示例实现的样子:

public class MyListener extends TransactionalTestExecutionListener {
    @Override
    public void afterTestMethod(TestContext testContext) throws Exception {
      super.afterTestMethod(testContext);
      ITestResult currentTestResult = getCorrespondingResultFor(testContext.getTestMethod());
    }

    private ITestResult getCorrespondingResultFor(Method method) {
      ITestContext context = Reporter.getCurrentTestResult().getTestContext();
      Set<ITestResult> allResults = new HashSet<>();
      allResults.addAll(context.getPassedTests().getAllResults());
      allResults.addAll(context.getFailedTests().getAllResults());
      allResults.addAll(context.getSkippedTests().getAllResults());
      return allResults
          .stream()
          .filter(result -> result.getMethod().getConstructorOrMethod().getMethod().equals(method))
          .findAny()
          .orElse(null);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring:如果无法扩展AbstractTestNGSpringContextTests,如何在Testng测试类中获取spring applicationContext - Spring: How to get spring applicationContext in a Testng test class, if I can NOT extend AbstractTestNGSpringContextTests 在使用TestNg ITestResult接口或任何其他testNg接口进行测试执行之后,如何调用TestNg @Test方法的输入参数的实例 - How to call instances of input arguments of TestNg @Test method, after test execution using TestNg ITestResult interface or any other testNg interface 如何使用org.testng.TestNG将参数输入此测试运行中? - How can I get parameters into this test run using org.testng.TestNG? 如何在AfterMethod中的TestNG中测试失败? - How can I fail a test in TestNG in an AfterMethod? 如何在Jbehave中调用TestNG自定义测试侦听器 - How to invoke TestNG Custom Test Listener with in Jbehave 我无法使用TestNG启动器将Spring Beans注入Test(多线程) - I can not Inject Spring Beans into Test using TestNG launcher (Multithreading) TestNG:如何将IConfigurationListener2.beforeConfiguration ITestResult链接到ITestListener.onTestStart - TestNG: How to link IConfigurationListener2.beforeConfiguration ITestResult to ITestListener.onTestStart 我如何在Selenium WebDriver中获得带有TestNG框架的测试报告? - How can i get Test Report with TestNG frame work with Selenium WebDriver? TestNG-在测试监听器中获取提供给构造函数的@Factory参数 - TestNG - Get @Factory parameters supplied to constructor in test listener 我如何使用testng发送多个测试数据到以下 - How can i send multiple test data using testng in to following
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM