3

If you wanted to say, 1 should equal 1, and if it doesn't then break, what would be the most eloquent way to do this in powershell with pester avoid code duplication?

Eg

{1 | should be 1} else {break}

rather than

1 | should be 1
if (1 -ne 1) {break}
  • That... doesn't really make sense. If breaking instead of returning 1 is a valid response, then `Should Be 1` is not a good test for your thing – Mathias R. Jessen Aug 05 '20 at 13:32
  • 1
    I think maybe they are asking if there's a good way to stop Pester from executing any further tests if a particular test fails. – Mark Wragg Aug 05 '20 at 13:43
  • @MarkWragg ahh, `break` from the test suite, that makes more sense :) – Mathias R. Jessen Aug 05 '20 at 13:45
  • Perhaps I picked a bad example. I just mean, if an assertion fails, and you want to run some commands in the event of failure, is it possible to tack this onto a failed pester assertion? –  Aug 05 '20 at 13:55

3 Answers3

3

There's no built in functionality for breaking the test execution when a failure occurs at the moment, but it has been discussed here: https://github.com/pester/Pester/issues/360 with some wrokarounds such as this one:

BeforeEach {
    $FailedCount =  InModuleScope -ModuleName Pester { $Pester.FailedCount }

    if ($FailedCount -gt 0) {
        Set-ItResult -Skipped -Because 'previous test failed'
    }
}

Another option might be to break your tests up into several different Pester scripts. Then you could have some high level or initial tests that you check for success on first and if they have not all passed then you skip execution of the remaining test scripts.

For example:

$Failed = (Invoke-Pester -Path First.tests.ps1 -PassThru).FailedCount

If ($Failed -eq 0) {
    Invoke-Pester -Path Second.tests.ps1
    ..
}
Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
1

There is built-in functionality for this now

https://github.com/pester/Pester/pull/2023

$pesterConfig = New-PesterConfiguration
$pesterConfig.Run.Container = @((New-PesterContainer -Path C:\Users\Armaan\Documents\powershell-dev\sample.Tests.ps1), (New-PesterContainer -Path C:\Users\Armaan\Documents\powershell-dev\sample2.Tests.ps1))
$pesterConfig.Output.Verbosity = "Detailed"

$pesterConfig.Run.SkipRemainingOnFailure = 'Run'
Invoke-Pester -Configuration $pesterConfig
Quinn Favo
  • 78
  • 7
  • This works great, you can even limit the skipped tests to a group of tests by setting `$pesterConfig.Run.SkipRemainingOnFailure = 'Block'`. E. g. if you group some tests by using `Context {}`, it will only skip the remaining tests of this block, but still executes more tests on a higher level. – zett42 May 17 '23 at 13:04
0

I landed here looking for a way to make one test have a dependency on another.

#Dependency in Pester v3.4.0
Describe 'testing pester' {
   $dep = @{}
   context 'when in here' {
      It 'when the dependent test fails' {
         $dep.aSpecificDependency = 'failed'

         'a' | should be 'b'

         $dep.aSpecificDependency = 'passed'
      }
      
      It 'then this is inconclusive' {
         if($dep.ContainsKey('aSpecificDependency') -and $dep.aSpecificDependency -ne 'passed'){
            Set-TestInconclusive -Message 'aSpecificDependency did not pass.'
         }
         'a' | should be 'a'
      }

      It 'and this should run fine' {
         if($dep.ContainsKey('aDifferentDependency') -and $dep.aDifferentDependency -ne 'passed'){
            Set-TestInconclusive -Message 'aDifferentDependency did not pass.'
         }
         'a' | should be 'a'
      }
      
      if($dep.ContainsKey('aSpecificDependency') -and $dep.aSpecificDependency -ne 'passed'){
         return
      }

      It 'stops before running this one' {
         'a' | should be 'a'
      }
   }

   context 'when looking at another section' {
      It 'goes fine' {
         'a' | should be 'a'
      }
   }

   if($dep.ContainsKey('aSpecificDependency') -and $dep.aSpecificDependency -ne 'passed'){
      return
   }
   context 'when looking at another section' {
      It 'or you could stop on that too' {
         'a' | should be 'a'
      }
   }
}
Gregor y
  • 1,762
  • 15
  • 22