Print Test Result in JUnit

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-25 01:02:05

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/46536753/print-test-result-in-junit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!