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

How to test the resolved parameter set?

I'm trying to write a basic Pester test that checks an "advanced" function for the resolved parameter set: function Do-Stuff { [CmdletBinding(DefaultParameterSetName='Set 1')] [OutputType([String])] Param ( …
Jaqueline Vanek
  • 1,100
  • 10
  • 20
2
votes
2 answers

Mocking a command and getting different results based on the number of times the Mock is called

I'm using Pester to unit test some code I've written. In the tests I mock Test-Path using a parameter filter: Mock -CommandName 'Test-Path' -MockWith { return $false } ` -ParameterFilter { $LiteralPath -and $LiteralPath -eq 'c:\dummy.txt' } The…
Alan T
  • 3,294
  • 6
  • 27
  • 27
2
votes
2 answers

How do I get code coverage for all branches using Pester?

Multiple test cases have been written to test a new Chocolatey function using Pester. How do check whether all branches have been covered?
030
  • 10,842
  • 12
  • 78
  • 123
1
vote
1 answer

Passing parameter to Pester -testcases

I am trying to figure out the best way to parameterise my Pester (V 5.3.3) testcases. The scenario is this. I would like to check that a list of logins exist on a group of database instances. My code is pretty repetitive, and looks like…
Andy
  • 399
  • 1
  • 2
  • 10
1
vote
1 answer

Remove xml comments based on xml tags inside the comments with Powershell

I want to remove comments in xml files based on the xml tags inside the comment with Powershell. Constraints: Multi line comments should be supported Keep xml formatting (e.g. do not write everything into a single line or remove indents) Keep file…
tomwaitforitmy
  • 509
  • 3
  • 16
1
vote
1 answer

Pester and test internal function

In my Pester-test I've created a function to Test-SQLConnection. I can test the function like this: param( [string] $Server, [string] $Database ) BeforeAll { $testDbServer = $Server $testDatabase = $Database } Describe "Status…
user3624378
  • 417
  • 4
  • 22
1
vote
1 answer

Pester 5.4 generates an empty Code coverage result file

POV: I'm generating Powershell test code coverage with pester. Problem: The generated Coverage.xml file remains empty. Question What am I doing wrong? Some technicalities: Write-Verbose -Message "Running Pester Tests" $conf =…
JDC
  • 1,569
  • 16
  • 33
1
vote
1 answer

PowerShell Tests: Module or Individual Files

Background I have a PowerShell module where I am starting to write the tests. Each function is stored in its own file with the same name (e.g. function MyFunction is stored in a file called MyFunction.ps1). Similarly, the tests are stored in a file…
Ninja
  • 356
  • 1
  • 3
  • 12
1
vote
1 answer

Pester how to test powershell script which does not have functions?

I have Azure powershell function which run.ps1 looks pretty much param($Request, $TriggerMetadata) Write-Host $Request.Param1 Write-Host $Request.ParamB Write-Host $Request.ParamC return 200 This is in the directory /func/myFunction/run.ps1 And my…
Kamsiinov
  • 1,315
  • 2
  • 20
  • 50
1
vote
0 answers

VS Code: Pester tests not showing in test explorer

My VS code test explorer tab is always empty for any Powershell Pester tests. I have the latest Powershell Extension installed (v2022.8.5). It looks like test discovering does not work, but I can run/debug single describe blocks nicely. Note: I…
tomwaitforitmy
  • 509
  • 3
  • 16
1
vote
1 answer

Pester with BeforeAll gives "Cannot process argument transformation on parameter 'Scriptblock'"

I have added a new unit test file for Pester 5 to my Powershell repo in VS code. When I try to run/debug it, I get a prompt in Terminal: cmdlet BeforeAll at command pipeline position 1 Supply values for the following parameters: Scriptblock: If I…
Rye bread
  • 1,305
  • 2
  • 13
  • 36
1
vote
0 answers

Cannot convert the "HeaderInterceptor" value of type "HeaderInterceptor" to type "HeaderInterceptor"

Thanks for taking the time to read my question! I'm having an issue \w Pester. When several tests are run with one Invoke-Pester cmd, I suddenly get PSInvalidCastException on blocks where I'm passing an inherited test class to a method that takes…
Mike
  • 179
  • 1
  • 1
  • 10
1
vote
1 answer

Testing with Pester 5.3.3

I have written a function to write the errors that occur in my functions in a csv file, this function is called in the catch block of my functions. I would like to write a Test in Pester to check that my function works correctly, but to be honest I…
1
vote
1 answer

Pester having problems with else statement

Hey guys so I have this code (Powershell v5), and when I run the pester for it, the else statement is not being detected. Runs perfectly fine on powershell v7 but need it to run on v5. Source Code Function 'INeedHelp' $array=…
1
vote
1 answer

How can I use pester v5 configuration or container with four standard arguments?

I'm trying to invoke a pester script, moving from pester V4.6.0 to V5.3.1 The V4 arguments I used to use, when invoking pester were: -supplying custom parameters -OutputFormat NUnitXML -OutputFile $xmlpath -EnableExit However, it seems for v5, I…
user13800089