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

Pester custom error text

Does anyone know if Pester supports custom error texts? Lets say I've the following test: Describe "Get-HelloWorld" { It "outputs 'Hello world!'" { Get-HelloWorld | Should Be 'Hello world!' } } Is there a way that I can set some…
Moerwald
  • 10,448
  • 9
  • 43
  • 83
4
votes
1 answer

Pester not mocking function that is dot-sourced

I'm using Pester to test a PowerShell script that dot-sources another script. When I try to mock the function that is dot-sourced, Pester refuses to use the mocked version. I'm having the same problem when I try to source the function by using…
jgeinosky
  • 41
  • 2
4
votes
1 answer

Unit test ParameterBindingValidationException

Can somebody show me the best way to test non-nullable string parameters with Pester? I receive a ParameterBindingValidationException when I pass an empty string my PowerShell module. function Get-MyFunc { param ( [parameter(Mandatory=$true)] …
Trujllo
  • 100
  • 1
  • 10
3
votes
1 answer

Use a PowerShell hash's key/values with Pester TestCases

I would like to pass the key/values of a PowerShell hash to a Pester unit test, via the TestCases parameter: BeforeAll { $Expected = @{ Address1='Address1' Address2='Address2' City='City' RegionCode='RegionCode' …
craig
  • 25,664
  • 27
  • 119
  • 205
3
votes
2 answers

PowerShell Pester Mock Rest API Calls

Is there any simple approach on how to mock a Rest API Calls in Pester. Here is my code, I just need to mock those Rest API Calls in Pester and test them, could someone help me here. Describe 'Test worldclockapi.com' { BeforeAll { …
Atdhe Kurteshi
  • 373
  • 5
  • 14
3
votes
1 answer

Can I Automate pester Tests in Azure test plans?

So I am new to Azure Test plans and created few test cases. I wanted to link/associate my tests (written using pester) with the test cases in azure test plans. I saw many tutorials but it's all about Visual studio and c# tests - how to associate…
3
votes
2 answers

Pester 5 variable scoping issue - BeforeDiscovery/It

Edit The crux of the question is: how do I get access to variable(s) declared in a BeforeDiscovery block in my It blocks that are not passed through by the it -foreach $var construct? I'm having difficulties adjusting to the Discovery/Run phase and…
Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146
3
votes
1 answer

Do my test code and function code need to be in the same directory for Pester Code Coverage to work?

My test and function code were in the same directory and I was getting the results of code coverage which was working fine. However after splitting the code into seperate directories Src and Tests I am now seeing the error below regarding…
3
votes
0 answers

How to create a new video file in Powershell

I would like to create a new video (MP4) file in PowerShell. The contents of the video do not matter but should be unique. The file size should be short (5 seconds). The is required as part of an integration test I am using PowerShell and Pester…
Martin Kearn
  • 2,313
  • 1
  • 21
  • 35
3
votes
1 answer

Pester v5.1: Should -Throw with message containing square-brackets fails

I'm using pester to test my thrown exception (negative tests). Although it seems, that the thrown exception message is a exact match with my Should -Throw, Pester declares the test failed, when there's square brackets in the exception message. By…
tommijo
  • 43
  • 7
3
votes
2 answers

Mocking class functions with Pester 5 and PowerShell 7

Does anyone have an example of mocking a dot-sourced class function with Pester 5 and PowerShell 7? Thank you. Edit: example Classes\MyClass.ps1: class MyClass { [void] Run() { Write-Host "Class: Invoking run..." …
XeonFibre
  • 33
  • 3
3
votes
1 answer

Powershell Core + Pester - Separating tests from src

Question: What would be the best way to import functions to tests that don't reside in the same directory? Example src Get-Emoji.ps1 test Get-Emoji.Tests.ps1 Inb4 Pester documentation[1] suggests test files are placed in the same…
Hoppjerka
  • 128
  • 1
  • 9
3
votes
3 answers

Invoke-Pester -OutputFile and -OutputFormat are member of the legacy parameter set

In Pester 4 the commandlet offers the possibility to explicitly specify the OutputPath. Invoke-Pester -Script $testFile -PassThru -Verbose -OutputFile $tr ` -OutputFormat NUnitXml -CodeCoverage "$tmp/*-*.ps1" ` -CodeCoverageOutputFile $cc…
melanie9701
  • 31
  • 1
  • 3
3
votes
3 answers

How to specify what to do if a Pester assertion fails?

If you wanted to say, 1 should equal 1, and if it doesn't then break, what would be the most eloquent way to do this in powershell with pester avoid code duplication? Eg {1 | should be 1} else {break} rather than 1 | should be 1 if (1 -ne 1)…
user13800089
3
votes
1 answer

Pester 5.0.2 BeforeAll block code not showing up in Describe block

I recently switched over to Pester 5.0.2 to start doing some testing on a Powershell script I wrote. I am able to break into the BeforeAll block and the paths are being populated as I would expect, however when I get to the Get-ChildItem area in my…
Efie
  • 1,430
  • 2
  • 14
  • 34
1 2
3
20 21