Upload
itcamp
View
376
Download
2
Embed Size (px)
Citation preview
@ITCAMPRO #ITCAMP17Community Conference for IT Professionals
Testing your PowerShell code with
Pester
Florin Loghiade
Microsoft Azure MVP
AVAELGO
florinloghiade.ro
@ITCAMPRO #ITCAMP17Community Conference for IT Professionals
Many thanks to our sponsors & partners!
GOLD
SILVER
PARTNERS
PLATINUM
POWERED BY
@ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• What is Pester?
• Why should I care?
• Using Pester
• Use Cases
Agenda
@ITCAMPRO #ITCAMP17Community Conference for IT Professionals
WHAT IS PESTER?
@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?
@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
@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
@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
@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
@ITCAMPRO #ITCAMP17Community Conference for IT Professionals
WHY SHOULD I CARE?
@ITCAMPRO #ITCAMP17Community Conference for IT Professionals
That’s why.
@ITCAMPRO #ITCAMP17Community Conference for IT Professionals
Writing tests kills and saves neurons at the same time.
@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..
@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/
@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…
@ITCAMPRO #ITCAMP17Community Conference for IT Professionals
USING PESTER
@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
@ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• Pester provides an easy way to start a .tests file
–New-Fixture -Name ITCamp2017
How to use it
@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
@ITCAMPRO #ITCAMP17Community Conference for IT Professionals
Example
@ITCAMPRO #ITCAMP17Community Conference for IT Professionals
USE CASES FOR POWERSHELL TESTS
@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
@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
@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
@ITCAMPRO #ITCAMP17Community Conference for IT Professionals
Stupidity Example
@ITCAMPRO #ITCAMP17Community Conference for IT Professionals
Why should I care?
So..
@ITCAMPRO #ITCAMP17Community Conference for IT Professionals
• Pester GitHub Repository
• Rspec
• The Pester Book
• VSTS Pester Build Task
• Testing PowerShell with Pester MVA
Resources
@ITCAMPRO #ITCAMP17Community Conference for IT Professionals
Q & A