如何在运行时获取JUnit 4中的测试用例名称?

发布于 2024-07-26 11:11:22 字数 294 浏览 7 评论 0原文

我想在执行 JUnit 测试时进行一些日志记录。 在 JUnit 3.x 中,获取当前运行的测试用例的名称总是很容易,无论测试用例是如何实例化的:

public void testFoo() throws Exception() {
  String testName = this.getName();
  // [...] do some stuff
}

在 JUnit 4 中,事情似乎并不那么容易。 有谁知道这个问题的解决方案吗? 是否有任何选项可以反映到当前的 Runner 实例中?

I want to do some logging while executing my JUnit test. In JUnit 3.x it was always easy to obtain the name of the currently running test case, no matter how the test case was instantiated:

public void testFoo() throws Exception() {
  String testName = this.getName();
  // [...] do some stuff
}

In JUnit 4 things seem to be not so easy. Does anyone know a solution to this? Is there any option to reflect into the current Runner instance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

百合的盛世恋 2024-08-02 11:11:22

在 JUnit 4.7 中,您还可以获得当前执行的测试方法的名称。 登录时可能会很好。

摘自 JUnit 4.7 发行说明(在 github 上阅读此处):

public class NameRuleTest {
    @Rule public TestName name = new TestName();
    
    @Test public void testA() {
        assertEquals("testA", name.getMethodName());
    }
    
    @Test public void testB() {
        assertEquals("testB", name.getMethodName());
    }
}

In JUnit 4.7, you can also get the name of the currently executed test method. May be nice when logging.

Taken from JUnit 4.7 Release Notes (read them here at github) :

public class NameRuleTest {
    @Rule public TestName name = new TestName();
    
    @Test public void testA() {
        assertEquals("testA", name.getMethodName());
    }
    
    @Test public void testB() {
        assertEquals("testB", name.getMethodName());
    }
}
無處可尋 2024-08-02 11:11:22

好的。 我在互联网上的某个地方找到了另一种方法( http://www.nabble.com/What-happened-to-getName()--td23456371.html)

    @RunWith(Interceptors.class) 
    public class NameTest { 
            @Interceptor public TestName name = new TestName(); 

            @Test public void funnyName() { 
                    assertEquals("funnyName", name.getMethodName()); 
            } 
    } 

OK. I've found another approach [somewhere on the Internet](http://www.nabble.com/What-happened-to-getName()--td23456371.html):

    @RunWith(Interceptors.class) 
    public class NameTest { 
            @Interceptor public TestName name = new TestName(); 

            @Test public void funnyName() { 
                    assertEquals("funnyName", name.getMethodName()); 
            } 
    } 
〃安静 2024-08-02 11:11:22
public class FooTest {
    @Rule
    final public TestRule traceTestWatcher = new TestWatcher() {
        @Override
        protected void starting(Description d) {
            System.out.println(d);
        }
    };

    @Test
    public void testBar() {
        ...
    }

    @Test
    public void testBaz() {
        ...
    }
}
public class FooTest {
    @Rule
    final public TestRule traceTestWatcher = new TestWatcher() {
        @Override
        protected void starting(Description d) {
            System.out.println(d);
        }
    };

    @Test
    public void testBar() {
        ...
    }

    @Test
    public void testBaz() {
        ...
    }
}
一身骄傲 2024-08-02 11:11:22

有什么问题:

@Test
public void foo() throws Exception() {
   String testName = this.getName();
   // [...] do some stuff
}

What's wrong with:

@Test
public void foo() throws Exception() {
   String testName = this.getName();
   // [...] do some stuff
}

?

一影成城 2024-08-02 11:11:22

我知道这已经很旧了,但这是一个有用的(非 junit)方法,我将其放在所有测试的顶部。

public static void printTestName(){
    final StackTraceElement[] ste = new Throwable().getStackTrace();
    int buffer = 35 - ste[1].getMethodName().length();
    System.out.println("*******************************************************");
    System.out.println("*            TEST:  " + ste[1].getMethodName() + getBuffer(buffer) + "*");
    System.out.println("*******************************************************");
}

private static String getBuffer(int offset){
    StringBuilder buffer = new StringBuilder("");
    for(int i = 1; i < offset; i++){
        buffer.append(" ");
    }
    return buffer.toString();
}

I know this is old, but here is a useful (non-junit) method that I put at the top of all my tests.

public static void printTestName(){
    final StackTraceElement[] ste = new Throwable().getStackTrace();
    int buffer = 35 - ste[1].getMethodName().length();
    System.out.println("*******************************************************");
    System.out.println("*            TEST:  " + ste[1].getMethodName() + getBuffer(buffer) + "*");
    System.out.println("*******************************************************");
}

private static String getBuffer(int offset){
    StringBuilder buffer = new StringBuilder("");
    for(int i = 1; i < offset; i++){
        buffer.append(" ");
    }
    return buffer.toString();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文