Skip to content

Instantly share code, notes, and snippets.

@stefanstranger
Last active September 10, 2024 12:33
Show Gist options
  • Save stefanstranger/839044c5c5c146cb97678933e9a001cc to your computer and use it in GitHub Desktop.
Save stefanstranger/839044c5c5c146cb97678933e9a001cc to your computer and use it in GitHub Desktop.
Run Pester Tests parallel
# Example code of running multiple Pester test in parallel and merging the result into one NUnitReport Test Report file
#region Run Pester Test scripts in parallel
$job = Get-ChildItem -Path "./tests" -Filter "Demo*"
| ForEach-Object -Parallel {
Invoke-Pester -Path $_ -PassThru
} -ThrottleLimit 10 -AsJob
$Results = ($job | Wait-Job | Receive-Job -Keep)
#endregion
#region Create new Pester Object with merged parallel Pester Tests Results.
$Pester = [pester.Run]::new()
$results | ForEach-Object {
$testresult = $_
$pester.Containers += $testresult.Containers
$pester.DiscoveryDuration += $testresult.DiscoveryDuration
$pester.Duration += $testresult.Duration
$pester.Executed += $testresult.Executed
$pester.Failed += $testresult.Failed
$pester.FailedBlocks += $testresult.FailedBlocks
$pester.FailedBlocksCount += $testresult.FailedBlocksCount
$pester.FailedContainers += $testresult.FailedContainers
$pester.FailedContainersCount += $testresult.FailedContainersCount
$pester.FailedCount += $testresult.FailedCount
$pester.FrameworkDuration += $testresult.FrameworkDuration
$pester.NotRun += $testresult.NotRun
$pester.NotRunCount += $testresult.NotRunCount
$pester.Passed += $testresult.Passed
$pester.PassedCount += $testresult.PassedCount
$pester.Result += $testresult.Result
$pester.Skipped += $testresult.Skipped
$pester.SkippedCount += $testresult.SkippedCount
$pester.Tests += $testresult.Tests
$pester.TotalCount += $testresult.TotalCount
$pester.UserDuration += $testresult.UserDuration
}
#endregion
#region Export a Pester result-object to an NUnit-compatible XML-report
$pester | Export-NUnitReport -Path TestResultsFinal.xml
#endregion
@msl0
Copy link

msl0 commented Sep 10, 2024

@stefanstranger I appreciate your solution. I would like to ask if you know how to print the $pester object result so that the whole result is displayed together and not separately
Current output:

Starting discovery in 1 files.
Discovery found 1 tests in 12ms.
Running tests.
[+] ...\test1.Tests.ps1 5.13s (5.02s|98ms)
Tests completed in 5.13s
Tests Passed: 1, Failed: 0, Skipped: 0, Inconclusive: 0, NotRun: 0

Starting discovery in 1 files.
Discovery found 1 tests in 12ms.
Running tests.
[+] ...\test2.Tests.ps1 5.12s (5s|109ms)
Tests completed in 5.12s
Tests Passed: 1, Failed: 0, Skipped: 0, Inconclusive: 0, NotRun: 0

Expected output:

Starting discovery in 2 files.
Discovery found 2 tests in 19ms.
Running tests.
[+] ...\test1.Tests.ps1 5.07s (5.01s|55ms)
[+] ...\test2.Tests.ps 5.04s (5.01s|26ms)
Tests completed in 5.12s
Tests Passed: 2, Failed: 0, Skipped: 0, Inconclusive: 0, NotRun: 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment