unit-testing - 测试在 jenkins 共享库中调用插件函数
问题描述
我正在尝试为 Jenkins 共享库中的 util 函数编写单元测试。util 函数,调用Office365Connector
Jenkins 中的插件。
我的 util 函数如下所示:
#!/usr/bin/env groovy
import pe.ui.BuildNotificationSettings
import pe.ui.BuildStates
def call(BuildNotificationSettings options, BuildStates status) {
options.teamsChannelWebhookUrlList.each {
office365ConnectorSend (
status: status.toString(),
webhookUrl: "$it",
color: BuildStates.valueOf(status.toString()).color,
message: "Build ${status.toString()}: ${JOB_NAME} - ${BUILD_DISPLAY_NAME}<br>Pipeline duration: ${currentBuild.durationString}"
)
}
}
我要测试的用例office365ConnectorSend
是调用该函数。
我尝试了以下方法:
import com.homeaway.devtools.jenkins.testing.JenkinsPipelineSpecification
import pe.ui.BuildNotificationSettings
import pe.ui.BuildStates
public class _sendTeamsMessageSpec extends JenkinsPipelineSpecification {
def _sendTeamsMessage = null
def setup() {
_sendTeamsMessage = loadPipelineScriptForTest("vars/_sendTeamsMessage.groovy")
def office365ConnectorSend = Mock(office365ConnectorSend)
}
def "returns without sending no build notification settings are passed" () {
given:
def options = new BuildNotificationSettings(
shouldSendNotification: null,
teamsChannelWebhookUrlList: null
)
when:
def result = _sendTeamsMessage(options, null)
then:
0 * explicitlyMockPipelineVariable("office365ConnectorSend")(_)
}
}
在 Jenkins 上运行它给了我一个java.lang.IllegalStateException:
There is no pipeline variable mock for [office365ConnectorSend].
,我在这种方法中做错了什么?
解决方案
在关注这个线程之后,我最终explicitlyMockPipelineStep
在office365ConnectorSend
插件函数上调用了该函数。这使得该功能在测试中可见,我能够恢复测试。
推荐阅读
- java - 当我的变量是 int 类型时,有没有办法检查 .chars 是否为空?
- javascript - 冻结已呈现表中列的宽度
- xml - 如何使用 Java 中的 Jaxb2Marshaller 在最终 XML 中禁用 xmlns targetNamespace
- qml - 如何在 Cpp 中为 Qml 制作打印助手
- android - 标记在 Android 的警报日志中意味着什么?
- asp.net - 三位小数在数据集中显示为 4 位小数
- android - 在 Android 上发布 React Native 项目时出现“应用程序已停止”错误
- java - 生成图表时文件数据解析不完整
- php - 我正在使用 NelmioApiDocBundle 来记录我的 API。如何在注释中强制执行验证?
- javascript - Ant Design 组件 initialValue 不适用于 validateFields