Ant 可以根据继承层次结构对 JUnit 测试进行分类吗?
我正在处理一个遗留项目,该项目具有:
- 纯单元测试
- 集成测试(运行缓慢;具有各种令人讨厌的依赖项)
我正在寻找分别运行两种类型的测试的最简单方法 /em> 与 Ant。
我想知道是否有一种方法可以让 Ant 根据继承层次结构自动识别这两个类别:
StringUtilsTest extends TestCase // "pure unit test"
vs
ProductionDBTest extends AbstractTransactionalTesterBase // "integration test"
集成测试所基于的抽象超类层次结构,但它们都归结为一些 Spring 测试类,最终是 AbstractSpringContextTests 扩展了junit.framework.TestCase
。
换句话说,我可以在 Ant 中区分(间接)扩展 AbstractSpringContextTests
的测试和直接扩展 TestCase
的测试吗?或者我是否必须手动完成测试,例如将它们放入单独的 类别 或 TestSuites 中? (有很多测试,所以我不想这样做。)
解决方案:我尝试了 Sean 的(非常有希望)方法,但无法让它工作(轻松)。因此,我最终还是半手动地完成了测试,使用 这个问题,并使用 Ant 像这样。 (请注意,编写自定义 TestRunner 并不是必需的。)
I'm working with a legacy project that has:
- Pure unit tests
- Integration tests (slow to run; have all sorts of nasty dependencies)
I'm looking for the simplest way to run both types of tests separately with Ant.
I wonder if there's a way to have Ant automatically recognise these two categories based on the inheritance hierarchy:
StringUtilsTest extends TestCase // "pure unit test"
vs
ProductionDBTest extends AbstractTransactionalTesterBase // "integration test"
There's a hierarchy of abstract superclasses that integration tests are based on, but they all come down to some Spring test classes and ultimately AbstractSpringContextTests
which extends junit.framework.TestCase
.
In other words, can I distinguish, in Ant, tests that (indirectly) extend AbstractSpringContextTests
and tests that directly extend TestCase
? Or would I have to manually go through the tests and e.g. put them in separate Categories or TestSuites? (There are many tests so I wouldn't want to do that.)
Resolution: I tried Sean's (very promising) approach, but couldn't get it working (easily). So I ended up going through the tests semi-manually after all, annotating the pure ones (which was the smaller group) using a setup described in this SO question, and running them with Ant like this. (Note that writing a custom TestRunner is not necessary.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们用于对 JUnit 测试进行分类的解决方案是使用自定义注释。然后,您可以使用自定义 TestRunner,可以为其指定要运行的测试类型或所有测试类型的标志或参数。
抱歉,没有示例代码,但创建注释和 TestRunner 非常基本,请尝试一下!
The solution we used for categorizing our JUnit tests was using a custom annotation. You can then use a custom TestRunner with that which can be given a flag or argument as to which test types to run, or all of them.
Sorry, no example code but creating annotations and a TestRunner is pretty basic, give it a try!
简单的方法是以测试类型结尾的测试类命名
假设我们正在测试日期。
然后有两个目标,一个是快速单元测试
,然后您创建一些 ant 目标,一个用于快速测试,一个用于系统测试,一个执行所有目标。类似的事情。
也就是说,您是否可以重命名您的测试类。
The simple way is to name your test classes ending with the test type
Lets say we were testing dates.
Then in there were two targets one the quick unit tests has
You then create a few ant targets, one for quick tests one for sys tests and one do them all.. Something like that.
That is to say if you can rename your test classes.
请注意,我自己没有使用过这个。但我知道您将单元测试表示为作为资源集合运行,并且可以使用 限制。并且,一种可能的限制是“instanceof”,它允许您选择基于超类的类。这允许您选择集成测试,然后使用资源集合“差异”功能您可以选择其他所有内容。如果不是微不足道的话,也不算太糟糕。我认为这在 Ant 1.8+ 中有效。
I haven't used this myself, mind. But I'm aware that you express unit tests to run as a resource collection, and those can be defined with restrictions. And, one possible restriction is "instanceof" which lets you select classes based on superclass. That lets you select the integration tests, and then using the resource collection "difference" function you can select everything else. Not too bad, if not trivial. This works in Ant 1.8+ I think.