如何在JUnit 5的“ @BeforeEach”方法中打印“待执行” @Test方法的名称?

时间:2019-05-19 08:14:04

标签: junit5

我了解我们可以在JUnit 4中使用@RuleTestName来做到这一点,但是我正在使用JUnit 5(木星),并且正在努力寻找一种打印测试方法(将要执行)名称的方法。使用@BeforeEach方法。

1 个答案:

答案 0 :(得分:2)

在您的@BeforeEach方法中声明一个TestInfo参数。 JUnit Jupiter将在运行时注入其实例,其中包含与“当前执行的测试”有关的所有可用信息。

例如这样的例子:

@BeforeEach
void init(TestInfo testInfo) {
    String displayName = testInfo.getDisplayName();
    String methodName = testInfo.getTestMethod().orElseThrow().getName();
    // ...
}

有关更多详细信息,请参见https://junit.org/junit5/docs/current/user-guide/#writing-tests-dependency-injection

相关问题