diff --git a/Functions/TestsRunningInCleanRunspace.Tests.ps1 b/Functions/TestsRunningInCleanRunspace.Tests.ps1 index bb8d6d92a..ceaee866e 100644 --- a/Functions/TestsRunningInCleanRunspace.Tests.ps1 +++ b/Functions/TestsRunningInCleanRunspace.Tests.ps1 @@ -1,16 +1,26 @@ -function Invoke-PesterInJob ($ScriptBlock) +function Invoke-PesterInJob ($ScriptBlock, [switch] $GenerateNUnitReport) { - #TODO: there must be a safer way to determine this while I am in describe - $PesterPath = Get-Module -Name Pester | Select -First 1 -ExpandProperty Path + $PesterPath = Get-Module Pester | Select-Object -First 1 -ExpandProperty Path $job = Start-Job { - param ($PesterPath, $TestDrive, $ScriptBlock) + param ($PesterPath, $TestDrive, $ScriptBlock, $GenerateNUnitReport) Import-Module $PesterPath -Force | Out-Null $ScriptBlock | Set-Content $TestDrive\Temp.Tests.ps1 | Out-Null - Invoke-Pester -PassThru -Path $TestDrive + $params = @{ + PassThru = $true + Path = $TestDrive + } + + if ($GenerateNUnitReport) + { + $params['OutputFile'] = "$TestDrive\Temp.Tests.xml" + $params['OutputFormat'] = 'NUnitXml' + } - } -ArgumentList $PesterPath, $TestDrive, $ScriptBlock + Invoke-Pester @params + + } -ArgumentList $PesterPath, $TestDrive, $ScriptBlock, $GenerateNUnitReport $job | Wait-Job | Out-Null #not using Recieve-Job to ignore any output to Host @@ -84,6 +94,26 @@ Describe "Tests running in clean runspace" { $result.TotalCount | Should Be 4 } + + It 'Produces valid NUnit output when syntax errors occur in test scripts' { + $invalidScript = ' + Describe "Something" { + It "Works" { + $true | Should Be $true + } + # Deliberately missing closing brace to trigger syntax error + ' + + $result = Invoke-PesterInJob -ScriptBlock $invalidScript -GenerateNUnitReport + + $result.FailedCount | Should Be 1 + $result.TotalCount | Should Be 1 + 'TestDrive:\Temp.Tests.xml' | Should Exist + + $xml = [xml](Get-Content TestDrive:\Temp.Tests.xml) + + $xml.'test-results'.'test-suite'.results.'test-suite'.name | Should Not BeNullOrEmpty + } } Describe 'Guarantee It fail on setup or teardown fail (running in clean runspace)' { diff --git a/Pester.psm1 b/Pester.psm1 index 18076b57f..074b1b0f8 100644 --- a/Pester.psm1 +++ b/Pester.psm1 @@ -289,6 +289,12 @@ about_pester { $firstStackTraceLine = $_.ScriptStackTrace -split '\r?\n' | & $script:SafeCommands['Select-Object'] -First 1 $pester.AddTestResult("Error occurred in test script '$($testScript.Path)'", "Failed", $null, $_.Exception.Message, $firstStackTraceLine, $null, $null, $_) + + # This is a hack to ensure that XML output is valid for now. The test-suite names come from the Describe attribute of the TestResult + # objects, and a blank name is invalid NUnit XML. This will go away when we promote test scripts to have their own test-suite nodes, + # planned for v4.0 + $pester.TestResult[-1].Describe = "Error in $($testScript.Path)" + $pester.TestResult[-1] | Write-PesterResult } }