本文介绍了是否有类似Spring的用于TestSuite的TestExecutionListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在使用TestExecutionListener进行测试,并且效果非常完美

Currently for tests I'm using TestExecutionListener and it works just perfect

public class ExecutionManager extends AbstractTestExecutionListener {

    @Override
    public void beforeTestClass(TestContext testContext) throws Exception {
        System.out.println("beforeClass");
    }

    @Override
    public void afterTestClass(TestContext testContext) throws Exception {
        System.out.println("afterClass");
    }
}

测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners(ExecutionManager.class)
public final class TC_001 {

    @Test
    public void test() {
        System.out.println("Test_001");
    }
}

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners(ExecutionManager.class)
public final class TC_002 {

    @Test
    public void test() {
        System.out.println("Test_002");
    }
}

当我将这些测试包含在测试套件中时,将为每个测试类执行beforeTestClass(TestContext testContext)afterTestClass(TestContext testContext)方法,这是很合逻辑的:

When I include those tests in test suite, beforeTestClass(TestContext testContext) and afterTestClass(TestContext testContext) methods are executed for each test class, what is quite logical:

@RunWith(Suite.class)
@Suite.SuiteClasses({
        TC_001.class,
        TC_002.class
})
public final class TS_001 {
}

是否有类似SuiteExecutionListener(套房为TestExecutionListener)的东西?

Is there anything like SuiteExecutionListener (TestExecutionListener for suites)?

基本上,我需要套件的非静态@BeforeClass@AfterClass

Basically I need non-static @BeforeClass and @AfterClass for suite

OR

在ExecutionListener中,我需要找出已启动的类:案例或套件.为此,我可以:

In ExecutionListener I need to find out what class has been launched: case or suite. For this purpose I can:

  • 分析StackTrace并获取调用类
  • 使用Reflection.getCallerClass(int i)(已弃用)
  • 将调用方类传递给ExecutionManager(顺便说一句,我该怎么做?是否可以像在Android Bundle中一样将Object放入TestContext中?)
  • analyze StackTrace and get calling class
  • use Reflection.getCallerClass(int i) (which is deprecated)
  • pass caller class to ExecutionManager (By the way, how can I do that? Is it possible to put Object into TestContext like in Android Bundle?)

但是我不太喜欢那些解决方案. SuiteExecutionListener更可取

But I don't really like those solutions. SuiteExecutionListener is much more preferable

谢谢

推荐答案

不,不幸的是,在 Spring TestContext Framework (TCF)中没有SuiteExecutionListener这样的东西.

No, there is unfortunately no such thing as a SuiteExecutionListener in the Spring TestContext Framework (TCF).

TCF在套件级别上未与JUnit 4集成.

The TCF does not integrate with JUnit 4 at the suite level.

如果要在TestContext中存储某些内容,那不是问题. TestContext实现了org.springframework.core.AttributeAccessor,因此您可以将属性存储在TestContext中.但是请注意,给定TestContext的生命周期与测试类相关.

If you want to store something in the TestContext, that's not a problem. TestContext implements org.springframework.core.AttributeAccessor, so you can store attributes in the TestContext. Note, however, that the lifecycle of a given TestContext is tied to a test class.

这篇关于是否有类似Spring的用于TestSuite的TestExecutionListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 10:32