简体   繁体   English

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

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

I'm trying to write a unit test for a util function in a Jenkins shared library.我正在尝试为 Jenkins 共享库中的 util 函数编写单元测试。 The util function, calls the Office365Connector plugin in Jenkins. util 函数,调用 Jenkins 中的Office365Connector插件。

My util function looks like this:我的 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}"
        )
    }

}

The use case that I'm trying to test is that the office365ConnectorSend function is called.我尝试测试的用例是调用了office365ConnectorSend函数。

I've tried the following approach:我尝试了以下方法:

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")(_)
    }
}

running this on Jenkins gives me a java.lang.IllegalStateException: There is no pipeline variable mock for [office365ConnectorSend].在 Jenkins 上运行它给了我一个java.lang.IllegalStateException: There is no pipeline variable mock for [office365ConnectorSend]. , what am I doing wrong in this approach? ,我在这种方法中做错了什么?

After following this thread, I ended up calling the explicitlyMockPipelineStep function on the office365ConnectorSend plugin function.在关注线程后,我最终调用了office365ConnectorSend插件函数上的explicitlyMockPipelineStep office365ConnectorSend函数。 This made the function visible in the tests and I was able to resume testing.这使得该功能在测试中可见,我能够继续测试。

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

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