Skip to content

Commit

Permalink
Merge pull request pester#467 from dlwyatt/ScriptSyntaxErrors
Browse files Browse the repository at this point in the history
Generate valid NUnit XML for script syntax errors
  • Loading branch information
dlwyatt committed Feb 6, 2016
2 parents a8b5449 + 1943cdc commit 226b3bc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
42 changes: 36 additions & 6 deletions Functions/TestsRunningInCleanRunspace.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)' {
Expand Down
6 changes: 6 additions & 0 deletions Pester.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit 226b3bc

Please sign in to comment.