获取JUnit 4中当前正在执行的测试的名称 [英] Get name of currently executing test in JUnit 4
本文介绍了获取JUnit 4中当前正在执行的测试的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在JUnit 3中,我可以得到当前正在运行的测试的名称:
In JUnit 3, I could get the name of the currently running test like this:
public class MyTest extends TestCase
{
public void testSomething()
{
System.out.println("Current test is " + getName());
...
}
}
会打印目前的测试是testSomething。
which would print "Current test is testSomething".
在JUnit 4中是否有任何开箱即用或简单的方法?
Is there any out-of-the-box or simple way to do this in JUnit 4?
背景:显然,我不想只打印测试的名称。我想加载存储在与测试同名的资源中的特定于测试的数据。您知道,约定优于配置以及所有这些。
Background: Obviously, I don't want to just print the name of the test. I want to load test-specific data that is stored in a resource with the same name as the test. You know, convention over configuration and all that.
推荐答案
JUnit 4.7在测试名规则。看起来这样可以获得方法名称:
JUnit 4.7 added this feature it seems using TestName-Rule. Looks like this will get you the method name:
import org.junit.Rule;
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());
}
}
这篇关于获取JUnit 4中当前正在执行的测试的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文