Home > Blockchain >  How to write a small test function using pester testing framework
How to write a small test function using pester testing framework

Time:10-23

How to write a small test function using pester testing framework

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

CodePudding user response:

We have written a sample test function using pester frame work & tested in local environment as well. Below is the sample Get-Something function here is the function module:

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

Here are the function module tests cases

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!"
        }
    }
}

For more information, you can refer this blog or pester documentation to Quick start with pester testing frame work.

  • Related