forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
It.ps1
142 lines (123 loc) · 3.88 KB
/
It.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
function It {
<#
.SYNOPSIS
Validates the results of a test inside of a Describe block.
.DESCRIPTION
The It function is intended to be used inside of a Describe
Block. If you are familiar with the AAA pattern
(Arrange-Act-Assert), this would be the appropriate location
for an assert. The convention is to assert a single
expectation for each It block. The code inside of the It block
should throw an exception if the expectation of the test is not
met and thus cause the test to fail. The name of the It block
should expressively state the expectation of the test.
In addition to using your own logic to test expectations and
throw exceptions, you may also use Pester's own helper functions
to assist in evaluating test results using the Should object.
.PARAMETER Name
An expressive phsae describing the expected test outcome.
.PARAMETER Test
The script block that should throw an exception if the
expectation of the test is not met.If you are following the
AAA pattern (Arrange-Act-Assert), this typically holds the
Assert.
.EXAMPLE
function Add-Numbers($a, $b) {
return $a + $b
}
Describe "Add-Numbers" {
It "adds positive numbers" {
$sum = Add-Numbers 2 3
$sum.should.be(5)
}
It "adds negative numbers" {
$sum = Add-Numbers (-2) (-2)
$sum.should.be((-4))
}
It "adds one negative number to positive number" {
$sum = Add-Numbers (-2) 2
$sum.should.be(0)
}
It "concatenates strings if given strings" {
$sum = Add-Numbers two three
$sum.should.be("twothree")
}
}
.LINK
Describe
Context
about_should
#>
param(
$name,
[ScriptBlock] $test
)
$pester.results = Get-GlobalTestResults
$pester.results.TestCount += 1
Setup-TestFunction
. $TestDrive\temp.ps1
$pester.ThisTest=$test
try{
[object]$test=(get-variable -name test -scope 1 -errorAction Stop).value
}
catch {
#throws if there is no parent test var
}
$pester.testTime = Measure-Command {
try{
temp
} catch {
$pester.results.FailedTestsCount += 1
$PesterException = $_
}
}
$pester.testResult = Get-PesterResult $pester.ThisTest $PesterException
$pester.results.CurrentDescribe.Tests += $pester.testResult
$pester.results.TotalTime += $pester.testTime.TotalSeconds
Write-PesterResult
}
function Setup-TestFunction {
@"
function temp {
$test
}
"@ | out-file $TestDrive\temp.ps1
}
function write-PesterResult{
$pester.margin = " " * $pester.results.TestDepth
$pester.error_margin = $pester.margin * 2
$pester.output = " $($pester.margin)$name"
$pester.humanSeconds = Get-HumanTime $pester.testTime.TotalSeconds
if($pester.testResult.success) {
"[+] $($pester.output) $($pester.humanSeconds)" | Write-Host -ForegroundColor green;
}
else {
"[-] $($pester.output) $($pester.humanSeconds)" | Write-Host -ForegroundColor red
Write-Host -ForegroundColor red $($pester.error_margin)$($pester.testResult.failureMessage)
Write-Host -ForegroundColor red $($pester.error_margin)$($pester.testResult.stackTrace)
}
}
function Get-PesterResult{
param([ScriptBlock] $test, $exception)
$testResult = @{
name = $name
time = 0
failureMessage = ""
stackTrace = ""
success = $false
};
if(!$exception){$testResult.success = $true}
else {
$testResult.failureMessage = $Exception.toString() -replace "Exception calling", "Assert failed on"
if($pester.ShouldExceptionLine) {
$line=$pester.ShouldExceptionLine
$pester.ShouldExceptionLine=$null
}
else {
$line=$exception.InvocationInfo.ScriptLineNumber
}
$failureLine = $test.StartPosition.StartLine + ($line-2)
$testResult.stackTrace = "at line: $failureLine in $($test.File)"
}
return $testResult
}