github pester/Pester 4.2.0

latest releases: 6.0.0-alpha5, 6.0.0-alpha4, 6.0.0-alpha3...
6 years ago

This release focuses primarily on making assertions better, even thought there are other small fixes and updates.

What's new since 4.1.1:

New assertions

  • There are now Should -BeLessThan and Should -BeLessOrEqual assertions to complement the existing BeGreaterThan and BeGreaterOrEqual assertions.

  • Should -Contain assertion was added that operates on collections, the same way the -contains operator operates on them.

    1,2,3 | Should -Contain 1
  • Should -HaveCount was added that counts items in a collection
1,2,3 | Should -HaveCount 3
  • Should -BeTrue and Should -BeFalse were added to assert on truthy and falsy values
$true | Should -BeTrue
1 | Should -BeTrue

$false | Should -BeFalse
0 | Should -BeFalse

Assertion improvements

  • Every built in assertion allows you to specify -Because parameter, to give more meaning to your tests.
function Get-Group { $null }
$groups = 1..10 | Get-Group -Size 3
$groups | Should -HaveCount 4 
    `-Because "because 10 items are split into three groups with 3 members and one extra group with 1 member"

Which fails with:

Expected a collection with size {4}, 
because 10 items are split into three groups with 3 members and one extra group with 1 member, 
but got collection with size {1} [].
  • Should -Throw has -ExceptionType parameter that matches the exception type and any of its subtypes.
function a () { throw [System.ArgumentNullException]"argument is null" }

# this passes because, ArgumentNullException inherits from ArgumentException
{ a } | Should -Throw -ExceptionType ([ArgumentException])

# this fails, because ArgumentNullExcption does not inherit from InvalidOperationException
{ a } | Should -Throw -ExceptionType ([InvalidOperationException])
  • Should -Throw has -PassThru parameter that returns the caputed exception to output. This is useful for catching a complex exception and further inspecting it.
$err = { 
    throw (New-Object System.InvalidOperationException "Something wrong happened",
            (New-Object DivideByZeroException "you divided by zero")
          )
 } | Should -Throw -PassThru
$err.Exception.InnerException.Message | Should -Be "you divided by zero"
  • Should -BeOfType is now aliased to -HaveType
1 | Should -HaveType [int]

Breaking changes:

  • Should -Not -Throw will fail if any exception is thrown. The filters applied on Should -Not -Throw do not apply, because when an exception is thrown, it automatically means that the test should not pass.

Don't miss a new Pester release

NewReleases is sending notifications on new releases.