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:
- Test-Driven Development with Pester (June Blender)
- Pester in Action Part 1: Pester Basics
- PowerShellMagazine articles about Pester
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 { $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" }else{ "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 $testResults #display specific tests within test suite $testResults.TestResult
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 = 'http://relevantcodes.com/Tools/ReportUnit/reportunit-1.2.zip' $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
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.
LikeLike
Great article.
LikeLike
There is certainly a great deal to learn about this subject.
I really like all of the points you have made.
LikeLiked by 1 person
Hi Jona,
thanks a lot for your comment.
LikeLike
This is awesome! Great reporting possibilities when it comes to reporting Pester results! Thanks!!!
LikeLiked by 1 person
Thanks Irwin! BTW, I saw the video with Jeffrey S.from the PSConfEU where he mentioned you. You are getting really popular those days ;-).
LikeLiked by 1 person
Hehe… Jeffrey Snover made the PSConfEU a memorable one for me! š
LikeLike