使用 jenkins-spock 在单元测试中评估来自 Jenkins 共享库的 return 值
Evaluate return value from Jenkins shared library in unit test with jenkins-spock
所以我的 vars 文件夹中有一个脚本共享管道库,returns 最后有一些值:
def call() {
def a = 3
//does stuff
return a
}
现在我试着这样测试它:
def "example test"() {
when:
def result = scriptUnderTest.call()
then:
result == 3
}
这行不通,因为结果始终为空。
我已经用 Jenkins-Spock 为不同的场景编写了很多测试,所以基本机制很清楚。但是在这种情况下我错过了什么?
问题可能在 //does stuff
部分。
这是 return 一个值的步骤的工作测试:
在 sayHello.groovy
中,我们定义了一个从 shell 获取标准输出并连接到它的步骤:
def call() {
def msg = sh (
returnStdout: true,
script: "echo Hello"
)
msg += " World"
return msg
}
在 sayHelloSpec.groovy
中,我们编写单元测试并检查 return 值:
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"
}
}
所以我的 vars 文件夹中有一个脚本共享管道库,returns 最后有一些值:
def call() {
def a = 3
//does stuff
return a
}
现在我试着这样测试它:
def "example test"() {
when:
def result = scriptUnderTest.call()
then:
result == 3
}
这行不通,因为结果始终为空。 我已经用 Jenkins-Spock 为不同的场景编写了很多测试,所以基本机制很清楚。但是在这种情况下我错过了什么?
问题可能在 //does stuff
部分。
这是 return 一个值的步骤的工作测试:
在 sayHello.groovy
中,我们定义了一个从 shell 获取标准输出并连接到它的步骤:
def call() {
def msg = sh (
returnStdout: true,
script: "echo Hello"
)
msg += " World"
return msg
}
在 sayHelloSpec.groovy
中,我们编写单元测试并检查 return 值:
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"
}
}