Is there a way to test / assert output from Write-Host using Pester?

Question:

I’m writing tests for a fairly complex script and there’s one particular function in the script that will output different series of logging messages to the user. I would like to assert whether a particular logging message is being displayed.

The main issue is that I don’t know what parameter is implicitly handling the text that I’m passing the Write-Host cmdlet.

Here is some code that captures the premise of what I am trying to do…

Function to test

Pester test

Output

Answer:

After reviewing the Microsoft documentation for the Write-Host cmdlet, I found that there is an -Object parameter. This accepts an array of generic objects to write to the console. The -Object parameter is the parameter which needs to be specified in the -ParameterFilter of the Pester tests in order to assert that the appropriate text is being displayed.

My updated code is as follows…

Function to test

Pester test

Output

Source:

Is there a way to test / assert output from Write-Host using Pester? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply