Describe and It

Groups of tests are written within a describe block. The describe block must be given a name. A describe block is often named after the subject of the tests.

Tests are declared using It followed by a description. The It statement contains assertions that are declared using Should.

Pester 4:
Pester 3 expected assertion keywords (Be, BeLike, and so on) to be written as a bare word. For example:
$value | Should Be 0
Pester 4 supports the syntax used by 3 as legacy syntax. The assertion names are now also presented as dynamic parameters. For example:
$value | Should -Be 0
This allows tools such as ISE and Visual Studio Code to provide auto-completion when Should - is typed.
The tests used as examples in this section use the syntax native to Pester 4.

The following function calculates the square root of a value. This particular function does not draw in information, except from the single parameter; testing is limited to validating output:

function Get-SquareRoot { 
    param ( 
        [Decimal]$Value 
    ) 
 
    if ($Value -lt 0) { throw 'Invalid value' } 
 
    $result = $Value 
    $previous = 0 
    while ([Math]::Abs($result - $previous) -gt 1e-300) { 
        $previous = $result 
        $result = ($result + $Value / $previous) / 2 
    } 
    return $result 
} 

Tests may be written to verify that the function does what it is expected to do:

Describe Get-SquareRoot { 
    It 'Returns a square root of 0 for a value of 0' { 
        Get-SquareRoot 0 | Should -Be 0 
    } 
 
    It 'Returns simple square root values' { 
        Get-Squareroot 1 | Should -Be 1 
        Get-SquareRoot 4 | Should -Be 2 
        Get-SquareRoot 9 | Should -Be 3 
        Get-SquareRoot 16 | Should -Be 4 
    } 
} 

Pester displays output showing the state of each of the tests:

Describing Get-SquareRoot
[+] Returns a square root of 0 for a value of 0 47ms
[+] Returns simple square root values 24ms

Each test, defined using It, returns a single line expressing the result of the test. A test may fail for two reasons:

  • The subject of the test has an error
  • The test has an error

For example, if an error is injected into the first test, the result will change, showing what about the test failed:

Describing Get-SquareRoot
[-] Returns a square root of 0 for a value of 0 42ms
Expected: {9}
But was: {0}
at <ScriptBlock>, : line 19
19: Get-SquareRoot 0 | Should Be 9
[+] Returns simple square root values 21ms

If a single test contains multiple Should assertions, conditions are evaluated in order until the first fails, or all pass.

For example, if two errors are injected into the last test, Pester is expected to indicate the test fails when it reaches the assertion that the square root of 9 is 33:

It 'Returns simple square root values' { 
    Get-Squareroot 1 | Should -Be 1 
    Get-SquareRoot 4 | Should -Be 2 
    Get-SquareRoot 9 | Should -Be 33 
    Get-SquareRoot 16 | Should -Be 44 
} 

Executing the tests shows an error once Pester reaches the third assertion, that the square root of 9 should be 33:

Describing Get-SquareRoot
[+] Returns a square root of 0 for a value of 0 32ms
[-] Returns simple square root values 30ms
Expected: {33}
But was: {3}
at <ScriptBlock>, : line 31
31: Get-SquareRoot 9 | Should Be 33

In this context, Pester will never execute the last assertion; the test has already failed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.202.61