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

How do I mock Read-Host in a Pester test?

If i have this function: Function Test-Foo { $filePath = Read-Host "Tell me a file path" } How do i mock the Read-Host to return what i want? e.g. I want to do something like this (which doesn't work): Describe "Test-Foo" { Context "When…
Pete Whitehead
  • 178
  • 2
  • 11
3
votes
1 answer

Why TestExplorer in VS2015 failed to load Pester module?

I installed the Powershell Tools for Visual Studio 2015 then I created a tests.ps1 files inside my Powershell project in VS2015. the test explorer recognizes the declared test, but when I try to run the test I get this: "Result Message: Failed to…
XtianGIS
  • 967
  • 16
  • 39
3
votes
1 answer

Pester: not all mocked functions are intercepted

I have a number of modules, including ModuleMain and ModuleSql. There is an interdependence between the modules such that Main-Function in ModuleMain uses 4 functions from ModuleSql: function Main-Function { …
Charlie Joynt
  • 4,411
  • 1
  • 24
  • 46
3
votes
1 answer

Pester Mock does not work for Invoke-Command using script block

I have a console logger function Common-Write-Log-Console { param ( [Parameter(Mandatory=$true)] [string] $logText ) $textToOutput = [String]::Format("{0}:{1}", [System.DateTime]::Now.ToString(), $logText) …
3
votes
2 answers

Using Pester to test a PowerShell module, my mocked function doesnt return a value

I'm using Pester, a PowerShell testing library to help with TDD / unit test coverage. I'm trying to mock out Get-ChildItem for tests I have inside a module that are supposed to do our environment setup. If I have my mocked Get-ChildItem function…
Josh R
  • 1,970
  • 3
  • 27
  • 45
2
votes
2 answers

PowerShell module with class defined in separate file fails Pester tests in GitHub Actions

I am creating a PowerShell module that defines a class. For example: class MyClass { [string] $Name } If I put the class definition directly in the psm1 file then everything works fine. However, if I move the class to its own file and include…
deadlydog
  • 22,611
  • 14
  • 112
  • 118
2
votes
2 answers

Is there any way to test functions in a PowerShell script without executing the script?

I would like to define stand-alone functions in my PowerShell script and be able to Pester test the functions without executing the rest of the script. Is there any way to do this without defining the functions in a separate file? In the following…
successhawk
  • 3,071
  • 3
  • 28
  • 44
2
votes
1 answer

Pester version 5 pass variables to test

I'm stuck on how to pass parameters to my Pester v5 tests. This is my config-file in Pester: Import-Module Pester -Force $testFile = 'C:\PowerShell\MyTests\' $tr = 'C:\PowerShell\MyTests\TEST-MyTests.xml' $configuration = [PesterConfiguration]@{ …
user3624378
  • 417
  • 4
  • 22
2
votes
1 answer

Get return value from kubectl exec out into powershell script

So I'm working on a powershell script that runs a pester test. The script connects to a Kubernetes pod with a Mongo database. The goal is to check whether or not a collection in the database is empty. I'm happy with the code up until the "return…
a_wahab
  • 41
  • 3
2
votes
1 answer

Pester Should -Throw does not catch error

This is the same problem and solution as this, but with simpler examples and hopefully easier to find, since it took me hours to find the above question. I have the following code that I want to test: Test-Function(){ {...} if($ExitCode -ne 0){ …
Kevin Holtkamp
  • 479
  • 4
  • 17
2
votes
0 answers

Exclude functions from Pester code coverage

The latest versions of Pester work with a PesterConfiguration object to set the configuration for the unit testing, as described here. In the 'old' command line settings of Invoke-Pester I could utilize a specific setting (Function) for Code…
user3596100
  • 147
  • 9
2
votes
1 answer

Pester Mocking CmdLets

I have question regarding to the mocking mechanism in Pester. I have a script Script.ps1 that I want to test. The tests are located in Script.Tests.ps1. In Script.ps1 I implemented the following function: function Start-Executable { …
dhelfer
  • 23
  • 2
2
votes
1 answer

PowerShell, Pester and Gherkin - Testing for Multiple Acceptable Values

I'm trying to write some tests using PowerShell, Pester and Gherkin but I'm struggling to work out how I can test against multiple values. For example, I could use Get-Service to check the status of a Windows service. If I want to check if it is…
xsquared_uk
  • 21
  • 1
  • 4
2
votes
1 answer

Pester test doesn't fail with the Array missing values

We are writing Pester test for testing the Azure Resource group to contain certain tags. Following is the script and unfortunately the Pester test is not reporting any failure even after a particular resource group we are checking doesn't contain…
user42012
  • 722
  • 12
  • 33
2
votes
2 answers

Show content of hashtable when Pester test case fails

Problem When a Hashtable is used as input for Should, Pester outputs only the typename instead of the content: Describe 'test' { It 'test case' { $ht = @{ foo = 21; bar = 42 } $ht | Should -BeNullOrEmpty …
zett42
  • 25,437
  • 3
  • 35
  • 72