Lan Yung Lee
Lan Yung Lee

Reputation: 31

Print Test Result in JUnit

Currently I am making the test case repeat 2 times, so how do I print the results as 2 separate results.

I tried using the built-in function to create the text, however, it does now show either "Success" or "Failure".

Currently I have this code:

public class UnitTestRunner {
    static JUnitCore junitCore;
    static Class<?> testClasses;

    public static void main(String[] args) {
        System.out.println("Running Junit Test Suite.");
        Result result = JUnitCore.runClasses(TestSuite.class);
        for (Failure failure : result.getFailures()) {
            System.out.println(failure.toString());
        }
        System.out.println("Successful: " + result.wasSuccessful() +
            " ran " + result.getRunCount() + " tests");
    }
}

This code is working correctly, but I do not know how to implement this into JUnit.

Can someone please help to show, how to implement this code into JUnit test case.

Upvotes: 3

Views: 10302

Answers (1)

Jabir
Jabir

Reputation: 2866

This will be slightly long answer. For the customized output you have to add your RunListener You can use following sample implementation for the same.

public class UnitTestRunner {
    static JUnitCore junitCore;
    static Class<?> testClasses;

    public static void main(String[] args) {
        System.out.println("Running Junit Test Suite.");
        junitCore = new JUnitCore();
        junitCore.addListener(new CustomExecutionListener());

        Result result = junitCore.run(TestSuite.class);
        for (Failure failure : result.getFailures()) {
            System.out.println(failure.toString());
        }
        System.out.println("Successful: " + result.wasSuccessful() + " ran " + result.getRunCount() + " tests");
    }
}

And implementation for the RunListener is as follows

import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

public class CustomExecutionListener extends RunListener {

    public void testRunStarted(Description description) throws Exception {
        System.out.println("Number of tests to execute: " + description.testCount());
    }

    public void testRunFinished(Result result) throws Exception {
        System.out.println("Number of tests executed: " + result.getRunCount());
    }

    public void testStarted(Description description) throws Exception {
        System.out.println("Starting: " + description.getMethodName());
    }

    public void testFinished(Description description) throws Exception {
        System.out.println("Finished: " + description.getMethodName());
    }

    public void testFailure(Failure failure) throws Exception {
        System.out.println("Failed: " + failure.getDescription().getMethodName());
    }

    public void testAssumptionFailure(Failure failure) {
        System.out.println("Failed: " + failure.getDescription().getMethodName());
    }

    public void testIgnored(Description description) throws Exception {
        System.out.println("Ignored: " + description.getMethodName());
    }
}

And by overriding the methods in RunListener you can format you output.

Upvotes: 7

Related Questions