SlideShare a Scribd company logo
@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

More Related Content

What's hot (20)

PDF
Modern cybersecurity threats, and shiny new tools to help deal with them - T...
ITCamp
 
PDF
The Microsoft Cloud and Server Strategy - Ben Armstrong
ITCamp
 
PDF
#NoAgile - Dan Suciu
ITCamp
 
PDF
Docker adventures in Continuous Delivery - Alex Vranceanu
ITCamp
 
PDF
Blockchain for mere mortals - understand the fundamentals and start building ...
ITCamp
 
PDF
Migrating to Continuous Delivery with TFS 2017 - Liviu Mandras-Iura
ITCamp
 
PDF
From Developer to Data Scientist - Gaines Kergosien
ITCamp
 
PDF
Scaling face recognition with big data - Bogdan Bocse
ITCamp
 
PDF
The fight for surviving in the IoT world - Radu Vunvulea
ITCamp
 
PPTX
ITCamp 2017 - Laurent Ellerbach - Bot. You said bot? Let's build a bot then...
ITCamp
 
PDF
Azure tales: a real world CQRS and ES Deep Dive - Andrea Saltarello
ITCamp
 
PDF
Everyone Loves Docker Containers Before They Understand Docker Containers - A...
ITCamp
 
PDF
The Fine Art of Time Travelling - Implementing Event Sourcing - Andrea Saltar...
ITCamp
 
PDF
ITCamp 2017 - Raffaele Rialdi - Adopting .NET Core in Mainstream Projects
ITCamp
 
PDF
Execution Plans in practice - how to make SQL Server queries faster - Damian ...
ITCamp
 
PDF
Creating Web and Mobile Apps with Angular 2 - George Saadeh
ITCamp
 
PDF
A new world of possibilities for contextual awareness with beacons - Dan Arde...
ITCamp
 
PDF
Kubernetes - Cloud Native Application Orchestration - Catalin Jora
ITCamp
 
PDF
The best of Hyper-V 2016 - Thomas Maurer
ITCamp
 
PDF
Azure Microservices in Practice - Radu Vunvulea
ITCamp
 
Modern cybersecurity threats, and shiny new tools to help deal with them - T...
ITCamp
 
The Microsoft Cloud and Server Strategy - Ben Armstrong
ITCamp
 
#NoAgile - Dan Suciu
ITCamp
 
Docker adventures in Continuous Delivery - Alex Vranceanu
ITCamp
 
Blockchain for mere mortals - understand the fundamentals and start building ...
ITCamp
 
Migrating to Continuous Delivery with TFS 2017 - Liviu Mandras-Iura
ITCamp
 
From Developer to Data Scientist - Gaines Kergosien
ITCamp
 
Scaling face recognition with big data - Bogdan Bocse
ITCamp
 
The fight for surviving in the IoT world - Radu Vunvulea
ITCamp
 
ITCamp 2017 - Laurent Ellerbach - Bot. You said bot? Let's build a bot then...
ITCamp
 
Azure tales: a real world CQRS and ES Deep Dive - Andrea Saltarello
ITCamp
 
Everyone Loves Docker Containers Before They Understand Docker Containers - A...
ITCamp
 
The Fine Art of Time Travelling - Implementing Event Sourcing - Andrea Saltar...
ITCamp
 
ITCamp 2017 - Raffaele Rialdi - Adopting .NET Core in Mainstream Projects
ITCamp
 
Execution Plans in practice - how to make SQL Server queries faster - Damian ...
ITCamp
 
Creating Web and Mobile Apps with Angular 2 - George Saadeh
ITCamp
 
A new world of possibilities for contextual awareness with beacons - Dan Arde...
ITCamp
 
Kubernetes - Cloud Native Application Orchestration - Catalin Jora
ITCamp
 
The best of Hyper-V 2016 - Thomas Maurer
ITCamp
 
Azure Microservices in Practice - Radu Vunvulea
ITCamp
 

Viewers also liked (10)

PDF
Big Data Solutions in Azure - David Giard
ITCamp
 
PDF
Great all this new stuff, but how do I convince my management - Erwin Derksen
ITCamp
 
PPTX
One Azure Monitor to Rule Them All? (IT Camp 2017, Cluj, RO)
Marius Zaharia
 
PDF
Forget Process, Focus on People - Peter Leeson
ITCamp
 
PDF
Columnstore indexes - best practices for the ETL process - Damian Widera
ITCamp
 
PDF
The Secret of Engaging Presentations - Boris Hristov
ITCamp
 
PDF
Building Powerful Applications with AngularJS 2 and TypeScript - David Giard
ITCamp
 
PDF
Storage Spaces Direct - the new Microsoft SDS star - Carsten Rachfahl
ITCamp
 
PDF
Xamarin Under The Hood - Dan Ardelean
ITCamp
 
PDF
7 Habits of Highly Paid Developers - Gaines Kergosien
ITCamp
 
Big Data Solutions in Azure - David Giard
ITCamp
 
Great all this new stuff, but how do I convince my management - Erwin Derksen
ITCamp
 
One Azure Monitor to Rule Them All? (IT Camp 2017, Cluj, RO)
Marius Zaharia
 
Forget Process, Focus on People - Peter Leeson
ITCamp
 
Columnstore indexes - best practices for the ETL process - Damian Widera
ITCamp
 
The Secret of Engaging Presentations - Boris Hristov
ITCamp
 
Building Powerful Applications with AngularJS 2 and TypeScript - David Giard
ITCamp
 
Storage Spaces Direct - the new Microsoft SDS star - Carsten Rachfahl
ITCamp
 
Xamarin Under The Hood - Dan Ardelean
ITCamp
 
7 Habits of Highly Paid Developers - Gaines Kergosien
ITCamp
 
Ad

Similar to Testing your PowerShell code with Pester - Florin Loghiade (20)

PPTX
ITCamp 2019 - Andy Cross - Machine Learning with ML.NET and Azure Data Lake
ITCamp
 
PPTX
ITCamp 2019 - Emil Craciun - RoboRestaurant of the future powered by serverle...
ITCamp
 
PDF
Azure SQL Database From A Developer's Perspective - Alex Mang
ITCamp
 
PPTX
Azure Microservices in Practice, Radu Vunvulea, ITCamp 2016
Radu Vunvulea
 
PDF
ITCamp 2018 - Ciprian Sorlea - Enterprise Architectures with TypeScript And F...
ITCamp
 
PPTX
Scaling Face Recognition with Big Data
Bogdan Bocse
 
PPTX
It camp 2015 how to scale above clouds limits, radu vunvulea
Radu Vunvulea
 
PDF
ITCamp 2018 - Damian Widera U-SQL in great depth
ITCamp
 
PDF
ITCamp 2018 - Damian Widera - SQL Server 2016. Meet the Row Level Security. P...
ITCamp
 
PPTX
The fight for surviving in the IoT world
Radu Vunvulea
 
PDF
Enacting Scrum - What it takes to maximize the chances for a successful adopt...
ITCamp
 
PDF
Single Source of Truth for Network Automation
Andy Davidson
 
PDF
ITCamp 2018 - Gunnar Peipman - Deep dive to C# 7
ITCamp
 
PDF
Provisioning Windows instances at scale on Azure, AWS and OpenStack - Adrian ...
ITCamp
 
PDF
ITCamp 2018 - Andrea Martorana Tusa - Failure prediction for manufacturing in...
ITCamp
 
PPTX
Day zero of a cloud project Radu Vunvulea ITCamp 2018
Radu Vunvulea
 
PDF
One Azure Monitor to Rule Them All? - Marius Zaharia
ITCamp
 
PPTX
ITCamp 2019 - Mihai Tataran - Governing your Cloud Resources
ITCamp
 
PDF
Elements of DDD with ASP.NET MVC & Entity Framework Code First v2
Enea Gabriel
 
PDF
ITCamp 2018 - Walter Belgers - Lockpicking and IT security
ITCamp
 
ITCamp 2019 - Andy Cross - Machine Learning with ML.NET and Azure Data Lake
ITCamp
 
ITCamp 2019 - Emil Craciun - RoboRestaurant of the future powered by serverle...
ITCamp
 
Azure SQL Database From A Developer's Perspective - Alex Mang
ITCamp
 
Azure Microservices in Practice, Radu Vunvulea, ITCamp 2016
Radu Vunvulea
 
ITCamp 2018 - Ciprian Sorlea - Enterprise Architectures with TypeScript And F...
ITCamp
 
Scaling Face Recognition with Big Data
Bogdan Bocse
 
It camp 2015 how to scale above clouds limits, radu vunvulea
Radu Vunvulea
 
ITCamp 2018 - Damian Widera U-SQL in great depth
ITCamp
 
ITCamp 2018 - Damian Widera - SQL Server 2016. Meet the Row Level Security. P...
ITCamp
 
The fight for surviving in the IoT world
Radu Vunvulea
 
Enacting Scrum - What it takes to maximize the chances for a successful adopt...
ITCamp
 
Single Source of Truth for Network Automation
Andy Davidson
 
ITCamp 2018 - Gunnar Peipman - Deep dive to C# 7
ITCamp
 
Provisioning Windows instances at scale on Azure, AWS and OpenStack - Adrian ...
ITCamp
 
ITCamp 2018 - Andrea Martorana Tusa - Failure prediction for manufacturing in...
ITCamp
 
Day zero of a cloud project Radu Vunvulea ITCamp 2018
Radu Vunvulea
 
One Azure Monitor to Rule Them All? - Marius Zaharia
ITCamp
 
ITCamp 2019 - Mihai Tataran - Governing your Cloud Resources
ITCamp
 
Elements of DDD with ASP.NET MVC & Entity Framework Code First v2
Enea Gabriel
 
ITCamp 2018 - Walter Belgers - Lockpicking and IT security
ITCamp
 
Ad

More from ITCamp (20)

PDF
ITCamp 2019 - Stacey M. Jenkins - Protecting your company's data - By psychol...
ITCamp
 
PDF
ITCamp 2019 - Silviu Niculita - Supercharge your AI efforts with the use of A...
ITCamp
 
PDF
ITCamp 2019 - Peter Leeson - Managing Skills
ITCamp
 
PDF
ITCamp 2019 - Ivana Milicic - Color - The Shadow Ruler of UX
ITCamp
 
PDF
ITCamp 2019 - Florin Coros - Implementing Clean Architecture
ITCamp
 
PPTX
ITCamp 2019 - Florin Loghiade - Azure Kubernetes in Production - Field notes...
ITCamp
 
PPTX
ITCamp 2019 - Florin Flestea - How 3rd Level support experience influenced m...
ITCamp
 
PPTX
ITCamp 2019 - Eldert Grootenboer - Cloud Architecture Recipes for The Enterprise
ITCamp
 
PPTX
ITCamp 2019 - Cristiana Fernbach - Blockchain Legal Trends
ITCamp
 
PPTX
ITCamp 2019 - Andy Cross - Business Outcomes from AI
ITCamp
 
PDF
ITCamp 2019 - Andrea Saltarello - Modernise your app. The Cloud Story
ITCamp
 
PDF
ITCamp 2019 - Andrea Saltarello - Implementing bots and Alexa skills using Az...
ITCamp
 
PPTX
ITCamp 2019 - Alex Mang - I'm Confused Should I Orchestrate my Containers on ...
ITCamp
 
PPTX
ITCamp 2019 - Alex Mang - How Far Can Serverless Actually Go Now
ITCamp
 
PDF
ITCamp 2019 - Peter Leeson - Vitruvian Quality
ITCamp
 
PDF
ITCamp 2018 - Ciprian Sorlea - Million Dollars Hello World Application
ITCamp
 
PDF
ITCamp 2018 - Mete Atamel Ian Talarico - Google Home meets .NET containers on...
ITCamp
 
PDF
ITCamp 2018 - Magnus Mårtensson - Azure Global Application Perspectives
ITCamp
 
PDF
ITCamp 2018 - Magnus Mårtensson - Azure Resource Manager For The Win
ITCamp
 
PDF
ITCamp 2018 - Ionut Balan - A beginner’s guide to Windows Mixed Reality
ITCamp
 
ITCamp 2019 - Stacey M. Jenkins - Protecting your company's data - By psychol...
ITCamp
 
ITCamp 2019 - Silviu Niculita - Supercharge your AI efforts with the use of A...
ITCamp
 
ITCamp 2019 - Peter Leeson - Managing Skills
ITCamp
 
ITCamp 2019 - Ivana Milicic - Color - The Shadow Ruler of UX
ITCamp
 
ITCamp 2019 - Florin Coros - Implementing Clean Architecture
ITCamp
 
ITCamp 2019 - Florin Loghiade - Azure Kubernetes in Production - Field notes...
ITCamp
 
ITCamp 2019 - Florin Flestea - How 3rd Level support experience influenced m...
ITCamp
 
ITCamp 2019 - Eldert Grootenboer - Cloud Architecture Recipes for The Enterprise
ITCamp
 
ITCamp 2019 - Cristiana Fernbach - Blockchain Legal Trends
ITCamp
 
ITCamp 2019 - Andy Cross - Business Outcomes from AI
ITCamp
 
ITCamp 2019 - Andrea Saltarello - Modernise your app. The Cloud Story
ITCamp
 
ITCamp 2019 - Andrea Saltarello - Implementing bots and Alexa skills using Az...
ITCamp
 
ITCamp 2019 - Alex Mang - I'm Confused Should I Orchestrate my Containers on ...
ITCamp
 
ITCamp 2019 - Alex Mang - How Far Can Serverless Actually Go Now
ITCamp
 
ITCamp 2019 - Peter Leeson - Vitruvian Quality
ITCamp
 
ITCamp 2018 - Ciprian Sorlea - Million Dollars Hello World Application
ITCamp
 
ITCamp 2018 - Mete Atamel Ian Talarico - Google Home meets .NET containers on...
ITCamp
 
ITCamp 2018 - Magnus Mårtensson - Azure Global Application Perspectives
ITCamp
 
ITCamp 2018 - Magnus Mårtensson - Azure Resource Manager For The Win
ITCamp
 
ITCamp 2018 - Ionut Balan - A beginner’s guide to Windows Mixed Reality
ITCamp
 

Recently uploaded (20)

PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Python basic programing language for automation
DanialHabibi2
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Python basic programing language for automation
DanialHabibi2
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 

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
  • 2. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals Many thanks to our sponsors & partners! GOLD SILVER PARTNERS PLATINUM POWERED BY
  • 3. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals • What is Pester? • Why should I care? • Using Pester • Use Cases Agenda
  • 4. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals WHAT IS PESTER?
  • 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
  • 10. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals WHY SHOULD I CARE?
  • 11. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals That’s why.
  • 12. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals Writing tests kills and saves neurons at the same time.
  • 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…
  • 16. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals USING PESTER
  • 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
  • 20. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals Example
  • 21. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals USE CASES FOR POWERSHELL TESTS
  • 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
  • 25. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals Stupidity Example
  • 26. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals Why should I care? So..
  • 27. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals • Pester GitHub Repository • Rspec • The Pester Book • VSTS Pester Build Task • Testing PowerShell with Pester MVA Resources
  • 28. @ITCAMPRO #ITCAMP17Community Conference for IT Professionals Q & A