Skip to content

Custom assertion operators can't easily be reused #891

@brantb

Description

@brantb

I have a few custom assertion operators which I'd like to use in multiple test suites, but Pester only allows an assertion to be defined once.

Assertions.ps1

function IsAwesome($ActualValue, $ExpectedValue, [switch]$Negate) {
  # ... snip implementation ...
}
Add-AssertionOperator -Name IsAwesome -Test $function:IsAwesome

Get-SomethingInteresting.Tests.ps1

. "$PSScriptRoot\Assertions.ps1"
Describe 'Get-SomethingInteresting' {
  # ... snip tests ...
}

Set-SomethingInteresting.Tests.ps1

. "$PSScriptRoot\Assertions.ps1"
Describe 'Set-SomethingInteresting' {
  # ... snip ...
}

Actual result

PS> Invoke-Pester
Executing script Get-SomethingInteresting.Tests.ps1
  (...snip output...)
Executing script Set-SomethingInteresting.Tests.ps1
  [-] Error occurred in test script 'Set-SomethingInteresting.Tests.ps1' 59ms
    at Assert-AssertionOperatorNameIsUnique, C:\path\to\Pester\4.0.8\Pester.psm1: line 243
    Assertion operator name 'IsAwesome' has been added multiple times.

Expected result

I can reuse Assertions.ps1 in multiple test files, somehow.

As a workaround for this I could create a single TestEntryPoint.Tests.ps1 which dot-sources Assertions.ps1 plus all of my test files, then run my tests by calling Invoke-Pester -Script TestEntryPoint.Tests.ps1, but I like simply running Invoke-Pester and having everything work.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions