如何在运行时在JUnit 4中获取测试用例名称? [英] How to obtain test case name in JUnit 4 at runtime?

查看:215
本文介绍了如何在运行时在JUnit 4中获取测试用例名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
}

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

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?

推荐答案

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

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

这篇关于如何在运行时在JUnit 4中获取测试用例名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆