【问题标题】:Groovy Spock unit tests with closures带有闭包的 Groovy Spock 单元测试
【发布时间】:2013-10-17 11:37:41
【问题描述】:

如何在 Spock/groovy 中做到这一点?

package org.jenkinsci.plugins

import hudson.matrix.*
import spock.lang.*
import org.junit.Rule
import org.jvnet.hudson.test.JenkinsRule

class xxxx extends Specification {

    @Rule JenkinsRule rule = new JenkinsRule()

    def 'matrix'() {
        given:
        def matrixProject = rule.createMatrixProject()
        AxisList axl = new AxisList();
        def axis = new TextAxis('TEST', "1", "2", "3")

        axl.add(axis)
        matrixProject.setAxes(axl)

        expect: matrixProject.scheduleBuild2(0).get().logFile.text.contains("Some String!")

        matrixProject.scheduleBuild2(0).get().getRuns().each(){
            expect: it.logFile.text.contains("Another String")
        }
    }
}

具体来说,如何使用嵌套测试运行闭包? “另一个字符串”测试不起作用

【问题讨论】:

    标签: groovy jenkins-plugins spock


    【解决方案1】:

    这行得通吗?

    def 'matrix'() {
        given:
            def matrixProject = rule.createMatrixProject()
            def axis = new TextAxis('TEST', "1", "2", "3")
            matrixProject.axes.add(axis)
    
        expect:
            with( matrixProject.scheduleBuild2(0).get() ) {
                logFile.text.contains("Some String!")
                runs.every { it.logFile.text.contains("Another String") }
            }
        }
    }
    

    【讨论】:

    • 遗憾的是这不起作用org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack: No signature of method: org.jenkinsci.plugins.xxxxx.with() is applicable for argument types: (hudson.matrix.MatrixBuild, org.jenkinsci.plugins.xxxxxxx$_$spock_feature_0_2_closure3) values: [test0 #1, org.jenkinsci.plugins.xxxx$_$spock_feature_0_2_closure3@1adc811d] Possible solutions: with(groovy.lang.Closure), wait(), find(), wait(long), wait(long, int), Mock()我尝试使用.with{但也没有
    【解决方案2】:

    要么使用every 而不是each,要么使用嵌套的assert

    【讨论】:

      【解决方案3】:

      我不确定我是否理解你的问题。但是,如果 nested test 你的意思是在 each 闭包内评估语句,为什么不直接使用 assert

      expect:
      matrixProject.scheduleBuild2(0).get().logFile.text.contains("Some String!")
      matrixProject.scheduleBuild2(0).get().getRuns().each() {
          assert it.logFile.text.contains("Another String")
      }
      

      @tim_yates 的方法看起来也不错,更像是 Spock 的方法。不过我还没有测试过。

      编辑

      如果您想确保所有日志文件都包含测试字符串,请按照 Peter 的建议使用“每个”方法。

      expect:
      ...
      matrixProject.scheduleBuild2(0).get().getRuns().every {
          it.text.contains('Another String')
      }
      

      其他方法,如果您想知道有多少日志文件在测试失败时不包含测试字符串,请计算它们并将结果大小与零进行比较:

      expect:
      ...
      matrixProject.scheduleBuild2(0).get().getRuns().count {
          !it.text.contains('Another String')
      } == 0
      

      另外,如果您想知道哪些文件导致测试失败,请获取不包含测试字符串的文件的名称并将其与空列表进行比较:

      expect:
      ...
      matrixProject.scheduleBuild2(0).get().getRuns().findAll {
          !it.text.contains('Another String')
      }*.name == []
      

      【讨论】:

      • 这是一个,但它只运行到测试失败,所以 assert it.logFile.text.contains("TEST/1") 在第二次运行时失败,第三次根本没有被调用
      • 再次检查我的答案,我添加了一些其他解决方案。也许有人会更好地满足您的需求。
      • 谢谢 - 我修复了原始问题的逻辑(需要一个 setAxes,而不是 axis.add() - 现在 every 工作给 Condition not satisfied: b.getRuns().every(){ it.logFile.text.contains("TEST/1") } | | | | | false | [test0 » 1 #1, test0 » 2 #1, test0 » 3 #1] test0 #1
      猜你喜欢
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 2012-09-04
      • 2014-08-21
      • 2016-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多