28
@ITCAMPRO #ITCAMP17 Community Conference for IT Professionals Testing your PowerShell code with Pester Florin Loghiade Microsoft Azure MVP AVAELGO florinloghiade.ro

Testing your PowerShell code with Pester - Florin Loghiade

  • Upload
    itcamp

  • View
    376

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Testing your PowerShell code with Pester - Florin Loghiade

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Testing your PowerShell code with

Pester

Florin Loghiade

Microsoft Azure MVP

AVAELGO

florinloghiade.ro

Page 2: Testing your PowerShell code with Pester - Florin Loghiade

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Many thanks to our sponsors & partners!

GOLD

SILVER

PARTNERS

PLATINUM

POWERED BY

Page 3: Testing your PowerShell code with Pester - Florin Loghiade

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals

• What is Pester?

• Why should I care?

• Using Pester

• Use Cases

Agenda

Page 4: Testing your PowerShell code with Pester - Florin Loghiade

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals

WHAT IS PESTER?

Page 5: Testing your PowerShell code with Pester - Florin Loghiade

@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?

Page 6: Testing your PowerShell code with Pester - Florin Loghiade

@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

Page 7: Testing your PowerShell code with Pester - Florin Loghiade

@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

Page 8: Testing your PowerShell code with Pester - Florin Loghiade

@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

Page 9: Testing your PowerShell code with Pester - Florin Loghiade

@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

Page 10: Testing your PowerShell code with Pester - Florin Loghiade

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals

WHY SHOULD I CARE?

Page 11: Testing your PowerShell code with Pester - Florin Loghiade

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals

That’s why.

Page 12: Testing your PowerShell code with Pester - Florin Loghiade

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Writing tests kills and saves neurons at the same time.

Page 13: Testing your PowerShell code with Pester - Florin Loghiade

@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..

Page 14: Testing your PowerShell code with Pester - Florin Loghiade

@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/

Page 15: Testing your PowerShell code with Pester - Florin Loghiade

@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…

Page 16: Testing your PowerShell code with Pester - Florin Loghiade

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals

USING PESTER

Page 17: Testing your PowerShell code with Pester - Florin Loghiade

@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

Page 18: Testing your PowerShell code with Pester - Florin Loghiade

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals

• Pester provides an easy way to start a .tests file

–New-Fixture -Name ITCamp2017

How to use it

Page 19: Testing your PowerShell code with Pester - Florin Loghiade

@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

Page 20: Testing your PowerShell code with Pester - Florin Loghiade

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Example

Page 21: Testing your PowerShell code with Pester - Florin Loghiade

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals

USE CASES FOR POWERSHELL TESTS

Page 22: Testing your PowerShell code with Pester - Florin Loghiade

@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

Page 23: Testing your PowerShell code with Pester - Florin Loghiade

@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

Page 24: Testing your PowerShell code with Pester - Florin Loghiade

@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

Page 25: Testing your PowerShell code with Pester - Florin Loghiade

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Stupidity Example

Page 26: Testing your PowerShell code with Pester - Florin Loghiade

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Why should I care?

So..

Page 27: Testing your PowerShell code with Pester - Florin Loghiade

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals

• Pester GitHub Repository

• Rspec

• The Pester Book

• VSTS Pester Build Task

• Testing PowerShell with Pester MVA

Resources

Page 28: Testing your PowerShell code with Pester - Florin Loghiade

@ITCAMPRO #ITCAMP17Community Conference for IT Professionals

Q & A