13

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.
    }
}
user2511126
  • 620
  • 1
  • 14
  • 31
  • 1
    refer: http://stackoverflow.com/questions/473401/get-name-of-currently-executing-test-in-junit-4 for similar problem. – Shailesh Aswal Jun 10 '14 at 08:47
  • This question has better answers to the question asked here: http://stackoverflow.com/questions/15222376/how-to-print-current-executing-junit-test-method-while-running-all-tests-in-a-cl – oberlies Jun 08 '15 at 09:41
  • This question uses a crude (broken?) mixture of JUnit3 and JUnit4 – oberlies Jun 08 '15 at 09:43

4 Answers4

24

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

The bottom-line is

  • remove the extends TestCase from all of your tests
  • rewrite the test suite in junit4 style
Community
  • 1
  • 1
cheffe
  • 9,345
  • 2
  • 46
  • 57
  • I tried this @ Rule, but I dont know why this @ Rule method doesnt gets triggered before @ Test method starts executing. Please suggest – user2511126 Jun 10 '14 at 09:26
  • Hm, probably you have some other TestRunner around your tests? Is the example given in your question exactly the code you try to run this with? – cheffe Jun 10 '14 at 09:42
  • yes. something similar to that. I know there should be a way to do this,. – user2511126 Jun 10 '14 at 09:47
  • Would you then please post the unit test you try to run? – cheffe Jun 10 '14 at 09:50
  • I just edited my question. Please let me know if you need more info. – user2511126 Jun 10 '14 at 10:02
  • yea I know, I have mixed both junit3 styling and junit 4 styling because of my needs. I am running these tests on my android device and because I couldnt find a way to generate a XML report using "addListner" in junit4 so I had to do this approach. – user2511126 Jun 10 '14 at 10:38
1

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/

Nawar Khoury
  • 170
  • 2
  • 13
0
String name = new Object(){}.getClass().getEnclosingMethod().getName();

Getting the name of the current executing method

Community
  • 1
  • 1
Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
0

I converted my test suite to junit4 and @Rule worked for me removed "extend TestCase"

suite.addTest(new JUnit4TestAdapter(Test1.class));
user2511126
  • 620
  • 1
  • 14
  • 31