如何编写自定义的Junit运行程序以记录结果 [英] How to write a custom Junit runner that will log results

查看:81
本文介绍了如何编写自定义的Junit运行程序以记录结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JUnit,并且我有要实现的想法. 我想编写一个运行程序,它将每个测试的结果记录到excel或文本文件中,以便我可以将其附加到我的报告中. 我需要学习什么才能开始使用

I am working with JUnit and I have this idea which I want to implement. I want to write a runner that will log the results of each test to an excel or a text file so that i can attach it in my reports. What do i need to learn to get started

推荐答案

两种选择

  1. 编写一个 RunListener 并按以下方式使用它:

  1. Write a RunListener and use it like:

public void main(String... args) { 
    JUnitCore core= new JUnitCore();
    core.addListener(new MyRunListener());
    core.run(MyTestClass.class);
}

  • 再次编写 RunListener .但是这次扩展了org.junit.runner.Runner实现,并覆盖了它的 run 方法,如

  • Write a RunListener again. But this time extend an org.junit.runner.Runner implementation and override its run method like

    @Override
    public void run(RunNotifier notifier) {
        notifier.addListener(new MyRunNotifier());
        super.run(notifier);
    }
    

  • 第二种方法也可以用于带有 @RunWith(MyRunner.class) 批注的测试中.

    Second approach can also be used in tests with @RunWith(MyRunner.class) annotation.

    这篇关于如何编写自定义的Junit运行程序以记录结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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