Skip to content

Instantly share code, notes, and snippets.

@DanielSundberg
Created December 5, 2018 09:04
Show Gist options
  • Save DanielSundberg/919aa09af577dfc778aae018defb15f0 to your computer and use it in GitHub Desktop.
Save DanielSundberg/919aa09af577dfc778aae018defb15f0 to your computer and use it in GitHub Desktop.
Powershell Run a Command And Count Errors
# Run command and count errors
# I use this to gather statistics about flaky tests:
#
# > RunCommandAndCountErrors "python -mpytest -ktest_some_flaky_test .\test_tests.py" 10
# ...
# ...
# Test 10, nofErrors: 3
# Nof test runs: 10, nof errors: 3
# > _
function RunCommandAndCountErrors($cmd, $reps)
{
$nofErrors = 0
$cnt = 0
for ($i = 1; $i -le $reps; $i++) {
Invoke-Expression $cmd
$ec = $LASTEXITCODE
write-host "Last exit code: $ec"
$cnt++
if ($ec -ne 0) {
$nofErrors++
write-host "Increasing nofErrors: $nofErrors"
}
write-host "Test $cnt of $reps, nofErrors: $nofErrors"
}
write-host "Nof test runs: $cnt, nof errors: $nofErrors"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment