Junit 4.10 how to get a test case name printed before running a test

junit 4.10 how to get the name of the test case before running the test.

So here I want to print "sampleTest".

How to do this 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.
    }
}
+4
source share
5 answers

Borrowing from Duncan answer to the question Get the name of the current executable 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 part of a larger project, you can

  • extract your own TestWatcher rule into your own small class, this way you will have one line of code for each test class, two if you count the annotation
  • ,

Update

junit3 junit4 . TestCase ( junit3), junit4 . , junit4

  • extends TestCase .
  • junit4
+10

, 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);
    }    
}

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() {

    }

}

,

: http://technicaltesting.wordpress.com/2012/10/23/junit-rule-for-printing-test-case-start-and-end-information/

+1
String name = new Object(){}.getClass().getEnclosingMethod().getName();

0

junit4, @Rule " TestCase"

suite.addTest(new JUnit4TestAdapter(Test1.class));
0
source

There is always the obvious:

@Test
public void sampleTest()
{
    System.out.println("sampleTest");
    //Some code here.
}
-2
source

Source: https://habr.com/ru/post/1543959/


All Articles