因此,我在vars文件夹中有一个脚本化的共享管道库,在最后返回一些值:
def call() {
def a = 3
//does stuff
return a
}现在我试着像这样测试它:
def "example test"() {
when:
def result = scriptUnderTest.call()
then:
result == 3
}这是行不通的,因为结果总是为空。我已经为不同的场景编写了一些与Jenkins有关的测试,所以基本的机制是明确的。但我在这个案子里漏掉了什么?
发布于 2021-12-21 20:48:42
捕获可能在//does stuff部分。
下面是一个返回值的步骤的工作测试:
在sayHello.groovy中,我们定义了一个步骤,该步骤从shell获取stdout并连接到它:
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"
}
}https://stackoverflow.com/questions/70203792
复制相似问题