我如何在JUnit中获取测试名称/测试状态 [英] How could i get a Testname / Test status in JUnit
问题描述
我是JUnit的新手.我正在研究获得
I am new to JUnit . I am working on the ways to get a output like
类名/函数名/状态/描述.
classname / function name / status / description.
我发现,除了使用Rule的FrameworkMethod之外,没有其他方法可以获取成功测试名称.是否可以事先在JUnitCore/textui.Thx的帮助下将规则设置为正在运行的套件!!
I figured that there is no other way to get success test name other than FrameworkMethod using Rule. Is it possible to set a Rule to a suite which s running with the help of JUnitCore/textui.Thx in advance.!!
-我在Junit 4.9b2上尝试过testwatchman,但是执行套件对我而言不起作用.任何帮助,将不胜感激.
- I tried testwatchman with Junit 4.9b2 but executing suites is not working for me. Any help would be appreciated.
@RunWith(Suite.class)
@SuiteClasses({testclass1.class, testclass2.class})
public class junitCheck {
private static String watchedLog;
@Rule
public MethodRule watchman= new TestWatchman() {
@Override
public void failed(Throwable e, FrameworkMethod method) {
watchedLog+= method.getName() + " " + e.getClass().getSimpleName()
+ "\n";
}
@Override
public void succeeded(FrameworkMethod method) {
watchedLog+= method.getName() + " " + "success!\n";
}
};
}
public class testclass1 {
@Test
public void add()
{
Assert.assertEquals(4,2+3);
}
}
public class testclass2 {
@Test
public void add1()
{
Assert.assertEquals(4,2+2);
}
}
推荐答案
它不适用于该套件.因此,唯一的方法是将org.junit.rules.TestWatcher添加到测试的通用超类中.
It won't work with the suite. So the only way is to add an org.junit.rules.TestWatcher to a common super class of the tests.
这篇关于我如何在JUnit中获取测试名称/测试状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!