且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

排除不修改测试类个人JUnit测试方法?

更新时间:2023-11-19 23:23:16

如果你不能在所有的你将会有一些严重的局限性触摸原始的测试。您压倒一切听起来就像是***的选择,但有几个变化:

If you can't touch the original test at all you are going to have some serious limitations. Your overriding sounds like the best bet, but with a couple of changes:

建立特别不包括超类蚂蚁的测试,从而使其他类,你不知道车子撞到。

Build the Ant tests specifically excluding the super classes, so that additional classes that you don't know about get run.

您可以使用@rule注释(新的JUnit 4.7)知道什么测试正在运行,并终止它(通过返回一个空语句实现),而不是覆盖特定的方法,让你知道更多的灵活性是否要避免了测试。用这种方法唯一的问题是,你不能使用此方法,这可能是缓慢停止运行@Before方法。如果这是一个问题(你真的不能碰测试)在重写的方法则是@Ignore我能想到的唯一的东西。

You can use the @Rule annotation (new to JUnit 4.7) to know what test is being run and abort it (by returning an empty Statement implementation) rather than overriding specific methods, giving you more flexibility in knowing whether or not to avoid the test. The only problem with this method is that you can't stop the @Before methods from running using this method, which may be slow. If that is a problem (and you really can't touch the tests) then @Ignore in the overridden method is the only thing I can think of.

如果,但是,您可以触摸这些测试,一些附加选项打开:

If, however, you can touch those tests, some additional options open up:

您可以通过指定的类@RunWith标签具有可定制的运行运行它们。这会亚军刚刚执行过到该项目中的标准亚军(JUnit4.class),但在你的项目中(通过系统属性或一些其他机制)将检查测试名称,而不是运行测试。这具有侵入性最小的优势,但也是最难实现的(选手都是毛茸茸的野兽的@rule的既定目标之一是消除大部分的需要,使他们)。

You could run them with a custom runner by specifying the @RunWith tag on the class. This runner would just pass over execution to the standard runner (JUnit4.class) in that project, but in your project (via a system property or some other mechanism) would inspect the test name and not run a test. This has the advantage of being the least intrusive, but the most difficult to implement (runners are hairy beasts, one of the stated goals of @Rule was to eliminate most of the need to make them).

另一种方法是使上,将检查一些配置设置如果测试应运行,这将是真正考验一个assumeThat声明。这实际上涉及到正确注射到测试,这是最有可能在任何一个大忌远程贴上了单独的项目。

Another is to make an assumeThat statement on the test that would check some configuration setting that would be true if that test should run. That would actually involve injecting right into the test, which is most likely a deal breaker in anything remotely labeled a "separate project."