Run your Pester tests from GitHub in a VSTS/VSO build

With your PowerShell scripts in a repository like GitHub and working Pester tests it’s you no want to set up a continuous integration build that executes all your tests after each commit and indicates that current state of your build on the public repository. This can be done with any CI platform – but I will show you how easy it is to set it up with Visual Studio Team Services (VSTS).

This is Part 3 of the series “Develop next level PowerShell with Visual Studio and Pester” and assumes that you have a working solution with pester tests in GitHub.

Post Content
Part 1: Develop next level PowerShell with Visual Studio and Pester In this post I focus on creating the project in Visual Studio and interacting with a source control system like git.
Part 2: Use Pester to author your PowerShell scripts using TDD/BDD This post focuses on writing PowerShell scripts or modules using Pester as a TDD/BDD framework.
Part 3: Run your Pester tests in a VSTS build In this post I show you how you can run you tests in a continuous integration build and display the build status with a badge in your repository.

Connect VSTS to GitHub

To build your repository on GitHub you first have to connect your VSTS project to your GitHub project. Go to the settings of your Team Project and under “Services” open “New Service Endpoint” and select GitHub.


Click “Authorize” and log in to GitHub with your credentials.


Give the connection a name and save it.


Create a VSTS CI build

Create a new build in your team project by clicking the “Plus” under “Builds”.


Select the empty template and GitHub as the source and enable continuous integration.


To execute the pester tests we have to use a PowerShell script. The problem is, that Pester is not installed on the hosted build agent. So if we don’t want to set up a separate machine we have to install pester inside the script. The problem is, that psget and chocolatey are not working on the hosted build agent because of security restrictions. I found a nice script from Johan Classon in his post that I had to adjust slightly. The script downloads Pester from github as a zip file and installs it. It then runs Invoke-Pester on the entire repository – so all scripts that contains a .test in the name are executed. The output is stored in a file in NUnit format and can be published to VSTS later on. The EnableExit switch fails the build if a test fails.

	[string]$SourceDir = $env:BUILD_SOURCESDIRECTORY,
    [string]$TempDir = $env:TEMP
$ErrorActionPreference = "Stop"

$modulePath = Join-Path $TempDir Pester-master\Pester.psm1

if (-not(Test-Path $modulePath)) {

	# Note: PSGet and chocolatey are not supported in hosted vsts build agent  
    $tempFile = Join-Path $TempDir
    Invoke-WebRequest -OutFile $tempFile

    [System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') | Out-Null
    [System.IO.Compression.ZipFile]::ExtractToDirectory($tempFile, $tempDir)

    Remove-Item $tempFile

Import-Module $modulePath -DisableNameChecking

$outputFile = Join-Path $SourceDir "TEST-pester.xml"

Invoke-Pester -Path $SourceDir -PassThru -OutputFile $outputFile -OutputFormat NUnitXml -EnableExit

Place the script anywhere in your repository and commit and push.

No add two build steps to your build: one to execute the PowerShell script and one to publish the test results.



In the PowerShell task, brows your script in your repository.


Then set the “Test Result Format” of the Publish Test Results” to “NUnit”.


Enable Badge

You can add an image to the GitHub indicating the last status of the build. To do this, go to general, check “Enable Badge” and click the link “Show Url…”. Copy the url to your clipboard and save your build.


Go to GitHub, navigate to yout and edit it.


Insert the URL of the badge into your markdown ![Build Status](https://…/badge). Enter a comment and commit your changes directly to the master branch.


The commit of the readme now triggers automatically a build. Go to VSTS and wait for the build to finish. In the build sumary page you now see all your test results.


In your start page of your GitHub repo you see the content of your that contains the badge indicating the current build status.



What’s next?

I hope I could give you an introduction how you can leverage the Power of Visual Studio, Pester and VSTS together with an external platform like GitHub to write high quality scripts with continuous integration. If you want to try it, you can use the sample code from my repo on github.

What I’m working on next is to get the code coverage results from Pester to a universal format so that I can publish them to the build results. I also will try to add the PSScriptAnalyzer (static code analysis for PowerShell) to the ci pipeline and publish the results to the build.

6 thoughts on “Run your Pester tests from GitHub in a VSTS/VSO build

Leave a Reply

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

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

Facebook photo

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

Connecting to %s