在模块中自定义 Junit 测试套件的执行
我有一组 Junit 4 测试类,我想在具有不同 BeforeClass、AfterClass、Before 和 After 挂钩的多个模块中运行它们。通过 @Rule 注入,我只能得到测试方法的包装,但没有 BeforeClass 和 AfterClass 行为。
另外,我不想使用测试运行程序执行此操作,因为从那时起我已经修复了测试运行程序以用于大量测试。
对目标模块中的每个测试类进行子类化并在那里应用自定义似乎不是一个好的解决方案。
最好的方法是在目标模块中声明类似的内容
@RunWith(Suite.class)
@Suite.SuiteClasses({
investmentTests.class,
catalogTests.class,
markerTests.class
})
public class AllTests {
// why on earth I need this class, I have no idea!
}
,并使用一些环境挂钩来应用之前/之后的代码。
您遇到过这个问题的解决方案吗?
I have a set of Junit 4 test classes which I want to run in multiple modules with differing BeforeClass, AfterClass, Before and After hooks. With @Rule
injections I get only wrapping of test methods, but no BeforeClass and AfterClass behaviour.
Also I don't want to do this with a test runner, since then I have fixed the test runner to be used for a large set of tests.
Subclassing each test class in the target modules and applying the customizations there doesn't seem to be a good solution.
The best would be to just declared something like this in the target modules
@RunWith(Suite.class)
@Suite.SuiteClasses({
investmentTests.class,
catalogTests.class,
markerTests.class
})
public class AllTests {
// why on earth I need this class, I have no idea!
}
and have some environmental hooks to apply the before/after code.
Have you come across a solution for this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 NamshubWriter 所评论的,org.junit.ClassRule 是在 JUnit4.9 中找到的。
@ClassRule
而不是@Rule
;TestRule
而不是MethodRule
。查看示例:
As NamshubWriter commented, org.junit.ClassRule is found in JUnit4.9.
@ClassRule
instead of@Rule
;TestRule
instead ofMethodRule
.See the sample: