Powershell Pester 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


10 thoughts on “Powershell Pester 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.


  4. Pester tests are failing in Azure pipelines Windows-2019 agents.
    They are running successfully in self hosted agents.
    Confirmed that the issue is with Azure agents by running an empty pester test and got the same errors.
    I used RunPesterTest: Unit task in pipeline.
    Please let me know how to resolve this.

    Please find the below error log:

    2020-06-03T11:46:50.4275788Z ##[section]Starting: Run Pester Tests: Unit
    2020-06-03T11:46:50.4385034Z ==============================================================================
    2020-06-03T11:46:50.4385363Z Task : Pester powershell unittests
    2020-06-03T11:46:50.4385625Z Description : Run Pester powershell unittests
    2020-06-03T11:46:50.4385831Z Version : 2.5.2
    2020-06-03T11:46:50.4386037Z Author : Peter Groenewegen – Xpirit
    2020-06-03T11:46:50.4386442Z Help : [More Information](https://pgroene.wordpress.com/2017/01/30/running-powershell-pester-unit-test-in-a-vsts-build-pipeline/)
    2020-06-03T11:46:50.4386858Z ==============================================================================
    2020-06-03T11:46:50.4431130Z ##[warning]Task ‘Pester Powershell unittest’ (2.5.2) is using deprecated task execution handler. The task should use the supported task-lib: https://aka.ms/tasklib
    2020-06-03T11:46:50.4444265Z Preparing task execution handler.
    2020-06-03T11:47:17.0657881Z Executing the powershell script: d:\a\_tasks\Pester Powershell unittest_31f040e5-e040-4336-878a-59a493334434\2.5.2\Pester.ps1
    2020-06-03T11:47:17.0659839Z WorkingDirectory: d:\a\1\s
    2020-06-03T11:47:47.2635608Z Installing latest version of pester
    2020-06-03T11:47:57.0906713Z ##[warning]Module ‘Pester’ version ‘3.4.0’ published by ‘CN=Microsoft Windows, O=Microsoft Corporation, L=Redmond, S=Washington, C=US’ will be superceded by version ‘5.0.2’ published by ‘CN=Jakub JareÅ¡, O=Jakub JareÅ¡, L=Praha, C=CZ’. If you do not trust the new publisher, uninstall the module.
    2020-06-03T11:48:01.4612777Z D:\a\1\TestResults\TEST-976cfa2e-57ba-4b9d-823c-cddfeb224f1c.xml
    2020-06-03T11:48:01.4631667Z Writing pester output: d:\a\1\TestResults\TEST-976cfa2e-57ba-4b9d-823c-cddfeb224f1c.xml
    2020-06-03T11:48:01.4633949Z Files: PPMApplyVersion\buildAndReleaseTask\*.Tests.ps1
    2020-06-03T11:48:01.6410467Z Tags included: Unit
    2020-06-03T11:48:01.6411277Z Invoke-Pester with the following parameters
    2020-06-03T11:48:01.6411881Z System.Collections.Hashtable
    2020-06-03T11:48:02.4721280Z ##[warning]You are using Legacy parameter set that adapts Pester 5 syntax to Pester 4 syntax. This parameter set is deprecated, and does not work 100%. The -Strict and -PesterOption parameters are ignored, and providing advanced configuration to -Path (-Script), and -CodeCoverage via a hash table does not work. Please refer to https://github.com/pester/Pester/releases/tag/5.0.1#legacy-parameter-set for more information.
    2020-06-03T11:48:02.6733337Z System.Management.Automation.RuntimeException: No test files were found and no scriptblocks were provided.
    2020-06-03T11:48:02.6734414Z at Invoke-Pester, C:\Users\VssAdministrator\Documents\WindowsPowerShell\Modules\Pester\5.0.2\Pester.psm1: line 4197
    2020-06-03T11:48:02.6735331Z at , D:\a\_tasks\Pester Powershell unittest_31f040e5-e040-4336-878a-59a493334434\2.5.2\Pester.ps1: line 84
    2020-06-03T11:48:02.6799685Z ##[error]Microsoft.PowerShell.Commands.WriteErrorException: Error Pester: 1 or more tests failed
    2020-06-03T11:48:02.7247703Z ##[error]PowerShell script completed with 1 errors.
    2020-06-03T11:48:02.7255796Z ##[section]Finishing: Run Pester Tests: Unit


    1. I’m not sure, it could have to do with the pester version:
      ##[warning]You are using Legacy parameter set that adapts Pester 5 syntax to Pester 4 syntax. This parameter set is deprecated, and does not work 100%. The -Strict and -PesterOption parameters are ignored, and providing advanced configuration to -Path (-Script), and -CodeCoverage via a hash table does not work. Please refer to https://github.com/pester/Pester/releases/tag/5.0.1#legacy-parameter-set for more information.


      1. Sorry for the late response, I’m currently not doing anything with powershell or pester. If you do a PR on the github repro, I’ll definitely check it and update the package if there are good updates.


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: