【问题标题】:Is there a concept of test suites in Gradle/Spock land?Gradle/Spock 中是否有测试套件的概念?
【发布时间】:2016-02-24 21:08:52
【问题描述】:

此处使用 Spock 进行单元测试的 Groovy/Gradle 项目。

Spock 和/或 Gradle 是否支持测试套件或命名测试集?由于此问题范围之外的原因,CI 服务器无法运行某些 Spock 测试 (Specifications)。

因此,最好将我应用的所有 Spock 测试分成两组:

  1. "ci-tests";和
  2. local-only-tests

然后也许我们可以通过以下方式调用它们:

./gradlew test --suite ci-tests

等等。这可能吗?如果是这样,设置/配置是什么样的?

【问题讨论】:

  • Spock 基于 JUnit,所以我认为您可以使用标准方法来开发套件。对于 Gradle 集成,请查看 junit 的 Gradle 单元测试插件文档。

标签: gradle groovy spock


【解决方案1】:

您可以使用 Spock 注释 @IgnoreIf( ) 来注释不应在 CI 服务器中运行的测试。

在此处查看文档:https://spockframework.github.io/spock/docs/1.0/extensions.html#_ignoreif

您需要做的就是让 CI 服务器设置一个环境变量,如果设置了该变量,则排除测试类。

Spock 甚至在闭包内也有一些属性来简化操作:

@IgnoreIf({ sys.isCiServer })

【讨论】:

    【解决方案2】:

    我会设置一个子模块my-app-ci-test,在 build.gradle 中包含以下内容:

    test {
        enabled = false
    }
    task functionalTest(type: Test) {
    }
    

    然后您将测试放在src/test/groovy 中并运行./gradlew functionalTest

    或者,您可以将它们包含在同一个模块中,并使用 includes / excludes 配置 testfunctionalTest 任务

    test {
        exclude '**/*FunctionalTest.groovy'
    }
    task functionalTest(type: Test) {
        include '**/*FunctionalTest.groovy'
    }
    

    【讨论】:

      【解决方案3】:

      如果您使用 Junit test-runner 进行 Spock 测试,您可以使用 @Category 注释。示例by articleofficial documentation

       public interface FastTests {
       }
      
       public interface SlowTests {
       }
      
       public interface SmokeTests
       }
      
       public static class A {
           @Test
           public void a() {
               fail();
           }
      
           @Category(SlowTests.class)
           @Test
           public void b() {
           }
      
           @Category({FastTests.class, SmokeTests.class})
           @Test
           public void c() {
           }
       }
      
       @Category({SlowTests.class, FastTests.class})
       public static class B {
           @Test
           public void d() {
           }
       }
      
      test {
          useJUnit {
              includeCategories 'package.FastTests'
          }
          testLogging {
              showStandardStreams = true
          }
      }
      

      【讨论】:

      【解决方案4】:

      您可以使用以下SpockConfiguration.groovy 允许通过系统属性传递包含/排除

          runner {
              exclude {
                  System.properties['spock.exclude.annotations']
                      ?.split(',')
                      *.trim()
                      ?.each {
                          try {
                              annotation Class.forName(it)
                              println "Excluding ${it}"
                          } catch (ClassNotFoundException e) {
                              println "Can't load ${it}: ${e.message}"
                          }
                      }
              }
              include {
                  System.properties['spock.include.annotations']
                      ?.split(',')
                      *.trim()
                      ?.each {
                          try {
                              annotation Class.forName(it)
                              println "Including ${it}"
                          } catch (ClassNotFoundException e) {
                              println "Can't load ${it}: ${e.message}"
                          }
                      }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2015-07-08
        • 2022-08-17
        • 2014-11-16
        • 1970-01-01
        • 2021-07-28
        • 1970-01-01
        • 2021-01-03
        • 2017-01-22
        • 1970-01-01
        相关资源
        最近更新 更多