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
1
vote
1 answer

Pass Variables to Powershell Pester Tests v5

I am at the last stages with my pester tests, but am having issues with the variable scoping. I have quite a few tests and would like use something like Global scoping in order to declare variables such as $ModuleName, $ProjectRoot, and other…
Josh Wright
  • 422
  • 3
  • 12
1
vote
1 answer

Pester InvokeVerifiable verify specific mock was called

I'm struggling a bit with the v5 Pester way of using mocks. Can anyone give me an example how I can verify that a specific mock was called via Should -InvokeVerifiable? And also how I can check that a specific mock was called n-times. I also checked…
Moerwald
  • 10,448
  • 9
  • 43
  • 83
1
vote
2 answers

Pester 5: Automate It

For my hobby project ConvertTo-Expression, I am rebuilding my test (Pester 5) script. I would like to automate the It (and possibly the Context part) as there are large number of syntax formats to test for and the function actually roundtrips which…
iRon
  • 20,463
  • 10
  • 53
  • 79
1
vote
0 answers

Powershell Pester Unit Test fails to mock Expand-Archive

I have an issue, where I try to mock the Windows built-in cmdlet Expand-Archive in a pester unit test. Somehow, during the execution the original cmdlet is executed rather than the mocked variant. I've tried putting the Expand-Archive mock outside…
Michael
  • 153
  • 1
  • 14
1
vote
0 answers

Error on running Pester test using foreach-object -parallel

I have this infrastructure pester test. Code for demonstration purpose: Describe 'WEB-Tests' { $servers = 'ServerA','ServerB' $sessions = @() foreach ($server in $servers) { $sessions += New-PSSession -ComputerName $server } …
medinibster
  • 57
  • 2
  • 11
1
vote
1 answer

Mix pester Discovery and Run variables

I have this pester script block. When I run it, the "Test1" fails, which is good. The second "Test2" fail, but I want it to pass. For pester 5 this are the recomandations: `Put all your code into It, BeforeAll, BeforeEach, AfterAll or AfterEach. Put…
1
vote
1 answer

It skips it-statement when executing function in Pester, but shouldn't

I am trying to test a Function in an script file that is testing the connection to a PC. I am trying to do that by calling the mock "Test-Connection" from an other Script in an Pester Test. When I run Temp.Testing.ps1 describe 'Test Error Handling'…
Gumpex
  • 23
  • 6
1
vote
1 answer

How can you mock an ErrorRecord with Pester in Powershell?

I have a function that I would like to include in my unit tests but I'm unable to figure out how. The method takes in an ErrorRecord object (from an Invoke-RestMethod call), grabs the status code and generates a custom error message. Here is what…
Efie
  • 1,430
  • 2
  • 14
  • 34
1
vote
1 answer

Case sensitive option for Pester's -FileContentMatchMultiline in PowerShell

I have a dozen lines or so that need to be consistent across multiple .ps1 files. I was able to get this working using the -FileContentMatchMultiline functionality in Pester but I need the match to be case sensitive. Is there any simple way to do…
Efie
  • 1,430
  • 2
  • 14
  • 34
1
vote
3 answers

Pester test evaluating type PSCustomObject

I wrote a unit test in pester where it verifies the result against the returned object (a hash table) created using PSCustomObject, but I am not sure how to definite it: $result = get-dataFromOverThere $result | Should -Be [PSObject] after invoking…
RSLAV
  • 53
  • 6
1
vote
2 answers

How do you write a Pester mock for task sequence variable setting

I am working on writing Pester tests for our PowerShell scripts that are used during task sequences. Several of them work with the task sequence variables and so I wrote a mock that allows for testing reading variables and am now trying to figure…
1
vote
2 answers

Pester Test for scriptproperty return type

I am new to pester and trying to create tests for PowerShell class modules. I am trying to determine how to test for my particular method of implementing properties for my classes. The basic structure that I use is: class TestClass { hidden…
TimN_FL
  • 67
  • 9
1
vote
1 answer

Pester test is failing when an expected error is thrown

I'm trying to use Pester for PowerShell to test some of my code, but I'm unable to get Pester to work in relation to errors. Take this very basic example - using module AccessTokenRequestModel InModuleScope -ModuleName AccessTokenRequestModel…
David Gard
  • 11,225
  • 36
  • 115
  • 227
1
vote
1 answer

Any dynamic way to use "using" statement in PowerShell?

I'm having issues with the using statement which can be seen here. This statement (unlike Import-Module) is required in order to get an actual class definition from a PowerShell module. I have a use case for this because I need the class definition…
Birdman
  • 1,404
  • 5
  • 22
  • 49
1
vote
1 answer

Coverage of powershell script using Pester

I am trying to unit test my Powershell script file with below code snippet using Pester #code to create a 7z file $7zipPath = "C:\Program Files\7-Zip\7z.exe" Set-Alias 7zip $7zipPath if (!(Test-Path -Path $7zipPath -PathType Leaf)) { throw "7…
An Kumar
  • 77
  • 1
  • 8