junit 4.10如何在测试开始运行之前打印测试用例名称。。
所以我想在这里打印"sampleTest"。
我该如何在第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.
}
}
借用Duncan对问题的回答获取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
}
如果你在一个更大的项目中,你可以
- 将自定义的TestWatcher规则提取到一个自己的小类中,这样每个测试类就有一行代码,如果算上注释,则有两行代码
- 将规则声明添加到所有其他测试实现的抽象测试类中
更新
你在混合junit3和junit4风格。您不能扩展TestCase(junit3样式(,然后尝试依赖注释驱动的junit4样式。在这里阅读如何使用junit4 创建测试服
- Junit4测试套件
- http://www.mkyong.com/unittest/junit-4-tutorial-5-suite-test
底线是
- 从所有测试中删除
extends TestCase
- 用junit4风格重写测试套件
您可以编写一个实现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)+" secn").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);
}
}
然后在您的测试用例中,您可以添加该测试规则设置器的一个实例,并使用注释@rule将System.out传递给它,如下所示:
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() {
}
}
这种方式可以让你在格式化测试时有很大的控制权,就像一样
查看这篇文章:http://technicaltesting.wordpress.com/2012/10/23/junit-rule-for-printing-test-case-start-and-end-information/
String name = new Object(){}.getClass().getEnclosingMethod().getName();
获取当前执行方法的名称
我将测试套件转换为junit4,@Rule对我有效删除了"扩展测试用例">
suite.addTest(new JUnit4TestAdapter(Test1.class));