簡體   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