为PowerShell函数编写测试 [英] Writing tests for PowerShell functions

查看:124
本文介绍了为PowerShell函数编写测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下功能(该功能是另一个功能的辅助功能,并且可以正常工作):

I have the following function (the function is an auxiliary function of an another function and it works correctly):

function Get-UserManager {
[CmdletBinding()]
param (
    [pscredential] $credential,
    [ValidateSet('ID')][string]$searchType,
    [string]$searchString,
)    

try {
    $reply = Invoke-RestMethod -Method Get -Uri $full_uri -Credentia $cred
} catch {
    Write-Verbose "Couldn't connect to the End Point"
    Write-Debug "$($_.Exception)"
    return $userListObject
}

$reply.elements | ForEach-Object {

    return $_    

  }

}

我需要为以下功能编写PowerShell测试(该测试必须包括所有可能的输出,因为我需要代码覆盖率为100%).

I am required to write a PowerShell test for the following function (The test must include all possible outputs, because I need the code coverage to be 100%).

有人可以帮助我如何编写PowerShell测试以检查此功能的所有可能输出吗?

Can someone please help me how do I write a PowerShell test that can check all the possible outputs of this function?

测试应该是这样的:

$moduleRoot = Resolve-Path "$PSScriptRoot\.."
$moduleName = Split-Path $moduleRoot -Leaf
$cred = Get-Credential

Describe "Demonstarting Code Coverage of: $moduleName" {

 It "Calls Function: get-UserManager" {
    {Get-UserManager -credential $cred -searchType ID -searchString 
    '12345' -delimiter} | Should Be $userListObject
}

}

推荐答案

您的代码目前无法正常运行,我认为是因为您减少了代码的使用量,使其可以在StackOverflow上共享,但是省略了一些关键要素.例如,没有填充$full_uri$userListObject,但在函数中使用了$full_uri$userListObject,因此在param块中还有一个逗号.

Your code is currently non-functional, I assume because you reduced it to share on StackOverflow but have left some key elements out. For example $full_uri and $userListObject aren't populated but are used in the function and you have an extra comma in your param block.

话虽这么说,您可能想采用模拟方法来模拟脚本的某些部分,以便您可以强制发生不同的行为并访问每条路径,以获取100%的代码覆盖率.例如,您需要进行测试,其中API返回异常并输入您的Catch块.可能看起来像这样:

That being said, you probably want to take the approach of using Mocking to simulate parts of your script so you can force different behaviour to occur and visit every path in order to get 100% code coverage. For example you need a test where the API returns an exception and enters your Catch block. That might look like this:

Describe "Demonstarting Code Coverage of: $moduleName" {

    Context 'Unable to connect to the endpoint' {

        Mock Invoke-RestMethod { Throw 'Endpoint unavailable' }
        Mock Write-Verbose
        Mock Write-Debug

        It 'Should enter the catch block when the endpoint returns an error' {
            Get-UserManager -Verbose -Debug
            Assert-MockCalled Write-Verbose -Times 1 -Exactly
            Assert-MockCalled Write-Debug -Times 1 -Exactly
        }
    }
}

如果您是Pester的新手,那么起初,嘲弄可能是一个棘手的话题.我建议先在Pester上进行一些学习.去年我在PSDay上做了关于Pester入门的演讲,您可能会发现翔实的信息

If you're completely new to Pester, Mocking can be a tricky topic to get your head around at first. I recommend doing some learning on Pester first. I did a talk on Getting Started with Pester at PSDay last year that you might find informative.

这篇关于为PowerShell函数编写测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆