pester / Pester

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

New Should Parameter: -And which would behave like -PassThru (and have -PassThru as alias) #1906

Closed JustinGrote closed 2 months ago

JustinGrote commented 3 years ago

Summary of the feature request

A common flow I have when doing multiple "Should" in a test looks like:

$myresult = Invoke-MyCommand
$myresult | Should -BeOfType [String]
$myresult | Should -Be 'myresult'

I would like to simply chain these statements together, especially using Powershell 7 Kusto-style pipeline syntax.

How should it work? (optional)

A new parameter -And or -Passthru which simply emits the object back to the pipeline, enabling this style of test declaration:

Invoke-MyCommand
| Should -BeOfType [String] -And
| Should -Be 'MyResult'

Or alternatively:

Invoke-MyCommand
| Should -BeOfType [String] -PassThru
| Should -Be 'MyResult'

The parameter should be implemented as -And but have an alias to -PassThru since that is a common parameter usage familiar to Powershell users.

Willing to attempt a PR if labeled up-for-grabs.

nohwnd commented 3 years ago

Have you seen the continue on error option for should?

nohwnd commented 3 years ago

https://pester.dev/docs/assertions/should-command#collect-all-should-failures

JustinGrote commented 3 years ago

@nohwnd The issue is not using multiple should in a function, that works fine with the first example.

The issue is to be able to use the truncated syntax shown in the second example, right now I cannot do that because Should doesn't output anything, so I have to keep operating against an existing resultant variable.

nohwnd commented 3 years ago

I think there is a proposal for this somewhere here, probably one of the issues in Better Should milestone. I was considering it, and am not against implementing it, but it was a bit limited, because only one assertion can fail. Granted you could use both this chaining and the error collecting, but when I tried it, it turned out to be rarely practical because you don't have an easy way of selecting a property from a bigger object so you will end up using variables anyway.

fflaten commented 2 years ago

I think there is a proposal for this somewhere here, probably one of the issues in Better Should milestone.

Probably this https://github.com/pester/Pester/issues/1319#issuecomment-499069698 Stumbled over it when removing an invalid example in the Should-help. The example was the one used to illustrate the compound assertion-concept 🙂

nohwnd commented 2 months ago

Won't implement, I reverted similar functionality in the linked issue. I think soft-assertions are better solution to this.