How to write a custom Junit runner that will log results

陌路散爱 提交于 2020-01-04 02:07:10

问题


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:


Two alternatives

  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);
    }
    
  2. 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);
    }
    

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



来源:https://stackoverflow.com/questions/19552351/how-to-write-a-custom-junit-runner-that-will-log-results

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