Java junit 4.10 如何在测试开始运行之前打印测试用例名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24136055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
junit 4.10 how to get the test case name printed before the test starts running
提问by user2511126
junit 4.10 how to get the test case name printed before the test starts running..
junit 4.10 如何在测试开始运行之前打印测试用例名称..
So here I want to print "sampleTest".
所以在这里我想打印“sampleTest”。
How do I do it in junit 4.10 ? Thank you in advance
我如何在 junit 4.10 中做到这一点?先感谢您
class1:
TestSuite all = new TestSuite();
all.addTestSuite(class2);
all.run(result);
class2:
public class profileTest extends TestCase()
{
//I want to print the test cases name before the test actually starts executing
@Test
public void sampleTest1(){
//Some code here.
}
@Test
public void sampleTest2(){
//some more code here.
}
}
回答by Oh Chin Boon
String name = new Object(){}.getClass().getEnclosingMethod().getName();
回答by cheffe
Borrowing from Duncan's answerto the question Get name of currently executing test in JUnit 4
借鉴Duncan对Get name of current execution test in JUnit 4问题的回答
@Rule
public TestRule watcher = new TestWatcher() {
protected void starting(Description description) {
System.out.println("Starting test: " + description.getMethodName());
}
};
@Test
public void someTest() {
// do some testing
}
@Test
public void someOtherTest() {
// do some other testing
}
If you are within a bigger project, you may
如果你在一个更大的项目中,你可以
- extract the custom TestWatcher rule into a own small class, that way you would have one line of code per test class, two if you count the annotation
- add the rule declaration to an abstract test class all other tests implement
- 将自定义 TestWatcher 规则提取到一个自己的小类中,这样每个测试类就会有一行代码,如果算上注释则为两行
- 将规则声明添加到所有其他测试实现的抽象测试类
Update
更新
You are mixing junit3 and junit4 style. You cannot extend TestCase (junit3 style) and then attempt to rely on the annotation driven junit4 style. Have a read here how you can create test suits with junit4
您正在混合 junit3 和 junit4 风格。您不能扩展 TestCase(junit3 样式),然后尝试依赖注释驱动的 junit4 样式。在这里阅读如何使用 junit4 创建测试服
The bottom-line is
底线是
- remove the
extends TestCase
from all of your tests - rewrite the test suite in junit4 style
extends TestCase
从所有测试中删除- 以junit4风格重写测试套件
回答by Nawar Khoury
you can write a class that implements TestRules, and define test rules in it, what's being written before and after every test (you can also add measuring test time and other stuff), like this class:
您可以编写一个实现 TestRules 的类,并在其中定义测试规则,每次测试前后编写的内容(您还可以添加测量测试时间和其他内容),例如这个类:
package Test;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DecimalFormat;
import org.junit.rules.ExternalResource;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
public class TestRulesSetter implements TestRule {
private OutputStream out = null;
private final TestCasePrinter printer = new TestCasePrinter();
private String beforeContent = null;
private String afterContent = null;
private long timeStart;
private long timeEnd;
public TestRulesSetter(OutputStream os) {
out = os;
}
private class TestCasePrinter extends ExternalResource {
@Override
protected void before() throws Throwable {
timeStart = System.currentTimeMillis();
out.write(beforeContent.getBytes());
};
@Override
protected void after() {
try {
timeEnd = System.currentTimeMillis();
double seconds = (timeEnd-timeStart)/1000.0;
out.write((afterContent+"Time elapsed: "+new DecimalFormat("0.000").format(seconds)+" sec\n").getBytes());
} catch (IOException ioe) { /* ignore */
}
};
}
public final Statement apply(Statement statement, Description description) {
beforeContent = "\n[TEST START] "+description.getMethodName()+"\n"; // description.getClassName() to get class name
afterContent = "[TEST ENDED] ";
return printer.apply(statement, description);
}
}
and then in your test case, you can add an instance of that test rule setter and pass System.out to it with the annotation @Rule like the following:
然后在您的测试用例中,您可以添加该测试规则设置器的实例并将 System.out 使用注释 @Rule 传递给它,如下所示:
package Test;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
public class rsdg {
@Rule
public TestRulesSetter pr = new TestRulesSetter(System.out);
@Test
public void test1() {
}
@Test
public void test2() {
}
@Test
public void test3() {
}
}
this way gives you a lot of control on formatting the way your test look like
通过这种方式,您可以对测试的格式进行很多控制
check out this article: http://technicaltesting.wordpress.com/2012/10/23/junit-rule-for-printing-test-case-start-and-end-information/
查看这篇文章:http: //technicaltesting.wordpress.com/2012/10/23/junit-rule-for-printing-test-case-start-and-end-information/
回答by user2511126
I converted my test suite to junit4 and @Rule worked for me removed "extend TestCase"
我将我的测试套件转换为 junit4,@Rule 为我工作删除了“扩展测试用例”
suite.addTest(new JUnit4TestAdapter(Test1.class));