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
3
votes
3 answers

Having difficultly mocking Invoke-WebRequest's BasicHtmlWebResponseObject

I'm attempting to test this section of a PowerShell function: # post $Response = Invoke-WebRequest -Method POST -Uri $Uri -Body $Body -ContentType 'application/xml' # parse Response.Content; return as…
craig
  • 25,664
  • 27
  • 119
  • 205
3
votes
1 answer

Remove a Pester mocked function

How is it possible to unmock a previously mocked function? Sometimes I find myself in a situation that I want to test a function that I previously mocked. A simplified example: Describe 'Pester mocking' { $testFile = Join-Path $env:TEMP…
DarkLite1
  • 13,637
  • 40
  • 117
  • 214
3
votes
1 answer

PowerShell: Pester Unit Test with Before/After Blocks

I'm learning to do Unit Tests with Pester and I am having a strange problem with the Before/After blocks. My Unit Test code is below: Describe "Before/After Blocks" { Context "BeforeEach/AfterAll" { BeforeAll { …
xero399
  • 171
  • 6
3
votes
1 answer

How to 'get arguments for calls made on' a Mock in Pester (or otherwise generate a helpful message containing actual and expected values)?

While there are many examples of using Pester to Assert a Mock, I am unable to find good (or any) examples on how to use Pester to get parameters made upon a Mock; this is useful to get a meaningful error message instead of a generic message of…
user2864740
  • 60,010
  • 15
  • 145
  • 220
3
votes
1 answer

Pester/PowerShell output should match/contain either $a or $b

I would like to Pester to check if a public IP is attached to either $edge00 or $edge01 NIC # id of the Get-AzPublicIpAddress output will =…
Beefcake
  • 733
  • 2
  • 12
  • 37
3
votes
1 answer

Pester test non-exported PowerShell cmdlets/function

I have a PowerShell module which exports one cmdlet. The module contains several functions which are not visible to the end user. However, I want to test these functions via Pester (since the test setup will be simple). Is it possible to call a…
Moerwald
  • 10,448
  • 9
  • 43
  • 83
3
votes
1 answer

How to properly test the ErrorVariable value of a mocked CmdLet?

We're trying to check the value in the ErrorVariable of Invoke-Command within a Pester test. But for one reason or another the -ErrorVariable is not instantiated. Describe 'test ErrorVariable' { Mock Invoke-Command { #[CmdletBinding()] …
DarkLite1
  • 13,637
  • 40
  • 117
  • 214
3
votes
1 answer

How do I prevent Pester Mocked Read-Host from prompting for input during Code Coverage

I have a Pester test where I mock up a Read-Host call for my function, which follows the format in this question here: How do I mock Read-Host in a Pester test? Describe "Test-Foo" { Context "When something" { Mock Read-Host {return…
3
votes
1 answer

Is there any way to mock $PSVersionTable variable of powershell in pester

Is there any way to mock the $PSVersionTable variable of PowerShell with Pester? Here is my code: Testcase.ps1: trap [Exception] { write-error $("TRAPPED: " + $_) exit 1 } function Check_Powershell_Version { $PSVersion =…
Sakshi Rawal
  • 97
  • 2
  • 12
3
votes
1 answer

Pester ParameterFilter not matched by Assert-MockCalled

We're trying to check if the CmdLet Start-ScheduledTask has been called exactly one time for a specific scheduled task. But for one reason or another the ParameterFilter is not matched. Code $here = Split-Path -Parent…
DarkLite1
  • 13,637
  • 40
  • 117
  • 214
3
votes
1 answer

Can't Pass Pester Test because of local variable variable error

I have a hard time creating a pester for a specific Powershell function using invoke-command and having a $using variable on a script block. It would always return an error whenever i run my test. Sample function and test below: Function: Function…
3
votes
1 answer

Can not reach 100% Pester Code Coverage due to Script block

Hi All, I have been struggling a long time because of my pester not reaching 100% code coverage due to a scriptblock. I have been researching and reading articles to no success and I have decided to ask some help from you guys. :) Part of the code I…
3
votes
1 answer

Pester Mock of Get-Date not called when using -ParameterFilter

I have created a new Pester fixture and am trying to mock a call to the Get-Date CmdLet, but it is not working. It does work if I don't use -ParameterFilter. dummy.ps1 function dummy { return Get-Date -f "dd" } dummy.Tests.ps1 $here =…
Zoodor
  • 424
  • 2
  • 4
  • 15
3
votes
1 answer

Mock [System.IO.Path]::IsPathRooted() using Pester?

How do I Mock [System.IO.Path]::IsPathRooted() using Pester? I tried the following but no luck. Describe "Configuration" { Mock [System.IO.Path]::IsPathRooted { return false } It "should mock rooted Path" { …
Adam Labi
  • 436
  • 7
  • 16
3
votes
2 answers

How should a Mock function accept pipeline input in Pester?

I am very new to Pester and I am trying to build tests for a very small and simple function in PowerShell: function Toggle-Notepad { if (-not ( Get-Process notepad -ErrorAction SilentlyContinue ) ) { Start-Process -FilePath Notepad …
Subhayan Bhattacharya
  • 5,407
  • 7
  • 42
  • 60