排除不修改测试类个人JUnit测试方法? [英] Exclude individual JUnit Test methods without modifying the Test class?

查看:214
本文介绍了排除不修改测试类个人JUnit测试方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在重新使用JUnit 4测试从反对我的code另一个项目。我直接获得他们从其他项目库作为我的自动化Ant构建的一部分。这是伟大的,因为它确保我把我的code绿对测试的最新版本。

I'm currently re-using JUnit 4 tests from another project against my code. I obtain them directly from the other project's repository as part of my automated Ant build. This is great, as it ensures I keep my code green against the very latest version of the tests.

不过,有一种我从来没有期望通过我的code测试的一个子集。但是,如果我开始添加@Ignore注解那些测试,我将不得不保持测试实施,这是我的我自己的单独副本真的不想做的事。

However, there is a subset of tests that I never expect to pass on my code. But if I start adding @Ignore annotations to those tests, I will have to maintain my own separate copy of the test implementation, which I really don't want to do.

有没有排除个别测试,而无需修改测试源的一种方式?以下是我已经看过迄今:

Is there a way of excluding individual tests without modifying the Test source? Here's what I have looked at so far:


  • 据我所看到的,蚂蚁junit任务只允许您以排除整个测试类,而不是单独的测试方法 - 所以这是不适合我,我需要方法的粒度

  • As far as I can see, the Ant JUnit task only allows you to exclude entire Test classes, not individual test methods - so that's no good for me, I need method granularity.

我认为放在一起,使用反射动态地查找并添加所有的原始测试,再加入code明确删除我不想运行的测试一个TestSuite。但是,我抛弃了这个想法时,我注意到, TestSuite的API 没有按'T提供了去除测试的方法。

I considered putting together a TestSuite that uses reflection to dynamically find and add all of the original tests, then add code to explicitly remove the tests I don't want to run. But I ditched that idea when I noticed that the TestSuite API doesn't provide a method for removing tests.

我可以创建扩展原有的测试类我自己的测试类,覆盖特定的测试,我不想跑,并与@Ignore注释它们。然后,我在我的子类中运行JUnit。这里的缺点是,如果新的测试类添加到原来的项目,我也不会自动接他们回家。我得监视新的测试类,它们被添加到原始项目。这是我最好的选择,到目前为止,但感觉并不理想。

I can create my own Test classes that extend the original Test classes, override the specific tests I don't want to run, and annotate them with @Ignore. I then run JUnit on my subclasses. The downside here is that if new Test classes are added to the original project, I won't pick them up automatically. I'll have to monitor for new Test classes as they are added to the original project. This is my best option so far, but doesn't feel ideal.

我能想到的唯一的另一种选择是想运行测试不好而忽略了失败。然而,这些测试需要一段时间才能运行(并失败!),所以我想preFER不运行它们。另外,我看不出告诉Ant任务来忽略具体的测试方法失败的一种方式。(再次 - 我看你怎么可以为单独的测试类做到这一点,但不是方法)

The only other option I can think of is to run the bad tests anyway and ignore the failures. However, these tests take a while to run (and fail!) so I'd prefer to not run them at all. Additionally, I can't see a way of telling the Ant task to ignore failures on specific test methods (again - I see how you can do it for individual Test classes, but not methods).

推荐答案

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

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."

这篇关于排除不修改测试类个人JUnit测试方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆