繁体   English   中英

如何使用pester测试框架编写一个小的测试函数

[英]How to write a small test function using pester testing framework

如何使用pester测试框架编写一个小的测试函数

function SaveStudent($Student){
 Write-Host "save student email for $($Student.Email)
 return $Student 
}

我们已经使用 pester 框架编写了一个示例测试函数并在本地环境中进行了测试。 下面是示例 Get-Something 函数,这里是函数模块:

    function Get-Something {    
    [CmdletBinding()]
    param (
        [Parameter()]
        [string]
        $ThingToGet
    )
 
    if ($PSBoundParameters.ContainsKey('ThingToGet')) {
        Write-Output "I got $ThingToGet!"
    }
    else {
        Write-Output "I got something!"
    }
}

以下是功能模块测试用例

Describe "Get-Something" {
    Context "when parameter ThingToGet is not used" {
        It "should return 'I got something!'" {
            Get-Something | Should -Be 'I got something!'
        }
    }
 
    Context "when parameter ThingToGet is used" {
        It "should return 'I got ' follow by a string" {
            $thing = 'a dog'
            Get-Something -ThingToGet $thing | Should -Be "I got $thing!"
        }
    }
}

有关更多信息,您可以参考此博客或纠缠文档快速启动纠缠测试框架工作。

暂无
暂无

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

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