Created
March 14, 2017 17:27
-
-
Save nohwnd/b9adac90257cbb82093a4bcd75078824 to your computer and use it in GitHub Desktop.
No logic in tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ex1 | |
# say not to this one | |
$configuration:Servers | foreach { | |
It "A test" { | |
# code | |
} | |
} | |
# use this instead | |
It "A test" -TestCases @( | |
@{ Server = "server1"}, | |
. . . | |
) { | |
# code | |
} | |
} | |
# if you DO need to generate the list of test cases dynamically, do it in a function that is tested. | |
# Pester currently does not fail when you provide 0 testcases, so look out for lack of data that allows | |
# your tests to pass | |
#(this is also common problem with foreach in tests, | |
#no items in collection -> no it block runs -> no fails -> passing tests that did not test anything) | |
#ex2 | |
# say no to this one | |
if ($PSVersionTable.PsVersion.Major -ge 5){ | |
It "A test running only on powershell 5="{ | |
# code | |
} | |
} | |
# write this (and test it!) instead | |
Invoke-OnPowerShell5OrLaterOnly { | |
It "A test" { | |
# code | |
} | |
} | |
# feel free to come up with a better name :)) | |
function Invoke-OnPowerShell5OrLaterOnly ([ScriptBlock]$ScriptBlock) { | |
if ($PSVersionTable.PsVersion.Major -ge 5) { | |
& $ScriptBlock | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment