Home > Net >  How can I dynamically create and add tags in PowerShell script during Invoke-Pester command?
How can I dynamically create and add tags in PowerShell script during Invoke-Pester command?

Time:02-12

I have several Describe/It tests in a ps1 file called Behavior.Tests.ps1. Each test, however, can only run successfully on systems with specific hardware. I am aware that I can use tags like this in the Invoke-Pester command:

Invoke-Pester $path -Tag "Acceptance" -ExcludeTag "Flaky", "Slow", "LinuxOnly"

But I cannot count on the user to know the specifics of their hardware and choose the correct -Tag and -ExcludeTag to include. What I would rather do for my workflow is simply have Invoke-Pester .\Behavior.Tests.ps1, and then in the script have code like this that would get hardware info and build the right list of Tags and ExcludeTags:

if ($currentHardware -in $HardwareList) {
    $ExcludeTag  = $TagFlaky
}

Then I would have a list of Tags and ExcludeTags before the first Describe/It that would run the right tests in the entire ps1 file. Is this possible in v5 Pester? I tried to debug and find the argument -Tag to modify it, but I could not find it.

CodePudding user response:

You could create a wrapper PowerShell script that determines the tags prior to calling Invoke-Pester and then you can use that to Invoke Pester with the required tags.

For example:

$ComputerInfo = Get-ComputerInfo

$OS = switch ($ComputerInfo.WindowsProductName) {
    { $_ -match 'Windows 10' } { 'Win10' }
    { $_ -match 'Windows 8' { { 'Win8' }
    { $_ -match 'Linux' } { 'Linux' }
}

$Processor = switch ($ComputerInfo.CsProcessors.Name) {
    { $_ -match 'Intel' } { 'Intel '}
    { $_ -match 'ARM' } { 'ARM '}
}

Invoke-Pester $Path -Tag $OS,$Processor
  • Related