0

所以我在我的 vars 文件夹中有一个脚本化的共享管道库,它在最后返回一些值:

def call() {
  def a = 3
  //does stuff
  return a
}

现在我尝试像这样测试它:

def "example test"() {
  when:
  def result = scriptUnderTest.call()
  then:
  result == 3
}

这不起作用,因为结果将始终为空。我已经用 Jenkins-Spock 针对不同的场景编写了相当多的测试,所以基本机制很清楚。但是在这种情况下我错过了什么?

4

1 回答 1

1

问题可能在//does stuff一部分。
这是一个返回值的步骤的工作测试:

在里面sayHello.groovy我们定义了一个从 shell 获取标准输出并连接到它的步骤:

def call() {
    def msg = sh (
        returnStdout: true,
        script: "echo Hello"
    )
    msg += " World"
    return msg
}

在里面sayHelloSpec.groovy我们编写单元测试并检查返回值:

import com.homeaway.devtools.jenkins.testing.JenkinsPipelineSpecification

public class sayHelloSpec extends JenkinsPipelineSpecification {

    def "sayHello returns expected value" () {
        def sayHello = null

        setup:
            sayHello = loadPipelineScriptForTest("vars/sayHello.groovy")
            // Stub the sh step to return Hello
            getPipelineMock("sh")(_) >> {
                return "Hello"
            }

        when:
            def msg = sayHello()

        then:
            msg == "Hello World"
    }

}
于 2021-12-21T20:48:42.860 回答