Questions tagged [pester]

Pester is a unit test framework for PowerShell. It provides a domain specific language that allows you to define test cases. It is also used to perform operational/infrastructure testing and this is encouraged by Microsoft who leverage Pester as part of the Operation Validation Framework project.

Per the Pester project page:

Pester provides a framework for running unit tests to execute and validate PowerShell commands from within PowerShell. Pester consists of a simple set of functions that expose a testing domain-specific language (DSL) for isolating, running, evaluating and reporting the results of PowerShell commands.

See

302 questions
70
votes
6 answers

How to do TDD and unit testing in powershell?

With MS ramming powershell into all new server products, I'm starting to (reluctantly) think I need to take it seriously. Part of "taking it seriously" is TDD. Have you found good methods to unit test power shell scripts? I've found samples of…
Precipitous
  • 5,253
  • 4
  • 28
  • 34
32
votes
1 answer

Determine if an array of PSCustomObject's contains an instance with a property value

I need to determine if an array of PSCustomObjects contains an item with its Title property matching a value. I need a Boolean value for use with Pester assertions: $Items - $Name | Should Be $True Assuming: $Items = @() $Items +=…
craig
  • 25,664
  • 27
  • 119
  • 205
11
votes
2 answers

Pester sample script gets "-Be is not a valid Should operator" on Windows 10, works fine on Ubuntu

New to Pester, installed Powershell and Pester on Ubuntu 16.04 per instructions on the Pester Github page https://github.com/pester/Pester. Executed their sample scripts Get-Planet.Tests.ps1 and Get-Planet.ps1 and it worked as advertised. Tried the…
Dave Nicolette
  • 171
  • 1
  • 9
10
votes
2 answers

Can Pester mock an exception?

I'm working on some Pester test cases and I'm looking at the CodeCoverage results. In most sets of test cases where we the code contains a try/catch we get 0% coverage on the catch. Here's an example: function Test-Is64Bit() { $Result =…
Dave Wiard
  • 113
  • 2
  • 10
10
votes
3 answers

How to mock a call to an exe file with Pester?

Developing a script in PowerShell, I require to call an external executable file(.exe). currently I am developing this script with a TDD approach, therefore I require to mock the called to this .exe file. I try this : Describe "Create-NewObject" { …
XtianGIS
  • 967
  • 16
  • 39
9
votes
2 answers

Testing for mandatory parameters with Pester

I'm trying to figure out how to have Pester test for parameters that are missing: Find-Waldo.Tests.ps1 $here = Split-Path -Parent $MyInvocation.MyCommand.Path $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.' Describe…
DarkLite1
  • 13,637
  • 40
  • 117
  • 214
7
votes
1 answer

Is there a way to test / assert output from Write-Host using Pester?

I'm writing tests for a fairly complex script and there's one particular function in the script that will output different series of logging messages to the user. I would like to assert whether a particular logging message is being displayed. The…
Zam
  • 1,121
  • 10
  • 27
7
votes
1 answer

How to mock a job in Pester?

We're trying to assess if Invoke-Command has been called exactly one time. Script.ps1 $job = Invoke-Command -ScriptBlock {'test'} -ComputerName localhost -AsJob $job | Wait-Job Script.Tests.ps1 BeforeAll { $testScript =…
DarkLite1
  • 13,637
  • 40
  • 117
  • 214
7
votes
1 answer

Pester mock method for Powershell 5 class

I am having an issue trying to mock a powershell 5 class method, when executing the test, I get the error " CommandNotFoundException: Could not find Command FunctionToMock". I am trying to unit test the "OutputToOverwrite" method by mocking…
BenoitM
  • 97
  • 1
  • 10
7
votes
3 answers

How can I user a parameterFilter with a switch parameter when mocking in Pester?

Using Pester, I'm mocking an advanced function which takes, amongst other parameters, a switch. How do I create a -parameterFilter for the mock which includes the switch parameter? I've tried: -parameterFilter { $Domain -eq 'MyDomain' -and $Verbose…
Simon
  • 426
  • 4
  • 12
6
votes
2 answers

Anyone's using BDD in Powershell?

I've found a lot of threads around this question here at stackoverflow. They are 2 years old now. So I would like to know if someone is using scottmuc functions to perform a BDD in powershell. It seems very powerful at first sight. What's your…
Emiliano Poggi
  • 24,390
  • 8
  • 55
  • 67
6
votes
2 answers

Access External Variable from with-in Mock Script Block (Pester)

Pretend I have a function like... function Get-Something { return Get-DogShit } ...in my Pester test script... $var = 1 Mock 'Get-Dogshit' { return $var } it 'should return true' { Get-Something | should $var } This doesn't work, but you see…
Adam
  • 3,891
  • 3
  • 19
  • 42
6
votes
2 answers

Pester doesn't catch the thrown error

When I run the following pester test I expect it to catch the expected error but it doesn't. But when I run the test with a different function with a different throw statement it works. Pester Test: Describe "Remove-GenericCredential Function…
Keith
  • 689
  • 10
  • 27
6
votes
0 answers

Unit Testing frameworks for Powershell

There was a question asked 3 years ago about Powershell Unit Testing Frameworks, How to do TDD and unit testing in powershell?, and all the frameworks were quite new at that moment: PSUnit: http://www.psunit.org/ Pester:…
Josep
  • 61
  • 2
5
votes
3 answers

How can I get the numer of failed tests from Invoke-Pester?

I have a few Pester tests running fine in the console, but I would like to run the tests automatically and send a message if any test fails. I read the option -EnableExit causes Invoke-Pester to return the numer of failed tests. But whenever I use…
1
2 3
20 21