This repository has been archived by the owner on Feb 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 205
/
unit_template.ps1
118 lines (89 loc) · 3.4 KB
/
unit_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
<#
.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.)
.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.
#>
#region HEADER
# Unit Test Template Version: 1.2.0
$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 (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force
# TODO: Insert the correct <ModuleName> and <ResourceName> for your resource
$TestEnvironment = Initialize-TestEnvironment `
-DSCModuleName '<ModuleName>' `
-DSCResourceName '<ResourceName>' `
-TestType Unit
#endregion HEADER
# Begin Testing
try
{
Invoke-TestSetup
InModuleScope '<ResourceName>' {
# 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 '<Test-name>' {
BeforeEach {
# per-test-initialization
}
AfterEach {
# per-test-cleanup
}
Context 'Context-description' {
BeforeEach {
# per-test-initialization
}
AfterEach {
# per-test-cleanup
}
It 'Should...test-description' {
# test-code
}
. . .
It 'Should...test-description' {
# test-code
}
}
Context 'Context-description' {
It 'Should ....test-description' {
# test-code
}
}
}
Describe '<Test-name>' {
Context '<Context-description>' {
It 'Should ...test-description' {
# test-code
}
}
}
# TODO: add more Describe blocks as needed
}
}
finally
{
Invoke-TestCleanup
}
function Invoke-TestSetup {
# TODO: Optional init code goes here...
}
function Invoke-TestCleanup {
Restore-TestEnvironment -TestEnvironment $TestEnvironment
# TODO: Other Optional Cleanup Code Goes Here...
}