如何查找不属于任何套件的单元测试?
使用 JUnit 3,如果您忘记将测试添加到套件中,它将无法运行。 如何找到不属于我们的顶级套件或其递归包含的任何套件的所有 JUnit 测试用例?
With JUnit 3, if you forget to add a test to a suite, it will not get run. How can I find all JUnit test cases that are not part of our top level suite, or any suite it recursively contains?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 UCDetector,您应该能够从 Eclipse 中识别未使用的公共类(其中包括未使用的测试用例)。
Using UCDetector, you should be able to identify unused public classes (which include your unused TestCases) from within Eclipse.
最简单的方法是使用 ant junit 任务和测试命名约定(例如匹配 *Test.java)。
然后您可以告诉 junit 运行项目中 *Test.java 中的每个测试。 下面是一个截取自ant 手册的示例。
The easiest way to do this is to use the ant junit task and a naming convention for tests (matches *Test.java for example).
Then you can tell junit to run every test in *Test.java in your project. Below is a truncated example taken from the ant manual.
我们在项目中所做的就是迭代所有类文件并查看扩展的 TestCase(您引用该文件,执行 Class clazz = Class.forName(),然后执行 Test.isAssignableFrom(clazz) 进行检查。
它肯定会变得混乱(例如,有时某些东西是不应该运行的基类,并且必须对其进行标记),
我会使用自己的套件构建器类。并要求使用它构建所有套件,并向其添加逻辑以记录构建器引用的所有现有类,然后构建器将它们传递给检查器,检查器知道它们已被考虑在内
。这在 JUnit4 中被打破,使其成为即时遗留代码。
最好、最稳定的解决方案(直到 JUnit 解决其套件构建问题)是拥有一种开发文化,即使用命名转换来命名所有测试(标准以 Test 结尾)。 )并让 Ant 运行它们,或者在开发测试时仅通过套件运行它们。
What we did on our project was to iterate through all of the class files and see what extended TestCase (you reference the file, do a Class clazz = Class.forName() and then do a Test.isAssignableFrom(clazz) to check.
It definitely got messy (sometimes something is a base class that isn't supposed to be run, for example, and those would have to be tagged).
Knowing if it is in a suite is even harder. I would use my own suite builder class and require all suites be built using it, and add logic to it to record all existing classes that are referenced by the builder, which then passes them to the checker, and the checker knows that they are accounted for.
The problem is that all of this breaks in JUnit4, making it instant legacy code.
The best, most stable solution (until JUnit solves its suite building issues) is to have a development culture that either names all of its tests with a naming conversion (the standard is ending in Test) and has Ant run them, or while developing the test only runs them via the suite.
包含测试包的代码覆盖率将拾取任何未运行的测试类
Code coverage that includes your test package would pick up any test classes that aren't being run