简体   繁体   English

如何为我的PowerShell函数编写Pester测试

[英]How to write Pester tests for my PowerShell function

I have the following function: For this function I'm required to write a Pester code that will have 100% coverage. 我有以下功能:对于这个功能,我需要编写一个具有100%覆盖率的Pester代码。

function buildFilterString {
[CmdletBinding()]
param (
    [string] $input_str, 
    [char] $delimiter
)

$out = $null

$input_str -split $delimiter | ForEach-Object {
    $item = $_
    $out += "'" + $item + "',"
}

$out.Substring(0,$out.Length-1)
}

As I understood the test should look like this, but I'm not sure how to write the "different cases" in the test. 据我所知,测试应该是这样的,但我不确定如何在测试中编写“不同的情况”。

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

Describe "Demonstarting Code Coverage of: $moduleName" {

 It "Calls Function: builedFilterString" {
    {buildFilterString -input_str "happy" -delimiter ';'} | Should Be 
     "happy"
  }
}

In this case you just need to make sure the -input_str contains the specified -delimiter and all you're code is covered. 在这种情况下,您只需要确保-input_str包含指定的-delimiter并且涵盖了所有代码。 You could keep the stated case were the -input_str does not contain the specified -delimiter , to test the edge case (which will fail by the way, because $out will stay $null ). 您可以保持所述的情况-input_str不包含指定的-delimiter ,以测试边缘情况(顺便说一下,因为$out将保持$null )。

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

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