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
2
votes
1 answer

Pester fails test even when exception is caught

I have a utility function that specifically handles an exception and ignores it, but when testing it with Pester the test fails, showing the exception that was already caught and handled. Am I missing something, or is this a bug in Pester? This code…
Joshua Honig
  • 12,925
  • 8
  • 53
  • 75
2
votes
2 answers

How can I use Pester to mock a cmdlet in which the module is not available?

I am trying to write Pester tests for my Azure automation runbooks. The runbook script uses the Get-AutomationVariable cmdlet, and I am attempting to mock it via: mock 'Get-AutomationVariable' {return "localhost:44300"} -ParameterFilter { $name -eq…
Tedford
  • 2,842
  • 2
  • 35
  • 45
2
votes
0 answers

Need help for testing custom powershell dsc resource with pester

i am new to pester testing and i a am curently trying to test a custom dsc resource i have made, the function i am testing is the following: <# .SYNOPSIS Get parameters for Snmp configuration .PARAMETER AgentSvcPhysical Used…
jiciftw
  • 65
  • 1
  • 6
2
votes
1 answer

In need of some assistance with PowerShell functions and modules

I have this module that I am working with: Function Get-BuildVersion { param( [string] $MajorMinorVersion, [int] $BuildCounter, [int] $Offset=30, [string] $Suffix = "-hotfix" ) return…
user9295625
2
votes
1 answer

Pester Tests with PSUseDeclaredVarsMoreThanAssignment

If a variable is declared in the Describe block of a PSPester script, and it is used in a It block, the PSScriptAnalyzer doesn't recognize the usage of that variable. How do I have to declare the variable to get rid of the warning, or how do i…
abbgrade
  • 548
  • 5
  • 19
2
votes
1 answer

Simple Powershell Pester Test not working for $false

I wrote a very simple function called "Check-RegValueExists" which works as follows when executing directly from the command prompt Check-RegValueExists "HKLM:\Software\Microsoft\Windows\CurrentVersion" "DevicePath" Check-RegValueExists…
2
votes
1 answer

How do I correctly mock my Function to return a custom property with Pester?

I'm a bit new to PowerShell and specifically Pester testing. I can't seem to recreate a scenario for the function I am making Pester test. Here is the code: $State = Get-Status if(State) { switch ($State.Progress) { 0 { …
2
votes
1 answer

How to invoke PowerShell Pester tests in an Azure Function Timer Trigger?

Doing an Invoke-Pester x.tests.ps1 that contains a Describe block and an It block that checks a bool. The function fails with the following. /ServiceStatus/modules/Pester_3.4.6/Pester.psm1 2017-06-27T20:19:39.503 Function completed (Failure,…
Doug Finke
  • 6,675
  • 1
  • 31
  • 47
2
votes
3 answers

-Verbose not working with my Pester Test in PowerShell

I wrote a pester test to check that certain folders and files exist. The pester test works great but I wanted to include suggestions for fixes if the test is called with the -Verbose option. But I can't seem to get the -Verbose parameter to the…
Keith
  • 689
  • 10
  • 27
2
votes
2 answers

Where to put Pester Variables?

I've recently started using Pester to write tests in PowerShell and I've got no problem running basic tests, however I'm looking to build some more complex tests and I'm struggling on what to do with variables that I need for the tests. I'm writing…
Sam Cogan
  • 4,124
  • 7
  • 44
  • 76
2
votes
2 answers

How can I Mock Out-File when testing my PowerShell script with Pester?

I am trying to test my PowerShell code using Pester. I want to mock out-file for the following line: $interactiveContent | Out-File -Append -FilePath (Join-Path $destDir $interactiveOutputFile) But I want to give my own file path while testing. I…
2
votes
1 answer

Mock a function within a .ps1 script

I have a PowerShell .ps1 file which contains functions at the top of the script followed by different commands calling these functions. I am using Pester to unit test my script file. How do I mock a function that is within my PowerShell .ps1…
2
votes
3 answers

Mock a command failing

Set-Location "C:\thisDirDoesNotExist" If(-not $?) { Write-Error -Message "error" Exit 1 } How do I mock a command failing? In the above code, the Set-Location command will error with a non-terminating error. The $? variable will be set to…
2
votes
2 answers

Assert-MockCalled vs Assert-VerifiableMocks in Pester

What makes Assert-VerifiableMocks different from Assert-MockCalled in Pester? I've been reading https://github.com/pester/Pester/wiki/Assert-VerifiableMocks https://github.com/pester/Pester/wiki/Assert-MockCalled but still wonder: are the…
QIU Quan
  • 23
  • 3
2
votes
2 answers

Why doesn't Pester catch errors using a trap

I'm wondering why I get the following behaviour when running this script. I have the script loaded in PowerShell ISE (v4 host) and have the Pester module loaded. I run the script by pressing F5. function Test-Pester { throw("An error") } Describe…
Charlie Joynt
  • 4,411
  • 1
  • 24
  • 46