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
5
votes
2 answers

Pester: How do I keep my script from running?

I am testing a PowerShell script. I'd like to test individual functions without executing the entire script. I am not sure if this is the intended use case or supported, and I am not finding a good answer searching online sut.ps1: Param( …
Martin K
  • 183
  • 9
5
votes
1 answer

Pester and testing for enum

How do I test for enum with the Powershell unit test framework Pester? What I get back from the testee seems to be a string and not my proper enum. Testresult The test results in an error. What I got back was Apple and not my enum…
LosManos
  • 7,195
  • 6
  • 56
  • 107
5
votes
2 answers

How to use PowerShell multithreading and still unit test with Pester Mocks

I'm trying to do a simple parallel operation in Powershell. I am using PoshRSJobs for multithreading, though I have also tried Invoke-Parallel with the same issue. I need to call a couple of my own functions in the scriptbody of the job, but this…
jrizzo
  • 1,652
  • 4
  • 17
  • 29
5
votes
1 answer

Test a collection for equality or equivalence in Pester

In nUnit, we can do something like this: Expect(actualColleciton, EquivalentTo(expectedCollection)); and Expect(actualCollection, EqualTo(expectedCollection)); Is there an equivalent in Pester? I know I can do $actualCollection | Should Be…
CubanX
  • 5,176
  • 2
  • 29
  • 44
5
votes
2 answers

Is it possible to include functions only without executing the script?

Say I have MyScript.ps1: [cmdletbinding()] param ( [Parameter(Mandatory=$true)] [string] $MyInput ) function Show-Input { param ([string] $Incoming) Write-Output $Incoming } function Save-TheWorld { #ToDo } Write-Host…
Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
4
votes
1 answer

how do I concatenate and join an array of strings with a delimiter in powershell?

PS C:\Users\User\ps-modules> more .\MyStrings.Tests.ps1 function slist { "1", "2", "3" } Describe 'StringTests' { It 'literal -join' { "1", "2", "3" -join "," | Should -Be "1,2,3" } It 'literal -join' { @("1", "2", "3") -join "," |…
4
votes
1 answer

How to create a wrapper for an advanced function cmdlet that uses dynamic parameters

I'm trying to create a wrapper (proxy) for Pester's Should cmdlet. Possible use cases include transparent logging of test input even in case of success and improve the way Pester logs objects of certain types, e. g. hashtable. As Should is an…
zett42
  • 25,437
  • 3
  • 35
  • 72
4
votes
1 answer

how to run Pester quickly with -OutPut Detailed in VSCode?

Press the shortcut key 'Ctrl+F5' in VSCode to run the Pester test file with the suffix '.tests.ps1'. In 'about_Parsing.Tests.ps1' After you press "Ctrl+F5", the console will directly…
Andy
  • 1,077
  • 1
  • 8
  • 20
4
votes
2 answers

PowerShell with Pester Tests - No test files were found and no scriptblocks were provided

Trying to learn Pester (v5.0.4 and PS v7.0.3). I have these files Get-Planet2.ps1: function Get-Planet ([string]$Name = '*') { $planets = @( @{ Name = 'Mercury' } @{ Name = 'Venus' } @{ Name = 'Earth' } @{…
Ultra GC
  • 311
  • 4
  • 15
4
votes
1 answer

Pester Testdrive does not exists

I'm trying to create a test for a custom file-management-powershell function using Pesters TestDrive. However, I'm not getting it running in any way, always receiving the error that TestDrive does not exists. Even with the example from the…
bbd
  • 43
  • 3
4
votes
2 answers

Pester: Cannot access parent scoped variable

I have the following simple test in Pester: # Name.Tests.ps1 $name = "foo" Describe "Check name" { It "should have the correct value" { $name | Should -Be "foo" } } So when I navigate to the folder containing the test script and run…
Khan
  • 185
  • 2
  • 10
4
votes
2 answers

How do I Powershell Pester Test for ThrowTerminatingError

How do I Powershell Pester Test for ThrowTerminatingError ? catch { $PSCmdlet.ThrowTerminatingError( $PSItem ) } Output: Missed command: File Class Function Line Command ---- ----- -------- …
Eric Dunn
  • 77
  • 4
4
votes
2 answers

How to pass parameters to all the pester test scripts

The Invoke-Pester command makes it possible to invoke a single test script with explicit parameters using the -Script parameter. But what if I want to pass the same parameters to all of the test scripts? I do not want to invoke pester in a loop,…
mark
  • 59,016
  • 79
  • 296
  • 580
4
votes
3 answers

Invoke-Pester to only run a single Assert/It block

I am writing unit tests for my Powershell Modules, with a file for each module, and Describe blocks for each function. Context blocks organize the tests along the behavior I am trying to test for with some arrange code, and my It blocks contain…
Brandon McClure
  • 1,329
  • 1
  • 11
  • 32
4
votes
1 answer

How can Pester mock the "test" function in the pattern "test existence (not found) - create - test again to confirm creation"?

Here's some pseudo-code showing the function to test: function Set-Something { if (Test-Something) { return $True } # Not found so do something to create it. Do-Something # Check it's been created successfully. …
Simon Elms
  • 17,832
  • 21
  • 87
  • 103
1
2
3
20 21