全局设置一个JUnit运行器而不是@RunWith [英] Globally setting a JUnit runner instead of @RunWith

查看:277
本文介绍了全局设置一个JUnit运行器而不是@RunWith的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不用研究JUnit源本身(下一步),是否有一种简单的方法来设置要在每个测试中使用的默认Runner,而不必在每个测试中都设置@RunWith?我们有大量的单元测试,我希望能够全面增加一些支持而不必更改每个文件.

Without looking into JUnit source itself (my next step) is there an easy way to set the default Runner to be used with every test without having to set @RunWith on every test? We've got a huge pile of unit tests, and I want to be able to add some support across the board without having to change every file.

理想情况下,我希望这样的东西:-Djunit.runner ="com.example.foo".

Ideally I'm hope for something like: -Djunit.runner="com.example.foo".

推荐答案

我认为这不可能在全局范围内定义,但是如果编写自己的主函数是一种选择,则可以通过代码执行类似的操作.您可以创建自定义RunnerBuilder并将其与测试类一起传递给Suite.

I don't think this is possible to define globally, but if writing you own main function is an option, you can do something similar through code. You can create a custom RunnerBuilder and pass it to a Suite together with your test classes.

Class<?>[] testClasses = { TestFoo.class, TestBar.class, ... };
RunnerBuilder runnerBuilder = new RunnerBuilder() {
    @Override
    public Runner runnerForClass(Class<?> testClass) throws Throwable {
        return new MyCustomRunner(testClass);
    }
};
new JUnitCore().run(new Suite(runnerBuilder, testClasses));

它不会与Eclipse中的UI测试运行器集成在一起,但是对于某些自动化测试方案来说,它可能是一个选择.

This won't integrate with UI test runners like the one in Eclipse, but for some automated testing scenarios it could be an option.

这篇关于全局设置一个JUnit运行器而不是@RunWith的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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