且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JUnit 5中的TestName规则等效于什么?

更新时间:2023-11-20 08:28:16

在测试方法中声明类型为TestInfo的参数,JUnit会自动为该方法提供该实例的一个实例:

Declare a parameter of type TestInfo in your test method and JUnit will automatically supply an instance of that for the method:

@Test
void getTestInfo(TestInfo testInfo) { // Automatically injected
    System.out.println(testInfo.getDisplayName());
    System.out.println(testInfo.getTestMethod());
    System.out.println(testInfo.getTestClass());
    System.out.println(testInfo.getTags());
}

您可以从TestInfo实例中获得测试方法名称(以及更多),如上所示.

You can get test method name (and more) from the TestInfo instance as shown above.