Skip to content

Commit

Permalink
Add a 'FailedTestsDetails' property to health reports
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuBuisson committed Jun 18, 2017
1 parent 8dcd7be commit 07ae506
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 26 deletions.
24 changes: 12 additions & 12 deletions Examples/HtmlReport.html
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ <h3 class="panel-title"><i class="fa fa-plus-square-o" aria-hidden="true">&nbsp;
</div>
</div>
<div class="col-sm-3 panelContainer">
<div class="panel panel-success">
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-check-square-o" aria-hidden="true">&nbsp;</i> Tests Pass Rate (%)</h3>
</div>
Expand Down Expand Up @@ -549,7 +549,7 @@ <h2 class="panel-title"> Per Function Information</h2>
<h3 class="panel-title"><i class="fa fa-list" aria-hidden="true">&nbsp;</i> Number of Tests</h3>
</div>
<div class="panel-body">
<h2>10</h2>
<h2>13</h2>
</div>
</div>
</div>
Expand All @@ -559,7 +559,7 @@ <h2>10</h2>
<h3 class="panel-title"><i class="fa fa-times" aria-hidden="true">&nbsp;</i> Number of Failed Tests</h3>
</div>
<div class="panel-body">
<h2>0</h2>
<h2>2</h2>
</div>
</div>
</div>
Expand All @@ -569,7 +569,7 @@ <h2>0</h2>
<h3 class="panel-title"><i class="fa fa-check-square-o" aria-hidden="true">&nbsp;</i> Number of Passed Tests</h3>
</div>
<div class="panel-body">
<h2>10</h2>
<h2>11</h2>
</div>
</div>
</div>
Expand All @@ -581,12 +581,12 @@ <h2>10</h2>
<h3 class="panel-title"><i class="fa fa-times" aria-hidden="true">&nbsp;</i> Number of Missed Commands</h3>
</div>
<div class="panel-body">
<h2>77</h2>
<h2>60</h2>
</div>
</div>
</div>
<div class="col-sm-3 panelContainer">
<div class="panel panel-success">
<div class="panel panel-danger">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-check-square-o" aria-hidden="true">&nbsp;</i> Tests Pass Rate (%)</h3>
</div>
Expand Down Expand Up @@ -633,8 +633,8 @@ <h2 class="panel-title"> Per Function Information</h2>
</tr>
<tr>
<td>Get-CoverageArray</td>
<td class="danger">0</td>
<td class="danger">18</td>
<td class="success">94.44</td>
<td class="success">1</td>
</tr>
<tr>
<td>Format-FileCoverage</td>
Expand Down Expand Up @@ -740,7 +740,7 @@ <h2 class="panel-title"> Per Function Information</h2>
labels: ["Pass","Fail"],
datasets: [
{
data: [10,0],
data: [11,2],
backgroundColor: ["#a3d48d","#d59595"],
hoverBackgroundColor: ["#a3d48d","#d59595"]
}]
Expand All @@ -755,7 +755,7 @@ <h2 class="panel-title"> Per Function Information</h2>
center: {
// This evaluates the max length of the text
maxText: '99.99%',
text: '100%',
text: '84.62%',
fontColor: '#a3d48d',
fontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
fontStyle: 'normal'
Expand All @@ -774,7 +774,7 @@ <h2 class="panel-title"> Per Function Information</h2>
labels: ["Covered","Missed"],
datasets: [
{
data: [21.43,78.57],
data: [38.78,61.22],
backgroundColor: ["#a3d48d","#d59595"],
hoverBackgroundColor: ["#a3d48d","#d59595"]
}]
Expand All @@ -789,7 +789,7 @@ <h2 class="panel-title"> Per Function Information</h2>
center: {
// This evaluates the max length of the text
maxText: '99.99%',
text: '21.43%',
text: '38.78%',
fontColor: '#d59595',
fontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
fontStyle: 'normal'
Expand Down
49 changes: 49 additions & 0 deletions PSCodeHealth/Private/New-FailedTestsInfo.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Function New-FailedTestsInfo {
<#
.SYNOPSIS
Creates one or more custom objects of the type : 'PSCodeHealth.Overall.FailedTestsInfo'.
.DESCRIPTION
Creates one or more custom objects of the type : 'PSCodeHealth.Overall.FailedTestsInfo'.
This outputs an object containing key information about each failed test. This information is used in the overall health report.
.PARAMETER TestsResult
To specify the Pester tests result object.
.EXAMPLE
PS C:\> New-FailedTestsInfo -TestsResult $TestsResult
Returns a new custom object of the type 'PSCodeHealth.Overall.FailedTestsInfo' for each failed test in the input $TestsResult.
.OUTPUTS
PSCodeHealth.Overall.FailedTestsInfo
.NOTES
#>
[CmdletBinding()]
[OutputType([PSCustomObject[]])]
Param (
[Parameter(Position=0, Mandatory)]
[PSCustomObject]$TestsResult
)
$FailedTests = $TestsResult.TestResult.Where({ -not $_.Passed })

Foreach ( $FailedTest in $FailedTests ) {

$SplitStackTrace = $FailedTest.StackTrace -split ':\s'
$File = ($SplitStackTrace[0] -split '\\')[-1]
$Line = ((($SplitStackTrace[1] -split '\n') | Where-Object { $_ -match 'line' }) -split '\s')[-1]

$ObjectProperties = [ordered]@{
'File' = $File
'Line' = $Line
'Describe' = $FailedTest.Describe
'TestName' = $FailedTest.Name
'ErrorMessage' = $FailedTest.FailureMessage
}

$CustomObject = New-Object -TypeName PSObject -Property $ObjectProperties
$CustomObject.psobject.TypeNames.Insert(0, 'PSCodeHealth.Overall.FailedTestsInfo')
$CustomObject
}
}
4 changes: 4 additions & 0 deletions PSCodeHealth/Private/New-PSCodeHealthReport.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Function New-PSCodeHealthReport {
To use an existing Pester tests result object for generating the following metrics :
- NumberOfTests
- NumberOfFailedTests
- FailedTestsDetails
- NumberOfPassedTests
- TestsPassRate (%)
- TestCoverage (%)
Expand Down Expand Up @@ -126,6 +127,8 @@ Function New-PSCodeHealthReport {
}
}

$FailedTestsDetails = If ($TestsResult.FailedCount -gt 0) { New-FailedTestsInfo -TestsResult $TestsResult } Else { $Null }

$ObjectProperties = [ordered]@{
'ReportTitle' = $ReportTitle
'ReportDate' = Get-Date -Format u
Expand All @@ -142,6 +145,7 @@ Function New-PSCodeHealthReport {
'FunctionsWithoutHelp' = ($FunctionHealthRecord | Where-Object { -not($_.ContainsHelp) } | Measure-Object).Count
'NumberOfTests' = If ( $TestsResult ) { $TestsResult.TotalCount } Else { 0 }
'NumberOfFailedTests' = If ( $TestsResult ) { $TestsResult.FailedCount } Else { 0 }
'FailedTestsDetails' = $FailedTestsDetails
'NumberOfPassedTests' = If ( $TestsResult ) { $TestsResult.PassedCount } Else { 0 }
'TestsPassRate' = If ($TestsResult.TotalCount) { [math]::Round(($TestsResult.PassedCount / $TestsResult.TotalCount) * 100, 2) } Else { 0 }
'TestCoverage' = $CodeCoveragePerCent
Expand Down
12 changes: 6 additions & 6 deletions Tests/Integration/Compliance.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,27 @@ Describe 'Test-PSCodeHealthCompliance' {
$ScriptAnalyzerFindingsTotal.Result | Should Be 'Pass'
}
It 'Should return correct value for the metric : NumberOfFailedTests' {
$NumberOfFailedTests.Value | Should Be 0
$NumberOfFailedTests.Value | Should Be 2
}
It 'Should return correct compliance result for the metric : NumberOfFailedTests' {
$NumberOfFailedTests.Result | Should Be 'Pass'
$NumberOfFailedTests.Result | Should Be 'Warning'
}
It 'Should return correct value for the metric : TestsPassRate' {
$TestsPassRate.Value | Should Be 100
$TestsPassRate.Value | Should Be 84.62
}
It 'Should return correct compliance result for the metric : TestsPassRate' {
$TestsPassRate.Result | Should Be 'Pass'
$TestsPassRate.Result | Should Be 'Fail'
}
It 'Should return correct value for the metric : TestCoverage' {
($TestCoverage | Where-Object SettingsGroup -eq 'OverallMetrics').Value |
Should Be 21.43
Should Be 38.78
}
It 'Should return correct compliance result for the metric : TestCoverage' {
($TestCoverage | Where-Object SettingsGroup -eq 'OverallMetrics').Result |
Should Be 'Fail'
}
It 'Should return correct value for the metric : CommandsMissedTotal' {
$CommandsMissedTotal.Value | Should Be 77
$CommandsMissedTotal.Value | Should Be 60
}
It 'Should return correct compliance result for the metric : CommandsMissedTotal' {
$CommandsMissedTotal.Result | Should Be 'Pass'
Expand Down
44 changes: 36 additions & 8 deletions Tests/Integration/HealthReport.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ Describe 'Invoke-PSCodeHealth' {
$GetCoverageArray.ScriptAnalyzerFindings | Should Be 0
}
It 'Should return correct TestCoverage metric for the function Get-CoverageArray' {
$GetCoverageArray.TestCoverage | Should Be 0
$GetCoverageArray.TestCoverage | Should Be 94.44
}
It 'Should return correct CommandsMissed metric for the function Get-CoverageArray' {
$GetCoverageArray.CommandsMissed | Should Be 18
$GetCoverageArray.CommandsMissed | Should Be 1
}
It 'Should return correct Complexity metric for the function Get-CoverageArray' {
$GetCoverageArray.Complexity | Should Be 5
Expand Down Expand Up @@ -137,22 +137,50 @@ Describe 'Invoke-PSCodeHealth' {
$Result.FunctionsWithoutHelp | Should Be 9
}
It 'The health report should have the expected NumberOfTests property' {
$Result.NumberOfTests | Should Be 10
$Result.NumberOfTests | Should Be 13
}
It 'The health report should have the expected NumberOfFailedTests property' {
$Result.NumberOfFailedTests | Should Be 0
$Result.NumberOfFailedTests | Should Be 2
}
It 'The health report should have 2 objects in its "FailedTestsDetails" property' {
$Result.FailedTestsDetails.Count | Should Be 2
}
It 'Should return an object with the expected property "File"' {
Foreach ( $Result in $Result.FailedTestsDetails ) {
$Result.File | Should Be 'Coveralls.Tests.ps1'
}
}
It 'Should return an object with the expected property "Line"' {
Foreach ( $Result in $Result.FailedTestsDetails ) {
$Result.Line | Should BeIn @('97','100')
}
}
It 'Should return an object with the expected property "Describe"' {
Foreach ( $Result in $Result.FailedTestsDetails ) {
$Result.Describe | Should Be 'Get-CoverageArray'
}
}
It 'Should return an object with the expected property "TestName"' {
Foreach ( $Result in $Result.FailedTestsDetails ) {
$Result.TestName | Should BeIn @('Should return 2 objects with the value 1','Should return 0 objects with the value 0')
}
}
It 'Should return an object with the expected property "ErrorMessage"' {
Foreach ( $Result in $Result.FailedTestsDetails ) {
$Result.ErrorMessage | Should BeLike 'Expected: * was*'
}
}
It 'The health report should have the expected NumberOfPassedTests property' {
$Result.NumberOfPassedTests | Should Be 10
$Result.NumberOfPassedTests | Should Be 11
}
It 'The health report should have the expected TestsPassRate property' {
$Result.TestsPassRate | Should Be 100
$Result.TestsPassRate | Should Be 84.62
}
It 'The health report should have the expected TestCoverage property' {
$Result.TestCoverage | Should Be 21.43
$Result.TestCoverage | Should Be 38.78
}
It 'The health report should have the expected CommandsMissedTotal property' {
$Result.CommandsMissedTotal | Should Be 77
$Result.CommandsMissedTotal | Should Be 60
}
It 'The health report should have the expected ComplexityAverage property' {
$Result.ComplexityAverage | Should Be 2
Expand Down
24 changes: 24 additions & 0 deletions Tests/TestData/MockObjects.json
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,30 @@
"FailureMessage": "MockFailureMessage",
"StackTrace": "MockStackTrace"
}
},
{
"2FailedTests": [
{
"TestResult": {
"Passed": false,
"Describe": "MockDescribe",
"Name": "MockTestName",
"StackTrace": [", C:\\GitHub\\PSCodeHealth\\Tests\\TestData\\coveralls\\Coveralls.Tests.ps1: line 97",
"97: ($Result | Where-Object { $_ -eq 1 }).Count | Should Be 2"],
"FailureMessage": "MockFailureMessage"
}
},
{
"TestResult": {
"Passed": false,
"Describe": "MockDescribe2",
"Name": "MockTestName2",
"StackTrace": [", C:\\GitHub\\PSCodeHealth\\Tests\\TestData\\coveralls\\Coveralls.Tests.ps1: line 101",
"101: ($Result | Where-Object { $_ -eq 0 }).Count | Should BeNullOrEmpty"],
"FailureMessage": "MockFailureMessage2"
}
}
]
}
],
"New-FunctionHealthRecord": [
Expand Down
19 changes: 19 additions & 0 deletions Tests/TestData/coveralls/Coveralls.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ Describe "Get-CommandsForFile" {
}
}

Describe 'Get-CoverageArray' {
Context 'There are 3 lines matching a coverage array line number' {

$LinesInFile = @('This is the first line','This is the second line', 'This is the third line')
Mock Get-Content { $LinesInFile }
$CoverageArray = @( @{Line = 1; CoverageResult = 1},@{Line = 2; CoverageResult = 1},@{Line = 3; CoverageResult = 0} )
$Result = Get-CoverageArray -CoverageResultArray $CoverageArray -File 'Any'

It 'Should return 3 objects' {
$Result.Count | Should Be 3
}
It 'Should return 2 objects with the value 1' {
($Result | Where-Object { $_ -eq 1 }).Count | Should Be 2
}
It 'Should return 0 objects with the value 0' {
($Result | Where-Object { $_ -eq 0 }).Count | Should BeNullOrEmpty
}
}
}
Describe "Format-FileCoverage" {
Context "Receiving proper input" {
It "outputs the expected information" {
Expand Down
50 changes: 50 additions & 0 deletions Tests/Unit/Private/New-FailedTestsInfo.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
$ModuleName = 'PSCodeHealth'
Import-Module "$PSScriptRoot\..\..\..\$ModuleName\$($ModuleName).psd1" -Force

Describe 'New-FailedTestsInfo' {
InModuleScope $ModuleName {

$Mocks = ConvertFrom-Json (Get-Content -Path "$($PSScriptRoot)\..\..\TestData\MockObjects.json" -Raw )
$MockTestsResult = $Mocks.'Invoke-Pester'.'2FailedTests'.Where({ $_ })

Context 'The specified test result contains 2 failed tests' {

$Results = New-FailedTestsInfo -TestsResult $MockTestsResult

It 'Should return objects of the type [PSCodeHealth.Overall.FailedTestsInfo]' {
Foreach ( $Result in $Results ) {
$Result | Should BeOfType [PSCustomObject]
($Result | Get-Member).TypeName[0] | Should Be 'PSCodeHealth.Overall.FailedTestsInfo'
}
}
It 'Should return 2 objects' {
$Results.Count | Should Be 2
}
It 'Should return an object with the expected property "File"' {
Foreach ( $Result in $Results ) {
$Result.File | Should Be 'Coveralls.Tests.ps1'
}
}
It 'Should return an object with the expected property "Line"' {
Foreach ( $Result in $Results ) {
$Result.Line | Should BeIn @('97','101')
}
}
It 'Should return an object with the expected property "Describe"' {
Foreach ( $Result in $Results ) {
$Result.Describe | Should BeIn @('MockDescribe','MockDescribe2')
}
}
It 'Should return an object with the expected property "TestName"' {
Foreach ( $Result in $Results ) {
$Result.TestName | Should BeIn @('MockTestName','MockTestName2')
}
}
It 'Should return an object with the expected property "ErrorMessage"' {
Foreach ( $Result in $Results ) {
$Result.ErrorMessage | Should BeIn @('MockFailureMessage','MockFailureMessage2')
}
}
}
}
}

0 comments on commit 07ae506

Please sign in to comment.