Skip to content

Commit

Permalink
Added ability to pass in xml outputfile. Updated format using test-su…
Browse files Browse the repository at this point in the history
…ite, environment etc
  • Loading branch information
mbergmann authored and manojlds committed Jul 30, 2012
1 parent 40ba767 commit bb87d34
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 76 deletions.
3 changes: 3 additions & 0 deletions Functions/It.ps1
Expand Up @@ -21,7 +21,9 @@ function It($name, [ScriptBlock] $test)
stackTrace = ""
success = $false
};

Start-PesterConsoleTranscript

$testTime = Measure-Command {
try{
temp
Expand All @@ -40,6 +42,7 @@ function It($name, [ScriptBlock] $test)
Write-Host -ForegroundColor red $error_margin$error_margin"at line: $failure_line_number in $test_file"
}
}

$testResult.time = $testTime.TotalSeconds
$results.Tests += $testResult;

Expand Down
97 changes: 55 additions & 42 deletions Functions/TestResults.Tests.ps1
@@ -1,37 +1,6 @@
function Test-Xml($inputFile, $schemaFile, $Namespace = "http://www.w3.org/2001/XMLSchema" ) {
BEGIN {
$failCount = 0
$failureMessages = ""
$fileName = ""
}

PROCESS {
$fileName = $inputFile.FullName
$readerSettings = New-Object -TypeName System.Xml.XmlReaderSettings
$readerSettings.ValidationType = [System.Xml.ValidationType]::Schema
$readerSettings.ValidationFlags = [System.Xml.Schema.XmlSchemaValidationFlags]::ProcessInlineSchema -bor
[System.Xml.Schema.XmlSchemaValidationFlags]::ProcessSchemaLocation -bor
[System.Xml.Schema.XmlSchemaValidationFlags]::ReportValidationWarnings
$readerSettings.Schemas.Add($Namespace, $SchemaFile) | Out-Null
$readerSettings.add_ValidationEventHandler(
{
$failureMessages = $failureMessages + [System.Environment]::NewLine + $fileName + " - " + $_.Message
$failCount = $failCount + 1
});
$reader = [System.Xml.XmlReader]::Create($inputFile, $readerSettings)
while ($reader.Read()) { }
$reader.Close()
}

END {
$failureMessages
"$failCount validation errors were found"
}
}

Describe "Write test results" {
Describe "Write nunit test results" {
Setup -Dir "Results"

$nunitSchema = (Convert-Path ".\nunit_schema_2.5.xsd")
It "should write a successful test result" {
$testResults = @{}
$testResults.Tests = @(
Expand All @@ -46,11 +15,10 @@ Describe "Write test results" {
$testFile = "$TestDrive\Results\Tests.xml"
Write-NunitTestReport $testResults $testFile
$xmlResult = [xml] (Get-Content $testFile)
$xmlTestCase = $xmlResult.'test-results'.'test-case'
$xmlTestCase = $xmlResult.'test-results'.'test-suite'.'results'.'test-case'
$xmlTestCase.name.Should.Be("Successful testcase")
$xmlTestCase.result.Should.Be("Success")
$xmlTestCase.time.Should.Be("1.0")
Test-Xml $testFile ".\nunit_schema_2.5.xsd"
}

It "should write a failed test result" {
Expand All @@ -68,33 +36,78 @@ Describe "Write test results" {
$testFile = "$TestDrive\Results\Tests.xml"
Write-NunitTestReport $testResults $testFile
$xmlResult = [xml] (Get-Content $testFile)
$xmlTestCase = $xmlResult.'test-results'.'test-case'
$xmlTestCase = $xmlResult.'test-results'.'test-suite'.'results'.'test-case'
$xmlTestCase.name.Should.Be("Failed testcase")
$xmlTestCase.result.Should.Be("Failure")
$xmlTestCase.time.Should.Be("2.0")
$xmlTestCase.failure.message.Should.Be('Assert failed: "Expected: Test. But was: Testing"');
$xmlTestCase.failure.'stack-trace'.should.be('at line: 28 in C:\Pester\Result.Tests.ps1')

Test-Xml $testFile ".\nunit_schema_2.5.xsd"
}

It "should write the test summary" {
$testResults = @{}
$testResults.Tests = @( "" );
$testResults.FailedTests = @("", "")
$testResults.runDate = "01-01-2012"
$testResults.runTime = "00:10:10"

$testFile = "$TestDrive\Results\Tests.xml"
Write-NunitTestReport $testResults $testFile
$xmlResult = [xml] (Get-Content $testFile)
$xmlTestResult = $xmlResult.'test-results'
$xmlTestResult.total.Should.Be(1)
$xmlTestResult.failures.Should.Be(2)
$xmlTestResult.date.Should.Be("01-01-2012")
$xmlTestResult.time.Should.Be("00:10:10")
$xmlTestResult.date.Should.Be($true)
$xmlTestResult.time.Should.Be($true)

Test-Xml $testFile ".\nunit_schema_2.5.xsd"
}

it "should write the test-suite information" {
$testResults = @{}
$testResults.Tests = @(
@{
name = "Successful testcase"
time = "1.0"
success = $true
},
@{
name = "Successful testcase"
time = "1.1"
success = $true
};
);
$testFile = "$TestDrive\Results\Tests.xml"
Write-NunitTestReport $testResults $testFile
$xmlResult = [xml] (Get-Content $testFile)

$xmlTestResult = $xmlResult.'test-results'.'test-suite'
$xmlTestResult.type.Should.Be("Powershell")
$xmlTestResult.name.Should.Be("Pester")
$xmlTestResult.result.Should.Be("Success")
$xmlTestResult.success.Should.Be("True")
$xmlTestResult.time.Should.Be(2.1)
}


# it "should write the environment information" {
# $testResults = @{}
# $testResults.Tests = @( "" );
# $testResults.Environment = @{
# osVersion = "Windows 7";
# platform = "Windows"
# runPath = "C:\workspace\Pester\"
# machineName = "Computer1"
# userName = "foo"
# };
# $testFile = "$TestDrive\Results\Tests.xml"
# Write-NunitTestReport $testResults $testFile
# $xmlResult = [xml] (Get-Content $testFile)

# $xmlEnvironment = $xmlResult.'test-results'.'environment'
# $xmlEnvironment.'os-Version'.Should.Be("Windows 7")
# $xmlEnvironment.platform.Should.Be("Windows")
# $xmlEnvironment.runPath.Should.Be("C:\workspace\Pester\")
# $xmlEnvironment.machineName.Should.Be("Computer1")
# $xmlEnvironment.userName.Should.Be("foo")
# }
}

83 changes: 57 additions & 26 deletions Functions/TestResults.ps1
Expand Up @@ -8,8 +8,7 @@ function Get-GlobalTestResults {
$testResults.FailedTests = @();
$testResults.TestCount = 0
$testResults.TestDepth = 0
$testResults.runDate = (Get-Date -format "dd-mm-yyyy")
$testResults.runTime = (Get-Date -format "hh:mm:ss")


$Global:TestResults = $testResults
return $Global:TestResults
Expand All @@ -21,42 +20,74 @@ function Reset-GlobalTestResults {

function Write-TestReport {
$results = $Global:TestResults
Write-NunitTestReport $results
Write-Host Tests completed
Write-Host Passed: $($results.TestCount - $results.FailedTests.length) Failed: $($results.FailedTests.length)
}

function Write-NunitTestReport($results, $outputFile = "TestResults.xml") {
$results.total = $results.Tests.length
$results.failures = $results.FailedTests.length
function Write-NunitTestReport($results, $outputFile) {
if($results -eq $null) {
return;
}
$report = @{
runDate = (Get-Date -format "yyyy-MM-dd")
runTime = (Get-Date -format "HH:mm:ss")
total = 0;
failuers = 0;
success = "True"
resultMessage = "Success"
totalTime = "0.0"
}

$report.total = $results.Tests.length
if($results.FailedTests) {
$report.failures = $results.FailedTests.length
$report.success = "False"
$report.resultMessage = "Failure"
}
$report.testCases = (Get-TestResults $results.Tests)
$report.totalTime = (Get-TotalTestTime $results.Tests)

$resultTemplate = (Get-Content 'templates\TestResults.template.xml');
$xmlResult = Invoke-Template $resultTemplate $report
$xmlResult | Set-Content $outputFile -force
}

$thisScript = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$successemplate = Get-Content '$thisScript\..\templates\TestCaseSuccess.template.xml'
$failureTemplate = Get-Content '$thisScript\..\templates\TestCaseFailure.template.xml'

$testCaseXmls = $results.Tests | %{
function Get-TotalTestTime($tests) {
$totalTime = 0;
$tests | %{
$totalTime += $_.time
}
return $totalTime;
}

function Get-TestResults($tests) {
$successemplate = Get-Content 'templates\TestCaseSuccess.template.xml'
$failureTemplate = Get-Content 'templates\TestCaseFailure.template.xml'
$testCaseXmls = $tests | %{
$result = $_
$replace_args = ($result.keys | %{
"-replace '@@$_@@', '$($result.$_)'"
}) -join " "
if($result.success) {
$expr = "`$successemplate $replace_args"
Invoke-Template $successemplate $result
}
else {
$expr = "`$failureTemplate $replace_args"
Invoke-Template $failureTemplate $result
}

iex $expr
}
return $testCaseXmls
}

function Get-ReplacementArgs($template, $data) {
$replacements = ($data.keys | %{
if($template -match "@@$_@@") {
"-replace '@@$_@@', '$($data.$_)'"
}
})
$replacements -join " "
return $replacements
}

$resultTemplate = (Get-Content '$thisScript\..\templates\TestResults.template.xml');
$replacements = @("total", "failures", "rundate", "runtime")
$replace_args = ($results.keys | ?{ $replacements -contains $_} | %{
"-replace '@@$_@@', '$($results.$_)'"
}) -join " "
$replace_args += " -replace '@@results@@', '$testCaseXmls'"
$expr = "`$resultTemplate $replace_args"
iex $expr | Set-Content $outputFile -force
function Invoke-Template($template, $data) {
$replacments = Get-ReplacementArgs $template $data
return Invoke-Expression "`$template $replacments"
}

function Exit-WithCode {
Expand Down
6 changes: 5 additions & 1 deletion Pester.psm1
Expand Up @@ -2,7 +2,7 @@ Resolve-Path $PSScriptRoot\Functions\*.ps1 |
? { -not ($_.ProviderPath.Contains(".Tests.")) } |
% { . $_.ProviderPath }

function Invoke-Pester($relative_path = ".", [switch] $EnableExit) {
function Invoke-Pester($relative_path = ".", [switch] $EnableExit, $OutputXml = $null) {
Reset-GlobalTestResults
. "$PSScriptRoot\ObjectAdaptations\PesterFailure.ps1"
Update-TypeData -pre "$PSScriptRoot\ObjectAdaptations\types.ps1xml" -ErrorAction SilentlyContinue
Expand All @@ -15,6 +15,10 @@ function Invoke-Pester($relative_path = ".", [switch] $EnableExit) {
% { & $_.PSPath }

Write-TestReport

if($OutputXml) {
Write-NunitTestReport (Get-GlobalTestResults) $OutputXml
}
if ($EnableExit) { Exit-WithCode }
}

Expand Down
4 changes: 0 additions & 4 deletions TestResults.xml

This file was deleted.

2 changes: 1 addition & 1 deletion bin/pester.bat
Expand Up @@ -8,7 +8,7 @@ if '%1'=='/help' goto usage
if '%1'=='help' goto usage
if '%1'=='new-fixture' goto newfixture

@PowerShell -NonInteractive -NoProfile -ExecutionPolicy unrestricted -Command "& Import-Module '%DIR%..\Pester.psm1'; & { Invoke-Pester -EnableExit; exit $LastExitCode}"
@PowerShell -NonInteractive -NoProfile -ExecutionPolicy unrestricted -Command "& Import-Module '%DIR%..\Pester.psm1'; & { Invoke-Pester -OutputXml Test.Xml -EnableExit; exit $LastExitCode}"

goto finish
:newfixture
Expand Down
10 changes: 8 additions & 2 deletions templates/TestResults.template.xml
@@ -1,4 +1,10 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<test-results name="Pester" total="@@total@@" errors="0" failures="@@failures@@" not-run="0" inconclusive="0" ignored="0" skipped="0" invalid="0" date="@@rundate@@" time="@@runtime@@">
@@results@@
<test-results xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="nunit_schema_2.5.xsd" name="Pester" total="@@total@@" errors="0" failures="@@failures@@" not-run="0" inconclusive="0" ignored="0" skipped="0" invalid="0" date="@@rundate@@" time="@@runtime@@">
<environment nunit-version="2.5.8.0" clr-version="2.0.50727.1433" os-version="@@osVersion@@" platform="@@platform@@" cwd="@@runPath@@" machine-name="@@machineName@@" user="@@userName@@" user-domain="" />
<culture-info current-culture="en-US" current-uiculture="en-US" />
<test-suite type="Powershell" name="Pester" executed="True" result="@@resultMessage@@" success="@@success@@" time="@@totalTime@@" asserts="0">
<results>
@@testCases@@
</results>
</test-suite>
</test-results>

0 comments on commit bb87d34

Please sign in to comment.