繁体   English   中英

测试在 jenkins 共享库中调用插件函数

[英]Testing that a plugin function is called in jenkins shared library

我正在尝试为 Jenkins 共享库中的 util 函数编写单元测试。 util 函数,调用 Jenkins 中的Office365Connector插件。

我的 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]. ,我在这种方法中做错了什么?

在关注线程后,我最终调用了office365ConnectorSend插件函数上的explicitlyMockPipelineStep office365ConnectorSend函数。 这使得该功能在测试中可见,我能够继续测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM