Reporting against Pester test results

Pester is (for very good reasons) getting more and more popular. If you don’t know about Pester I would highly recommend you to start using it. Here are some good resources to learn about the framework:

In this post, I assume that you have already some previous experience using Pester. Most of the articles and videos about Pester I’ve seen so far, do not go into much details about reporting on test results from Pester. Let’s first see what the result could look like:

The screenshot above shows the output from ReportUnit, which can take the Pester NUnit XML output and turn it into a very nice HTML report.
Ok, having seen what could be done, we take a step back and see what is possible using the built-in Pester capabilities. Let’s create some dummy functions and tests first:

$tempFolder = New-Item "$env:Temp\PesterTest" -ItemType Directory -force
foreach ($num in (1..10)){
    $functionTemplate =  @"
function Test$num {
"@ | Set-Content -Path (Join-Path $tempFolder "Test$num.ps1")
    $testTemplate = @"
`$here = Split-Path -Parent `$MyInvocation.MyCommand.Path
`$sut = (Split-Path -Leaf `$MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.'
. "`$here\`$sut"
Describe "Test$num" {
    It "Should output $num" {
        $(if ($Num -eq 8){
        "Test$num | Should Be 9"
        "Test$num | Should Be $num"
"@ | Set-Content -Path (Join-Path $tempFolder "Test$num.Tests.ps1")

With a few liens of code we created a folder (PesterTest) within the temp directory that contains 10 script files (Test1 – Test10.ps1) including a simple function that outputs a number in correspondence to the script number. We also created a very basic test against each script file which tests whether the function’s output is correct. For good measure I’ve also added a bug into the Test8.ps1 script.
Running Invoke-Pester against the folder without any additional arguments results in the default console output for Pester:

While this looks nice, it’s not good enough if you want to run this unattended/automated or if you have a very long list of tests in your test-suite. Using the ‘-PassThru’ switch Parameter will make Pester return a rich object containing the detailed test results and also a lot of contextual information (error message, environment, stacktrace….) in addition to the console output:

$testResults = Invoke-Pester -PassThru
#display overall test-suite results
#display specific tests within test suite

Pester can even do better, using the ‘OutputFile’ and ‘OutputFormat’ the result is turned into an XML in NUnit compatible format. The .xml file can be imported into tools like TeamCity in order to view test results in a human readable way. For people without access to full-fledged development tools, there is ReportUnit an open source command line tool that automatically transforms the XML into a nice HTML report (see screenshot at the top of the post). Let’s use PowerShell to download and extract ReportUnit.exe and run it against an output file generated by Pester:

#run the test-suite and generate the NUnit output file
Push-Location $tempFolder
Invoke-Pester -OutputFile report.xml -OutputFormat NUnitXml

#download and extract ReportUnit.exe
$url = ''
$fullPath = Join-Path $tempFolder $url.Split("/")[-1]
(New-Object Net.WebClient).DownloadFile($url,$fullPath)
(New-Object -ComObject Shell.Application).Namespace($tempFolder.FullName).CopyHere((New-Object -ComObject Shell.Application).Namespace($fullPath).Items(),16)
del $fullPath

#run reportunit against report.xml and display result in browser
& .\reportunit.exe report.xml
ii report.html

Happy testing!


Photo Credit: Photosightfaces via Compfight cc

7 thoughts on “Reporting against Pester test results

  1. Hey I am so delighted I found your site, I really found you by accident, while I
    was browsing on Aol for something else, Anyhow I am
    here now and would just like to say kudos for a remarkable
    post and a all round exciting blog (I also love the theme/design), I don’t
    have time to go through it all at the minute but I have
    saved it and also included your RSS feeds, so when I have time I will
    be back to read a great deal more, Please do keep up the fantastic work.


I'd love to hear what you think

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s