在JUnit中打印测试结果 [英] Print Test Result in JUnit

查看:419
本文介绍了在JUnit中打印测试结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我使测试用例重复2次,因此如何将结果打印为2个单独的结果.

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".

当前我有以下代码:

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

此代码正常工作,但是我不知道如何在JUnit中实现它.

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

请帮忙演示如何在JUnit测试用例中实现此代码.

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

推荐答案

答案会有点长.对于自定义输出,您必须添加RunListener 您可以使用以下示例实现来实现相同的目的.

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 =新的JUnitCore();

junitCore = new JUnitCore();

junitCore.addListener(new CustomExecutionListener());

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

RunListener的实现如下

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

通过覆盖RunListener中的方法,您可以格式化输出.

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

这篇关于在JUnit中打印测试结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆