What is the equivalent of TestName rule in JUnit 5?

会有一股神秘感。 提交于 2019-12-06 18:31:12

问题


How can I get name of the test method in JUnit 5?


回答1:


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

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




回答2:


Personally I prefer to use this:

System.out.println(Thread.currentThread().getStackTrace()[1].getMethodName());



来源:https://stackoverflow.com/questions/52074872/what-is-the-equivalent-of-testname-rule-in-junit-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!