Using Tags to Include or Exclude Pester Tests

Datetime:2016-08-22 23:28:39          Topic:          Share

While it’s fairly simple to filter through Pester tests by their names with the TestName parameter, it’s not always a great way to limit a run of tests. I recently received a pull request to improve the Vester project with testing tags. Not realizing that this was a feature of Pester, I thought I’d share how it works. The idea is that each Describe block within a Pester test can have one or more tags applied to help make filtering tests much simpler and more effective.

Here’s a sample bit of code from the Update-DRS.Tests.ps1 file.

Describe -Name 'Cluster Configuration: DRS Settings' -Tags @("vcenter","cluster")

Note that the test above can now be referenced with either the vcenter or cluster tag. This can be done in two ways:

  • Inclusive – use the Tag  param to limit a run of Pester tests to a specific set of tags.
  • Exclusive – use the ExcludeTag  param to run all tests  except for the ones with the specific set of tags.

Example: Invoke-Pester .\Vester\Tests -Tag host -ExcludeTag nfs will run tests with the host tag but skip tests with the nfs tag.

This was a great pull request and I am thrilled to learn more about Pester through the eyes of community contributions! The README file has been updated appropriately, too.