-
-
Notifications
You must be signed in to change notification settings - Fork 476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Checking a proper errors #673
Comments
More extensibility for built in assertions is coming in pester 4, which I am currently piloting with sqldbatools, so hopefully it will be released soon. BUT there is nothing preventing you from writing your own assertions. So I would recomend that you do not use that template, it is introducing untested logic into your test scripts. I can see right away that you have potential bug in there. A much better option is to write your own assertion. And write tests for it! Here is a quick prototype to get you started: function ShouldThrowException
{
param (
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[scriptblock] $ScriptBlock, [type] $ExpectedExceptionType = [exception], $ExpectedExceptionMessage, $ExpectedFullyQualifiedErrorId)
$exceptionThrown = $false
try {
$null = &$ScriptBlock
}
catch {
$exceptionThrown = $true
# you might need to work here a bit to extract the error info correctly, there are some edge cases, feel free to look in pester internals and reuse that code
$ex = $_.Exception
if ($ex -isnot $ExpectedExceptionType)
{ throw "Expected an exception of type $($ExpectedExceptionType.Name) to be thrown, but exception of type '$($ex.GetType().Name) was thrown instead" }
#... etc, rest of your criteria in the same manner
}
if (-not $exceptionThrown)
{
throw "Expected an exception to be thrown, but no exception was thrown"
}
}
describe "my assert" {
it "fails because nothing was thrown" {
{"10"} | ShouldThrowException
}
it "passes because an exception was thrown" {
{throw "this code fails"} | ShouldThrowException
}
it "fails because exception of a wrong type was thrown" {
{throw "this code fails"} | ShouldThrowException -ExpectedExceptionType InvalidOperationException
}
} When writing tests for this, it's best to keep the throwing and the internal logic separeted, write a function that returns a result object { passed: $false; Errors: "Expected an exception to be thrown, but no exception was thrown" } and then another one that will just take the result and throw the apropriate exception if it fails. That way you will have much easier time testing. |
@nohwnd Thanks for great comments!
|
The syntax in v4 did not change much, the positional parameters became dynamic parameters on the should command, so all you need to do is add dashes in front of your throws and nots. The old syntax is still possible though so this can be gradual.
The last two switches are not implemented yet, but the exception type will match exceptions of the given type and any derived types (ArgumentException will match ArgumentException as well as ArgumentNullException). One more thing. Prompted by this discussions, other discussions and my own frustration with Pester assertions I started writing a set of assertions that aim to be more consistent, easily discoverable and offer richer functionality than the built-in Pester assertions. Those will be Pester v3 and v4 compatible, and can offer you the extended functionality that Pester v4 assertions can have. I will release those as soon as I feel I have something worth using, to see what the community thinks (so in few days). That would then allow you to import my assertion module and write tests like this: It "Get-Item on a nonexisting file should have error PathNotFound" {
{ Get-Item "ThisFileCannotPossiblyExist" } |
Should-Throw -FullyQualifiedErrorId "PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand"
} Pretty much the same way you would in Pester v4, but in all Pester versions. Then if people see my assertion module as a valuable tool, and help me stabilize it, I can make it part of Pester v5. |
@nohwnd Thanks for great news! |
@iSazonov Aha okay, then disregard this, Pester v3 nor v4 is compatible with that fork. That is the next step. |
@iSazonov, we are close not to release Pester v.4. Can you test the newest version? |
…xtension parameter in New-TemporaryFile as part of PR PowerShell#4612 In Pester v3 the 'Should Throw' assertion is quite limited, see here: pester/Pester#673 Therefore the example in the guide here was followed: https://github.com/PowerShell/PowerShell/blob/68bcd4b5284bb169c52a570afbfdd1bb70d0de10/docs/testing-guidelines/WritingPesterTests.md
@vors is already opened PowerShell/PowerShell#4618 and we see some problems. |
Due to lack of additional information, the issue is closed now. Please feel free to open when you feel that your question or request was not handled correctly. |
In Powerhell Core project, we often use the following template for checking a proper errors:
It would be useful to implement something like:
or
or
The text was updated successfully, but these errors were encountered: