5

Pester: Triangulation and reusing test cases

Pester has a great feature called TestCases. This feature enables you to easily call the same test code multiple times, but with different data.

This comes handy in two cases–triangulation and reusing test code.

Triangulation

Triangulation is one of the base principles of automated testing. You provide two or more examples of input & output and run it through the tested function. When done properly it will force you to come up with general implementation of the tested function.

In the following example we verify the behavior of the modulo operation. This operation returns the remainder of whole number division and is denoted as % in PowerShell.

Pester_Triangulation_01

Providing just the first two test cases, we could simply fool the test and return the value of the a. But the more test cases we provide, the more certain we are that the operation in fact behaves correctly.

Adding more test cases without the TestCases functionality would require a lot of copy-pasting and modifying the values. This is very error prone and would likely cause a lot of problems.

With TestCases it is as simple as this:

Describe 'Test reuse' {
    It 'Prerequisite file &lt;file&gt; is present' <code>
    -TestCases </code>
        @{file = '.\Script1.ps1'},
        @{file = '.\Script2.ps1'},
        @{file = '.\Script3.ps1'} `
    -Test {
        param ([string]$file)

        $file | Should Exist
    }
}

Pester_Triangulation_02

As you can see the test was executed once for each test case and produced a very clear output without barely any effort. You should also notice that you can use tokens named as the parameter names to show the input and output values for the given test case. In our example the parameters are $a, $b and $c and so the tokens are <a>, <b>, and <c>.

Test case reuse

Another scenario where the TestCases feature is very useful is for testing prerequisites.

Pester_Triangulation_03

In the above picture you can see that the test takes a path to a script and tests if that script file exist. On the first look it might seem pretty similar to triangulation, but the test does not point to a single function. Instead it points to multiple places which are determined by the input data.

In code it would look like this:

Describe 'Test reuse' {
It 'Prerequisite file is present' -TestCases
@{file = '.\Script1.ps1'},
@{file = '.\Script2.ps1'},
@{file = '.\Script3.ps1'} -Test {
param ([string]$file)

$file | Should Exist
}
}

Pester_Triangulation_04

Filed in: Articles

5 Responses to "Pester: Triangulation and reusing test cases"

  1. Ano says:

    The script for ‘Describe Triangulation’ is missing.

  2. Jaco de Weerd says:

    The correct code for triangulation:

    Describe ‘Triangulation’ {
    $TestCases = @(
    @{a=0 ; b=2 ; ExpectedResult=0},
    @{a=1 ; b=2 ; ExpectedResult=1},
    @{a=10 ; b=2 ; ExpectedResult=0},
    @{a=-11; b=2 ; ExpectedResult=-1},
    @{a=0 ; b=3 ; ExpectedResult=0},
    @{a=1 ; b=3 ; ExpectedResult=1},
    @{a=3 ; b=3 ; ExpectedResult=0},
    @{a=4 ; b=3 ; ExpectedResult=1},
    @{a=145; b=12; ExpectedResult=1}
    )

    It ‘Modulo , returns ‘ -TestCases $TestCases {
    param ([int]$a, [int]$b, [int]$ExpectedResult)
    Modulo $a $b | Should Be $ExpectedResult
    }

    }

  3. Jaco de Weerd says:

    Another try: in the code there are lessthan and greaterthan symbols. In this site they are treated as html tags.

    Please substirute [ and ] in the code below to lessthan en greaterthan symbols:

    Describe ‘Triangulation’ {
    $TestCases = @(
    @{a=0 ; b=2 ; ExpectedResult=0},
    @{a=1 ; b=2 ; ExpectedResult=1},
    @{a=10 ; b=2 ; ExpectedResult=0},
    @{a=-11; b=2 ; ExpectedResult=-1},
    @{a=0 ; b=3 ; ExpectedResult=0},
    @{a=1 ; b=3 ; ExpectedResult=1},
    @{a=3 ; b=3 ; ExpectedResult=0},
    @{a=4 ; b=3 ; ExpectedResult=1},
    @{a=145; b=12; ExpectedResult=1}
    )

    It ‘Modulo [a],[b] returns [ExpectedResult]‘ -TestCases $TestCases {
    param ([int]$a, [int]$b, [int]$ExpectedResult)
    Modulo $a $b | Should Be $ExpectedResult
    }

    }

Leave a Reply

Submit Comment

© 4307 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: