pester / Pester

Pester is the ubiquitous test and mock framework for PowerShell.
https://pester.dev/
Other
3.08k stars 470 forks source link

How to get Pester to see custom Should operators? #590

Closed kamranayub closed 7 years ago

kamranayub commented 8 years ago

I must be missing something simple because I feel really close.

I have defined a new PesterBeDate function and failure functions. According to what I see in the Should assertion source, it should be able to find my command. In fact if I run:

Get-Command PesterBeDate -CommandType Function

I see the command output. But for some reason Pester doesn't:

RuntimeException: 'BeDate' is not a valid Should operator.
   at Get-TestResult, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.2\Functions\Assertions\Should.ps1: line 42
dlwyatt commented 8 years ago

Just put your function into a PS1 file in the Pester\Functions\Assertions directory, and reload the module. Should be all set. :)

kamranayub commented 8 years ago

Ah, but this for an OSS project... Is there a way to source Pester not from global? Or any other way to "extend" Pester so people don't need to do custom stuff to contribute?

On Fri, Aug 12, 2016, 12:31 Dave Wyatt notifications@github.com wrote:

Just put your function into a PS1 file in the Pester\Functions\Assertions directory, and reload the module. Should be all set. :)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/pester/Pester/issues/590#issuecomment-239509345, or mute the thread https://github.com/notifications/unsubscribe-auth/AAiaa6w7FzAGQOLifRszwyRvegrBDHi7ks5qfK3WgaJpZM4JjSwc .

dlwyatt commented 8 years ago

It probably depends on where you've defined those functions. Unless they're in the global scope, Pester's calls to Get-Command aren't going to find them. (The way that Should operators get loaded is also changing in v4.)

However, for now, you can inject your functions into Pester's scope after loading the module and it should work:

$pesterModule = Import-Module Pester -PassThru

. $pesterModule {
    function PesterBeDate { }
    # etc
}

Edit: I'm not 100% certain whether you need to use the . operator here, but I changed the code to be on the safe side. Otherwise the functions might be discarded when your script block goes out of scope.

kamranayub commented 8 years ago

Thanks, I did end up sourcing a local module for better out of the box tests for contributors. Then just copying any assertions after the fact and that works fine.

On Mon, Aug 15, 2016, 20:24 Dave Wyatt notifications@github.com wrote:

It probably depends on where you've defined those functions. Unless they're in the global scope, Pester's calls to Get-Command aren't going to find them. (The way that Should operators get loaded is also changing in v4.)

However, for now, you can inject your functions into Pester's scope after loading the module and it should work:

$pesterModule = Import-Module Pester -PassThru & $pesterModule { function PesterBeDate { }

etc

}

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/pester/Pester/issues/590#issuecomment-239977094, or mute the thread https://github.com/notifications/unsubscribe-auth/AAiaawbkEid_M7QpDyudtggN4qsPzZq2ks5qgRFOgaJpZM4JjSwc .

kamranayub commented 8 years ago

Here's an example of what I did for future reference: https://github.com/kamranayub/posh-openweathermap/blob/afe931f979dd675c279eecd89028f3fe7c4a5893/Run-Tests.ps1

ryangribble commented 7 years ago

(The way that Should operators get loaded is also changing in v4.)

Hey guys, is there any information about how these have changed in v4? Ive been unsuccesful in located instructions on how to do custom Should extensions in the new version?

dlwyatt commented 7 years ago

Didn't write instructions yet, will get to that this weekend. However, you can look at https://github.com/pester/Pester/blob/master/Functions/Assertions/Be.ps1 for guidance. Three main changes:

The names of your functions don't matter anymore. You can add aliases for your operator with the -Alias parameter on Add-AssertionOperator. If your operator can handle entire arrays, you can add the -SupportsArrayInput switch, and anything that gets piped to Should will be passed in as an array to a single call of your operator in the $ActualValue parameter. If you don't use -SupportsArrayInput, then piped in elements to Should will get tested one at a time, same as how everything worked in v3.

dlwyatt commented 7 years ago

Actually, Be.ps1 is probably one of the more complex files. Here's a simple one: https://github.com/pester/Pester/blob/master/Functions/Assertions/BeGreaterThan.ps1

ryangribble commented 7 years ago

Thanks for the info @dlwyatt

It doesn't seem like Add-AssetionOperator is an exported command though? Running version 4.0.3

PS C:\Users\ryan.gribble> Get-Command -Module Pester -Name Add-*

<nothing>

PS C:\Users\ryan.gribble> Get-Command -Module Pester

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        AfterAll                                           4.0.3      pester
Function        AfterEach                                          4.0.3      pester
Function        AfterEachFeature                                   4.0.3      pester
Function        AfterEachScenario                                  4.0.3      pester
Function        Assert-MockCalled                                  4.0.3      pester
Function        Assert-VerifiableMocks                             4.0.3      pester
Function        BeforeAll                                          4.0.3      pester
Function        BeforeEach                                         4.0.3      pester
Function        BeforeEachFeature                                  4.0.3      pester
Function        BeforeEachScenario                                 4.0.3      pester
Function        Context                                            4.0.3      pester
Function        Describe                                           4.0.3      pester
Function        Find-GherkinStep                                   4.0.3      pester
Function        Get-MockDynamicParameters                          4.0.3      pester
Function        Get-TestDriveItem                                  4.0.3      pester
Function        GherkinStep                                        4.0.3      pester
Function        In                                                 4.0.3      pester
Function        InModuleScope                                      4.0.3      pester
Function        Invoke-Gherkin                                     4.0.3      pester
Function        Invoke-GherkinStep                                 4.0.3      pester
Function        Invoke-Mock                                        4.0.3      pester
Function        Invoke-Pester                                      4.0.3      pester
Function        It                                                 4.0.3      pester
Function        Mock                                               4.0.3      pester
Function        New-Fixture                                        4.0.3      pester
Function        New-MockObject                                     4.0.3      pester
Function        New-PesterOption                                   4.0.3      pester
Function        SafeGetCommand                                     4.0.3      pester
Function        Set-DynamicParameterVariables                      4.0.3      pester
Function        Set-TestInconclusive                               4.0.3      pester
Function        Setup                                              4.0.3      pester
Function        Should                                             4.0.3      pester
dlwyatt commented 7 years ago

That's correct, though there's a PR open right now to change that.

stuartleeks commented 7 years ago

I've just installed 4.0.4 to try to update a couple of libraries with custom assertions and am hitting this issue. I can't see Add-AssertionOperator

nohwnd commented 7 years ago

765 merged need to release new version

stuartleeks commented 7 years ago

Thanks @nohwnd. Do you have any idea of timelines for a new release now that this is merged?

it-praktyk commented 7 years ago

@stuartleeks , please read #793.

stuartleeks commented 7 years ago

Thanks @it-praktyk, I read through #793 when I was trying to track down my issue. I guess the questions I was really asking is whether there will be a release ahead of a final v4 release (as there still seems to be a list of issues before then) :-)

nohwnd commented 7 years ago

@stuartleeks it's released as 4.0.5. Closing this issue, feel free to reopen if you need more help.

stuartleeks commented 7 years ago

Awesome, thanks @nohwnd !