This repository was archived by the owner on Feb 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathunit_test_template.ps1
183 lines (151 loc) · 6.15 KB
/
unit_test_template.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<#
.SYNOPSIS
Template for creating DSC Resource Unit Tests
.DESCRIPTION
To Use:
1. Copy to \Tests\Unit\ folder and rename <ResourceName>.tests.ps1
(e.g. MSFT_xFirewall.tests.ps1).
2. Customize TODO sections.
3. Delete all template comments (TODOs, etc.).
4. Remove any unused It-, Context-, BeforeAll-, AfterAll-,
BeforeEach- and AfterEach-blocks.
5. Remove this comment-based help.
.NOTES
There are multiple methods for writing unit tests. This template provides a few examples
which you are welcome to follow but depending on your resource, you may want to
design it differently. Read through our TestsGuidelines.md file for an intro on how to
write unit tests for DSC resources: https://github.com/PowerShell/DscResources/blob/master/TestsGuidelines.md
#>
#region HEADER
# TODO: Update to correct module name and resource name.
$script:dscModuleName = '<ModuleName>'
$script:dscResourceName = '<ResourceName>'
# Unit Test Template Version: 1.2.4
$script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
(-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
{
& git @('clone', 'https://github.com/PowerShell/DscResource.Tests.git', (Join-Path -Path $script:moduleRoot -ChildPath 'DscResource.Tests'))
}
Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResource.Tests' -ChildPath 'TestHelper.psm1')) -Force
# TODO: Insert the correct <ModuleName> and <ResourceName> for your resource
$TestEnvironment = Initialize-TestEnvironment `
-DSCModuleName $script:dscModuleName `
-DSCResourceName $script:dscResourceName `
-ResourceType 'Mof' `
-TestType Unit
#endregion HEADER
function Invoke-TestSetup
{
# TODO: Optional init code goes here...
}
function Invoke-TestCleanup
{
Restore-TestEnvironment -TestEnvironment $TestEnvironment
# TODO: Other Optional Cleanup Code Goes Here...
}
# Begin Testing
try
{
Invoke-TestSetup
InModuleScope $script:dscResourceName {
# TODO: Optionally create any variables here for use by your tests
# TODO: Complete the Describe blocks below and add more as needed.
# The most common method for unit testing is to test by function. For more information
# check out this introduction to writing unit tests in Pester:
# https://www.simple-talk.com/sysadmin/powershell/practical-powershell-unit-testing-getting-started/#eleventh
# You may also follow one of the patterns provided in the TestsGuidelines.md file:
# https://github.com/PowerShell/DscResources/blob/master/TestsGuidelines.md
Describe 'MSFT_<ResourceName>\Get-TargetResource' -Tag 'Get' {
BeforeAll {
# Per describe-block initialization
}
AfterAll {
# Per describe-block cleanup
}
BeforeEach {
# per-test-initialization
Mock -CommandName Get-Something
}
AfterEach {
# per-test-cleanup
}
Context 'When the system is in the desired state' {
BeforeAll {
# Per context-block initialization
}
AfterAll {
# Per context-block cleanup
}
BeforeEach {
# per test initialization
Mock -CommandName New-Thing -MockWith {
return 'New thing'
}
}
AfterEach {
# per test cleanup
}
<#
TODO: (Optional) If It-block description tends to be long,
consider adding nested Context-blocks ('When...'), e.g 'When
the configuration is absent', 'When the configuration should
be present' or 'When the current description is returned
as an empty string'.
#>
It 'Should ....test-description' {
# TODO: Update test-code
$getTargetResourceResult = Get-TargetResource -Parameter1 'Value1'
$getTargetResourceResult.Value1 | Should -Be 'Expected value'
Assert-MockCalled -CommandName Get-Something -Exactly -Times 1 -Scope It
Assert-MockCalled -CommandName New-Thing -Exactly -Times 1 -Scope It
}
It 'Should ....test-description' {
# test-code
}
}
Context 'When the system is not in the desired state' {
It 'Should ....test-description' {
# test-code
}
}
}
Describe 'MSFT_<ResourceName>\Set-TargetResource' -Tag 'Set' {
Context 'When the system is in the desired state' {
It 'Should ...test-description' {
# test-code
}
}
Context 'When the system is not in the desired state' {
It 'Should ....test-description' {
# test-code
}
}
}
Describe 'MSFT_<ResourceName>\Test-TargetResource' -Tag 'Test' {
Context 'When the system is in the desired state' {
It 'Should ...test-description' {
# test-code
}
}
Context 'When the system is not in the desired state' {
It 'Should ....test-description' {
# test-code
}
}
}
Describe 'MSFT_<ResourceName>\Get-HelperFunction' -Tag 'Helper' {
It 'Should ...test-description' {
# test-code
}
It 'Should ....test-description' {
# test-code
}
}
# TODO: add more Describe blocks as needed
}
}
finally
{
Invoke-TestCleanup
}