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

查看:18
本文介绍了从自动化测试套件中排除一些 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 测试.但是如果没有(未注释的)注释,这将不起作用.

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天全站免登陆