Advent of Code: Day 01: Pester Test

Does Santa check twice with Pester?

Words: 414

Time to read: ~ 2 minutes

Describe

You can read all the books, watch all the videos, and study all the code examples that you want but the only true way to get better at a language is to use it

That is what they say anyway so in an effort to get better at PowerShell, I’ve taken advantage of the seasonal site that is Advent Of Code.

It’s a really good premise, taking you through a story board of different skill challenges. As they say themselves:

Advent of Code is a series of small programming puzzles for a variety of skill levels. They are self-contained and are just as appropriate for an expert who wants to stay sharp as they are for a beginner who is just learning to code. Each puzzle calls upon different skills and has two parts that build on a theme.

Context

Like I said above, I’m trying to get better at PowerShell but this is a great opportunity to improve my Pester skills as well.

It helps that each Code puzzle gives you a few examples of what they want. We can use these examples to create TestCases for our Pester tests.

So that’s the plan.

  1. Create a Pester test for each Advent of Code
  2. Create a PowerShell function that passes the test.

Will I get them all done? HA! No! I got to the 3rd code puzzle and went “Wait? What?”. It’s my holidays, I’m full on food and drink. My dedication and focus are at a all time low.

So let’s see how far we can go so?

It

We only need 3 things for this:

  • Examples (which have been provided)
  • A name (let’s call it Get-AOCDay01Part01)
  • a parameter (let’s call it InputObject)

But we should probably install the latest version of Pester. Let’s find it first!


Find-Module -Name Pester -MinimumVersion 4.0.0

FindPester
Latest version!

And let’s install it!


Find-Module -Name Pester -MinimumVersion 4.0.0 | Install-Module -Force

InstallNonAdmin
And we get an error…

Right, yes I don’t open VS Code as an administrator. That means we can’t install this module without specifying -Scope CurrentUser so let’s do that.


Find-Module -Name Pester -MinimumVersion 4.0.0 | Install-Module -Scope CurrentUser -Force

 

InstallCurrentUser
Tiny line

Now that we have the newest version of Pester installed, we can create our Pester test.

The thing to remember about using TestCases is that we have to add the line param(${hash table value 1}, ${hash table value 2}, ${hash table value n}) so what we put into the TestCases can be put into the Pester test.


Describe -Name 'Get-AOCDay01Part01' {
It -name 'should return a result of [<ExpectedResult>] for the input [<InputObject>]' -TestCases @(
@{ InputObject = 1122; ExpectedResult = 3 },
@{ InputObject = 1111; ExpectedResult = 4 },
@{ InputObject = 1234; ExpectedResult = 0 },
@{ InputObject = 91212129; ExpectedResult = 9 }
) {
param($InputObject, $ExpectedResult)
$Result = Get-AOCDay01Part01 -InputObject $InputObject
$Result | Should -Be $ExpectedResult
}
}

Not necessary but nice to know is, if we put the TestCases value names into the -name part of the It clause, it substitutes those values in; even with using single quotes! šŸ™‚

Anyway running the above code, we get…

FirstCreated
Command not found…

…failure!

Which is good, because now that we have a test, we can work on making it pass!

That’s for the next post though.

Author: Shane O'Neill

DBA, T-SQL and PowerShell admirer, Food, Coffee, Whiskey (not necessarily in that order)...

One thought on “Advent of Code: Day 01: Pester Test”

  1. Just checked out Advent of Code, there’s all my free time gone šŸ˜‰

    Good luck with PowerShell that’s something that I really need to get my head around, that and Pester.

Leave a Reply

Discover more from No Column Name

Subscribe now to keep reading and get access to the full archive.

Continue reading