如何仅对已更改的源文件运行单元测试?
有没有办法让 ant 仅为它构建的 java 类运行单元测试? 例如,如果 MyClass.java 已过时,ant 将构建 MyClass.class。 之后,我希望它也运行 MyClassTest 和 MyClassTestSuite(如果存在)。 它不必基于命名约定。 我可以使用注释或任何其他有效的方法。
编辑:有几个人插话说这是一个坏主意。 如果我计划在不运行所有单元测试的情况下签入,那么可能会这样。 我的主要项目有超过 16k 个单元测试,运行时间大约为 20 分钟。 我将在签入之前运行它们,但是每次更改文件时都运行整个套件是完全不切实际的。 抱歉,我应该提供更多背景信息。
Is there a way I can get ant to run the unit tests just for the java classes it builds? For example, if MyClass.java is out of date, ant will build MyClass.class. After that I want it to also run MyClassTest and MyClassTestSuite if they exist. It doesn't have to be based on a naming convention. I'd be fine with using annotations or any other method that works.
EDIT: Several people have chimed in saying this is a bad idea. It would be, if I planned on checking in without running all of our unit tests. My main project has over 16k unit tests which take about 20 minutes to run. I'll run them all before check-in, but it's completely impractical to run the whole suite every time I change a file. Sorry, I should have given more context.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我不推荐这种方法,很有可能会遗漏由副作用引起的错误。
I would not recommend this approach, There is a good chance of missing errors introduced by side effects.
这没那么容易...
看看 Atlassian Clover。 它具有您梦想的功能,但它不是免费的。
It's not that easy...
Take a look at Atlassian Clover. It has the feature you dream about, but it's not free.
对我来说这似乎是个坏主意。 当然,更改后的源文件需要测试,但还需要测试依赖于这些文件的任何文件,以及任何依赖于这些文件的文件等。您必须跟踪哪个文件依赖于其他文件,这是一个需要解决的相当大的问题。
在我看来,如果项目足够大,在尝试解决测试的依赖树方面有很大的收获,也许项目太大了? 也许是时候将其分成单独的项目来协同工作,每个项目都可以彼此分开进行测试。
That seems like a bad idea to me. Sure the changed source files need testing, but also any files that depend on those files, and any that depend on those, etc. You would have to track which file depend on what other files, and thats a rather big problem to solve.
In my opinion, if the project is big enough that there is a significant gain in trying to resolve dependency trees of testing, maybe the project is too big? Maybe it is time to split it up into separate projects that work together, which could each be tested apart from each other.
看一下 Infinitest,它是 Eclipse(和 Intellij)的一个插件,可以根据您刚刚更改的源文件运行测试:
Infinitest 网站
有关概述,请参阅此处的截屏视频:
截屏概述
Take a look at Infinitest, it's a plugin for Eclipse (and Intellij) that runs tests based on the source files you have just changed:
Infinitest Website
For an overview see the screencast here:
Screencast Overview
据我所知,无法使用 ANT 执行此操作,但您可以在 Eclipse 中执行类似的操作。
Kent Beck 编写了一个名为 JUnit Max 的 Eclipse 插件,它可以在保存代码后运行单元测试。 它首先运行“最有可能失败”的测试。
JUnit Max
There is no way I know of to do this with ANT, but you can do something like this in Eclipse.
Kent Beck has written an Eclipse plugin called JUnit Max that runs your unit tests after you save your code. It runs the tests "most likely to fail" first.
JUnit Max
我想和其他人一起说这是你想要避免的事情。 仅运行已更改的特定类的测试用例会带来风险,您所做的更改会破坏对未更改但依赖于某些已更改代码的类的测试。
即使您积极对接口进行编程并在单元测试中模拟依赖关系,这也是一种风险。 例如,静态实用程序类可能有很多直接使用它们的类。 在这里,很容易想象这样一种情况,您可以对静态实用程序方法进行更改,确保该方法的测试有效,并且如果您没有运行所有的代码,则可以破坏调用该方法的大量代码。测试。
I'd like to chime in with the other folks that have said that this is something you want to avoid. Running only the test cases for the specific classes which have changed introduces a risk that you will make a change which breaks a test for a class which wasn't changed but has a dependency on some of the code that has been changed.
This is a risk even if you're aggressive about programming to an interface and mocking dependencies in unit tests. For example, static utility classes are likely to have a lot of classes using them directly. Here, it would be easy to imagine a case where you could make a change to a static utility method, ensure that the tests for that method worked, and break a ton of code that called the method if you didn't run all of your tests.
好吧,如果您遵循命名约定,您可能可以使用变量来完成此操作。 获取已更改文件的列表,并运行相应的测试。 但我建议不要这样做,就像其他答案所解释的那样。
Well, you could probably do it using variables, if you follow a naming convention. Get a list of changed files, and run the corresponding tests. But I would recommend against it, like the other answers explain.