junit 4.10 how to get the test case name printed before the test starts running..
So here I want to print "sampleTest".
How do I do it in junit 4.10 ? Thank you in advance
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.
}
}
Borrowing from Duncan's answer to the question Get name of currently executing 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
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
The bottom-line is
extends TestCase
from all of your testsyou 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:
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:
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With