Port of schuchert.wikispaces.com


PowerShell5.GettingSetUp

PowerShell5.GettingSetUp

Overview

For this point forward, we assume you have a working environment. To verify this, here is a a quick smoke test of your environment to make sure things work well enough to proceed. After this, we’ll look at implementing an algorithm to practice the TDD cycle. Finally, we’ll dig into a larger problem to look into OOP and Design Patterns.

Primordial Beginnings

A good start is to verify that you can run tests, they fail, and you can make them pass. So to get started, open up an instance of PowerShell and try the following:

    PS C:\Users\Brett> mkdir pester_take1
        Directory: C:\Users\Brett
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    d-----        9/25/2017   9:50 PM                pester_take1
    PS C:\Users\Brett> cd .\pester_take1\
    PS C:\Users\Brett\pester_take1> invoke-pester
    Executing all tests in '.'
    Tests completed in 0ms
    Tests Passed: 0, Failed: 0, Skipped: 0, Pending: 0, Inconclusive: 0

This tells me that we can run Pester from the shell. We will look at a few more ways to run tests, but this is enough to get started.

First Failing Test

A good practice is to make sure tests fail before they pass. This makes it easy to verify that your tests are running.

    Describe "Trivial Tests" {
      It "Should know how to sort an array of size 1" {
        $arr = @()
    
        $arr.Count | Should Be 1
      }
    }
    PS C:\Users\Brett\pester_take1> invoke-pester
    Executing all tests in '.'
    
    Executing script C:\Users\Brett\pester_take1\Trivial.Tests.ps1
    
      Describing Trivial Tests
        [-] Should know how to sort an array of size 1 442ms
          Expected: {1}
          But was:  {0}
          5:     $arr.Count | Should Be 1
          at Invoke-LegacyAssertion, C:\Program Files\WindowsPowerShell\Modules\Pester\4.0.8\Functions\Assertions\Should.ps1
    : line 190
          at <ScriptBlock>, C:\Users\Brett\pester_take1\Trivial.Tests.ps1: line 5
    Tests completed in 442ms
    Tests Passed: 0, Failed: 1, Skipped: 0, Pending: 0, Inconclusive: 0
    Describe "Trivial Tests" {
      It "Should know how to sort an array of size 1" {
        $arr = @('Hello, World!')
    
        $arr.Count | Should Be 1
      }
    }
    PS C:\Users\Brett\pester_take1> invoke-pester
    Executing all tests in '.'
    
    Executing script C:\Users\Brett\pester_take1\Trivial.Tests.ps1
    
      Describing Trivial Tests
        [+] Should know how to sort an array of size 1 54ms
    Tests completed in 54ms
    Tests Passed: 1, Failed: 0, Skipped: 0, Pending: 0, Inconclusive: 0

Congratulations, you’ve got a working system. Rather than spend more time on this trivial example, we’ll move on to a more complex problem.


Comments

" Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.