从自动化测试套件中排除一些JUnit测试 [英] Exclude some JUnit tests from automated test suite

查看:208
本文介绍了从自动化测试套件中排除一些JUnit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当编写与外部资源交互的代码(例如使用Web服务或其他网络操作)时,我经常构造类,以便它也可以使用文件或其他输入方法存根。因此,我最终使用存根实现来测试系统的其他部分,然后是一个或两个专门测试调用Web服务的测试。

When writing code that interacts with external resources (such as using a web service or other network operation), I often structure the classes so that it can also be "stubbed" using a file or some other input method. So then I end up using the stubbed implementation to test other parts of the system and then one or two tests that specifically test calling the web service.

问题是我不知道我不想从Jenkins或我运行项目的所有测试(例如gradle test)调用这些外部服务。有些服务有副作用,或者可能无法供所有开发人员访问。

The problem is I don't want to be calling these external services either from Jenkins or when I run all of the tests for my project (e.g. "gradle test"). Some of the services have side effects, or may not be accessible to all developers.

现在我只是取消注释,然后在这些特定测试中重新评论@Test注释启用和禁用它们的方法。启用它,手动运行以检查它,然后记得再次注释掉它。

Right now I just uncomment and then re-comment the @Test annotation on these particular test methods to enable and disable them. Enable it, run it manually to check it, then remember to comment it out again.

// Uncomment to test external service manually
//@Test
public void testSomethingExternal() {

是否有这是一个更好的方法吗?

Is there is a better way of doing this?

编辑:对于手动单元测试,我使用Eclipse并且能够右键单击测试方法并执行Run As - > JUnit test。但是如果没有(未注释的)注释,这是行不通的。

For manual unit testing, I use Eclipse and am able to just right-click on the test method and do Run As -> JUnit test. But that doesn't work without the (uncommented) annotation.

推荐答案

我建议使用junit类别。有关详细信息,请参阅此博客: https://community.oracle.com/blogs/johnsmart/2010/04/25/grouping-tests-using-junit-categories-0

I recommend using junit categories. See this blog for details : https://community.oracle.com/blogs/johnsmart/2010/04/25/grouping-tests-using-junit-categories-0.

基本上,您可以将某些测试注释为特殊类别,然后您可以设置两个测试套件:一个运行该类别的测试,另一个忽略该类别中的测试(但运行其他所有内容)

Basically, you can annotate some tests as being in a special category and then you can set up a two test suites : one that runs the tests of that category and one that ignores tests in that category (but runs everything else)

@Category(IntegrationTests.class)
public class AccountIntegrationTest {

  @Test
  public void thisTestWillTakeSomeTime() {
    ...
   }

  @Test
  public void thisTestWillTakeEvenLonger() {
     ....
  }

}

你甚至可以注释个人测试

you can even annotate individual tests"

public class AccountTest {

   @Test
   @Category(IntegrationTests.class)
   public void thisTestWillTakeSomeTime() {
     ...
    }

任何时候我看到手动打开或关闭的东西我都会畏缩。

Anytime I see something manually getting turned on or off I cringe.

这篇关于从自动化测试套件中排除一些JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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