Running Powershell Pester unit test in a VSTS build pipeline

When you are developing Powershell scripts, creating some unit tests will help you in monitoring the quality of the scripts. Writing some tests will give you some assurance that your code still works after you make some changes. Writing Powershell unit tests can be done with Pester. Pester will enable you to test your Powershell scripts from within Powershell. It is a set of Powershell functions for unit testing Powershell. These functions will allow you to mock and isolate the Powershell code under test. When you want to integrate your unit test into your VSTS build pipeline, you need an build extension to run then in your build pipeline.

Adam Bertram @adbertram

It’s Monday. Run those PSPester tests for your code before getting started. You never know how you left that code on Friday!

Run tests in VSTS
When you want to run your Pester unit test in your VSTS build pipeline, you need to add the Pester task from the marketplace into your VSTS project. Then open the build pipeline and Add the Pester task.

The following configuration will run all Pester unit tests in your project:

Test files           : **/*.tests.ps1
Fail build on error  : true

Upload the tests result
When you want the test results visible in VSTS, you need to upload the test result file. This can be done with the Upload test results task. Pester will write the test results in nUnit format to a test results file. This test results file is located in the test results directory of build agent. That is one directory higher than the working directory of the task.

The following configuration can be used to upload the test results to VSTS:
Test Result Format   : NUnit
Test Results Files   : ../**/TEST-*.xml
Always run           : true



4 thoughts on “Running Powershell Pester unit test in a VSTS build pipeline”

  1. Hi,
    very good article to Pester and VSTS.
    When it try to use the Task i get an error like below. Do you have any idea or a possible solution?

    Thanks and regards

    Executing the powershell script: C:\a\_tasks\Pester Powershell unittest_31f040e5-e040-4336-878a-59a493334434\1.1.6\Pester.ps1
    2017-03-24T12:02:45.5222145Z ##[error]System.ArgumentException: Illegal characters in path.
    2017-03-24T12:02:45.5222145Z at System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String fullPath)
    2017-03-24T12:02:45.5222145Z at System.Security.Permissions.FileIOPermission.QuickDemand(FileIOPermissionAccess access, String[] fullPathList, Boolean checkForDuplicates, Boolean needFullPath)
    2017-03-24T12:02:45.5222145Z at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)
    2017-03-24T12:02:45.5222145Z at System.IO.DirectoryInfo.EnumerateDirectories(String searchPattern)
    2017-03-24T12:02:45.5222145Z at Microsoft.PowerShell.Commands.FileSystemProvider.Dir(DirectoryInfo directory, Boolean recurse, UInt32 depth, Boolean nameOnly, ReturnContainers returnContainers)
    2017-03-24T12:02:45.5222145Z Test files found:
    2017-03-24T12:02:48.4147181Z Installing latest version of pester
    2017-03-24T12:02:54.3892946Z Microsoft.PackageManagement.Packaging.SoftwareIdentity
    2017-03-24T12:02:54.7502954Z Pester installed: 4.0.3
    2017-03-24T12:02:54.7552955Z C:\a\1\TestResults\TEST-d72f50a8-2f43-47d7-ae12-b1e0a596f9df.xml
    2017-03-24T12:02:54.7564391Z Writing pester output to C:\a\1\TestResults\TEST-d72f50a8-2f43-47d7-ae12-b1e0a596f9df.xml


  2. If I would guess what is going on I would say that this line fails:
    $TestFiles=$(get-childitem -path $env:BUILD_SOURCESDIRECTORY -recurse $ItemSpec).fullname

    $ItemSpec is the argument from the Test Files in the task configuration. Try to put: ‘**\*.tests.ps1’ in the field.


  3. Hi Daniel
    I had the exact same error, my problem was that I was doing a proof of concept with Pester and VSTS so I only had a single file in the root directory of my Git repo. If this is the case for you then the easy fix is to change the test files path to just ‘*.tests.ps1’
    Thanks for the great post and VSTS task Peter


    1. It’s not a matter of repo design. The default value is indeed broken. My test files are under $ProjectRoot\$ModuleName\Tests\*.tests.ps1 and the only argument that made it work (after trying almost all possible glob-lie patterns) was ‘*.tests.ps1’.
      I think this extension’s search pattern is somewhat broken, but it’s feasible to make it run.


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s