Get the Name of the Currently Executing Test in JUnit – 在JUnit中获取当前执行的测试的名称

最后修改: 2022年 1月 19日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

When using JUnit, we may need our tests to have access to their name. This may help with error messages, especially for tests with system-generated names.

当使用JUnit时,我们可能需要我们的测试能够访问他们的名字。这可能有助于错误信息的处理,特别是对于具有系统生成的名称的测试。

In this short tutorial, we’ll look at how to get the name of the current test case in both JUnit 4 and JUnit 5.

在这个简短的教程中,我们将看看如何在JUnit 4JUnit 5中获得当前测试案例的名称。

2. JUnit 5 Approach

2.JUnit 5的方法

Let’s look at two scenarios. First, we’ll see how to get access to the name of a single test. This name is usually predictable since it’s probably the name of the function or the value of the @DisplayName annotation. However, if we’re using parameterized tests or display name generators, then we may need to know the name JUnit has provided.

让我们看一下两个场景。首先,我们将看到如何获得对单个测试的名称的访问。这个名字通常是可预测的,因为它可能是函数的名称或@DisplayName注释的值。但是,如果我们使用参数化测试或显示名称生成器,那么我们可能需要知道JUnit提供的名称。

JUnit 5 can inject a TestInfo object into our tests to show us the name of the current test case.

JUnit 5可以在我们的测试中注入一个TestInfo对象,向我们显示当前测试案例的名称。

2.1. Individual Test

2.1.个人测试

Let’s inject a TestInfo object into our test function:

让我们把一个TestInfo对象注入我们的测试函数中。

@Test
void givenNumbers_whenOddCheck_thenVerify(TestInfo testInfo) {
    System.out.println("displayName = " + testInfo.getDisplayName());
    int number = 5;
    assertTrue(oddCheck(number));
}

Here we have used the getDisplayName method of the interface TestInfo to display the name of the test. When we run the test, we get the test name:

这里我们使用了接口TestInfogetDisplayName方法来显示测试的名称。当我们运行测试时,我们会得到测试的名字。

displayName = givenNumbers_whenOddCheck_thenVerify(TestInfo)

2.2. Parameterized Test

2.2.参数化测试

Let’s try this with a parameterized test. Here we’ll use the name field of the @ParameterizedTest annotation to describe to JUnit how to produce a name for the test for us:

让我们用一个参数化测试来试试。在这里,我们将使用@ParameterizedTestannotation的name字段来向JUnit描述如何为我们的测试产生一个名称。

private TestInfo testInfo;

@BeforeEach
void init(TestInfo testInfo) {
    this.testInfo = testInfo;
}

@ParameterizedTest(name = "givenNumbers_whenOddCheck_thenVerify{0}")
@ValueSource(ints = { 1, 3, 5, -3, 15 })
void givenNumbers_whenOddCheck_thenVerify(int number) {
    System.out.println("displayName = " + testInfo.getDisplayName());
    assertTrue(oddCheck(number));
}

We should note that, unlike the individual test, we cannot inject TestInfo into the function. This is because the function parameters must relate to the parameterized data. To solve this we need to store the TestInfo in a field in the test class via the beforeEach method.

我们应该注意到,与单个测试不同,我们不能将TestInfo注入到函数中。这是因为函数参数必须与参数化数据有关。为了解决这个问题,我们需要通过 beforeEach方法将TestInfo存储在测试类的一个字段中

When we run the test, we get the test names:

当我们运行测试时,我们得到测试名称。

displayName = givenNumbers_whenOddCheck_thenVerify5
displayName = givenNumbers_whenOddCheck_thenVerify-3
displayName = givenNumbers_whenOddCheck_thenVerify3
displayName = givenNumbers_whenOddCheck_thenVerify1
displayName = givenNumbers_whenOddCheck_thenVerify15

3. JUnit 4 Approach

3.JUnit 4方法

JUnit 4 can populate a TestName object in our testsTestName is a JUnit rule, and rules are executed as part of JUnit’s test execution, showing them the details of the currently running test along the way.

JUnit 4 可以在我们的测试中填充一个 TestName 对象TestNameJUnit规则,规则作为JUnit测试执行的一部分被执行,沿途向他们展示当前运行的测试的细节。

3.1. Individual Test

3.1.个人测试

Let’s consider an individual test:

让我们考虑一个单独的测试。

@Rule
public TestName name = new TestName();

@Test
public void givenString_whenSort_thenVerifySortForString() {
    System.out.println("displayName = " + name.getMethodName());
    String s = "abc";
    assertEquals(s, sortCharacters("cba"));
}

As shown above, we can use the getMethodName method of class TestName to display the name of the test.

如上所示,我们可以使用类TestNamegetMethodName方法来显示测试的名称

Let’s run the test:

让我们运行测试。

displayName = givenString_whenSort_thenVerifySortForString

3.2. Parameterized Test

3.2.参数化测试

Now let’s use the same method to display the test name generated for a parameterized test. First, we need to annotate the test with the special test runner:

现在让我们用同样的方法来显示为一个参数化测试生成的测试名称。首先,我们需要用特殊的测试运行器对测试进行注释。

@RunWith(Parameterized.class)
public class JUnit4ParameterizedTestNameUnitTest {
}

Then we can implement the test with both the TestName rule and the fields and constructor for assigning the parameter values of the current test:

然后我们可以用TestName规则和字段及构造函数来实现测试,以分配当前测试的参数值。

@Rule
public TestName name = new TestName();
private String input;
private String expected;

public JUnit4ParameterizedTestNameUnitTest(String input, String expected) {
    this.input = input;
    this.expected = expected;
}

@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> suppliedData() {
    return Arrays.asList(new Object[][] { 
      { "abc", "abc" }, { "cba", "abc" }, { "onm", "mno" }, { "a", "a" }, { "zyx", "xyz" }});
}

@Test
public void givenString_whenSort_thenVerifySortForString() {
    System.out.println("displayName = " + name.getMethodName());
    assertEquals(expected, sortCharacters(input));
}

In this test, we supply the test data Collection which contains both input strings as well as expected strings. This is done via the suppliedData function, annotated with the @Parameterized.Parameters annotation. This annotation also allows us to describe the test name.

在这个测试中,我们提供了测试数据Collection,其中包含输入字符串和预期字符串。这是通过suppliedData函数完成的,该函数用@Parameterized.Parameters注解来注释。这个注解还允许我们描述测试名称。

When we run the test, the TestName rule is given the names of each test for us to see:

当我们运行测试时,TestName规则会给出每个测试的名称供我们查看。

displayName = givenString_whenSort_thenVerifySortForString[abc]
displayName = givenString_whenSort_thenVerifySortForString[cba]
displayName = givenString_whenSort_thenVerifySortForString[onm]
displayName = givenString_whenSort_thenVerifySortForString[a]
displayName = givenString_whenSort_thenVerifySortForString[zyx]

4. Conclusion

4.总结

In this article, we discussed how to find the name of the current test in both JUnit 4 and 5.

在这篇文章中,我们讨论了如何在JUnit 4和5中找到当前测试的名称。

We saw how to do this for both individual tests and parameterized tests.

我们看到了如何为单个测试和参数化测试做这件事。

As usual, the complete source code is available over on GitHub.

像往常一样,完整的源代码可以在GitHub上找到