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

最后修改: 2022年 1月 19日

1. Overview


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.


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.


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


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


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

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:


displayName = givenNumbers_whenOddCheck_thenVerify(TestInfo)

2.2. Parameterized Test


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:


private TestInfo testInfo;

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());

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


Let’s consider an individual test:


public TestName name = new TestName();

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.


Let’s run the test:


displayName = givenString_whenSort_thenVerifySortForString

3.2. Parameterized Test


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:


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:


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" }});

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.


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


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


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.