Reputation:
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}
Upvotes: 3
Views: 1649
Reputation: 2050
I landed here looking for a way to make one test have a dependency on another.
#Dependency in Pester v3.4.0, Windows-PowerShell v5.1
Describe 'testing pester' {
It 'Check for legacy Pester version' {@(Get-Module 'Pester')[0].version | should be '3.4.0'}
$dep = @{}
context 'when in here' {
It 'when the independent test fails' {
$dep.aSpecificDependency = 'failed'
'a' | should be 'b'
$dep.aSpecificDependency = 'passed'
}
It 'then the dependent test is inconclusive' {
if(-not $dep.ContainsKey('aSpecificDependency')){
Set-TestInconclusive -Message 'aSpecificDependency did not run.'
}elseif($dep.aSpecificDependency -ne 'passed'){
Set-TestInconclusive -Message 'aSpecificDependency did not pass.'
}
'a' | should be 'a'
}
It 'it is also inconclusive when the independent has not yet been performed.' {
if(-not $dep.ContainsKey('aDifferentDependency')){
Set-TestInconclusive -Message 'aDifferentDependency did not run.'
}elseif($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'
}
}
}
Upvotes: 0
Reputation: 86
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
Upvotes: 2
Reputation: 23355
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
..
}
Upvotes: 3