As Infrastructure as Code is growing more in popularity, system administrators and devs started writing more and more sophisticated systems code and scripts.
Testing code is something that devs have been doing this for a long time while system administrators just started adopting the idea. With the growing popularity of PowerShell, more and more system administrators and devs began to write PowerShell code for provisioning and configuring infrastructure either on-premises or in the cloud, but the biggest problem was that there was no useful framework to test that code when a breaking change occurred.
This is the concept of “I ran it, and it worked,” did it now?
Enter Pester.
Pester is a unit testing framework for PowerShell. It provides a few simple-to-use keywords that let you create tests for your scripts. Pester implements a test drive to isolate your test files, and it can replace almost any command in PowerShell with your implementation. This makes it an excellent framework for both Black-box and White-box testing.
In this presentation, you will learn what Pester is, how you can use pester as your daily driver when you’re writing scripts and how you can use Pester to make your life better when change happens.
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Testing your PowerShell code with Pester - Florin Loghiade
1. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
Testing your PowerShell code with
Pester
Florin Loghiade
Microsoft Azure MVP
AVAELGO
florinloghiade.ro
5. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• Pester is a Unit Testing Framework for PowerShell
• Has a few simple key-words that let you test your
script
• You can do integration, functional or operational
testing with it
• It’s so cool that it was integrated in Windows 10 /
Windows Server 2016
What is Pester?
6. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• Test
–Tests are a special kind of scripts that tell Pester what
commands to run
• Mocks
–When you’re running code, you usually have to run other
code for it to work properly. Mocking helps you do that.
• Assertions
–Are the core component of Pester, they provide a method
to compare what the output should be like
Pester Concepts
7. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
Describe -Name 'Set-Computer' -Fixture {
It -name 'when a server name is passed, it returns the right string' -test {
Set-Computer -ComputerName 'MYSRV' | Should be 'Did that thing to the
server'
}
It -name 'when anything other than server name is passed, it returns the right
string' -test {
Set-Computer -ComputerName 'MYCLIENT' | Should be 'Did that thing to
the client'
}
}
Pester Test Example
8. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
Describe -Name 'New-Employee' -Fixture {
Mock -CommandName 'Import-Csv' -MockWith {
[pscustomobject]@{
UserName = 'floghiade'
}
}
Mock -CommandName 'Get-AdUser' -MockWith {
@{
givenName = 'Florin'
surname = 'Loghiade'
}
}
It -name 'returns all expected AD users' -test {
$adUsers = Get-Employee
$adUsers.givenName | Should be 'Florin'
$adUsers.surname | Should be 'Loghiade'
}
}
Pester Mock Example
9. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• Should (Not) Be
• Should (Not) BeExactly
• Should (Not) BeNullOrEmpty
• Should (Not) Match
• Should (Not) MatchExactly
• Should (Not) Exist
• Should (Not) Contain
• Should (Not) ContainExactly
• Should (Not) Throw
Pester Assertions
13. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• Nobody likes writing tests.
– But once you have them, you can’t live without them
• Thinking what to test is the brain twister
– How should I test that function again?
• A good practice is to first write the tests then the script
Do tell.
–Usually never happens but we try.
Sometimes..
14. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• We need to release code as frequently as possible
• Automated tests permit you to release reliable code
as fast as possible
And DevOps
Image Source: http://www.platformpoints.com/corp/cms/Services/Continuous-Integration-and-Deployment/
15. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• We want to avoid:
–What did this do, again?
–Is my code working now?
–This has an input and an
output. AWESOME! What did
it output?
The reality is…
17. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• When writing tests, be as explicit as possible
• When dot sourcing functions or importing modules,
don’t assume they just exist.
Design Practices
18. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• Pester provides an easy way to start a .tests file
–New-Fixture -Name ITCamp2017
How to use it
19. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• Any CI / CD tool can use Pester
• Pester can export test results in NUnit
• VSTS has an extension for it and works great!
Integration in CI/CD pipelines
22. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
Integration / acceptance tests
• Is my service installed?
• Did my script actually do the modifications?
• Is there a web application installed and operational?
• Is port 80 open?
• Is the web server responding?
Use case #1
23. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
Operational Validation Tests
• Did the configuration change?
• Is SQL Server running and operational?
• Are my web servers serving requests?
• Who changed settings on my AD?
Use case #2
24. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals
Testing stupidity
• Am I getting the right input?
• Is the input in a readable state?
• Case-Sensitive content
• Does that archive contain the right files? Or is
it an archive?
If you think that something is stupid and
shouldn't be validated, think again. You will be
amazed.
Use case #3