Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

junit 4.10 how to get the test case name printed before the test starts running [duplicate]

Tags:

java

junit

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.
    }
}
like image 476
user2511126 Avatar asked Jun 10 '14 08:06

user2511126


2 Answers

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

  • 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

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

  • Junit4 Test Suites
  • http://www.mkyong.com/unittest/junit-4-tutorial-5-suite-test

The bottom-line is

  • remove the extends TestCase from all of your tests
  • rewrite the test suite in junit4 style
like image 197
cheffe Avatar answered Sep 20 '22 06:09

cheffe


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:

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/

like image 35
Nawar Khoury Avatar answered Sep 20 '22 06:09

Nawar Khoury