3
3
## Core Requirements
4
4
``` powershell
5
5
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"}
6
- param($ModuleName = "dbatools")
7
- $global:TestConfig = Get-TestConfig
6
+ param(
7
+ $ModuleName = "dbatools",
8
+ $PSDefaultParameterValues = ($TestConfig = Get-TestConfig).Defaults
9
+ )
8
10
```
9
- These three lines must start every test file.
11
+ These lines must start every test file.
10
12
11
13
## Test Structure
12
14
@@ -45,20 +47,6 @@ Describe "Get-DbaDatabase" -Tag "IntegrationTests" {
45
47
}
46
48
```
47
49
48
- ## TestCases
49
- Use the ` -ForEach ` parameter in ` It ` blocks for multiple test cases:
50
-
51
- ``` powershell
52
- It "Should calculate correctly" -ForEach @(
53
- @{ Input = 1; Expected = 2 }
54
- @{ Input = 2; Expected = 4 }
55
- @{ Input = 3; Expected = 6 }
56
- ) {
57
- $result = Get-Double -Number $Input
58
- $result | Should -Be $Expected
59
- }
60
- ```
61
-
62
50
## Style Guidelines
63
51
- Use double quotes for strings (we're a SQL Server module)
64
52
- Array declarations should be on multiple lines:
@@ -72,21 +60,65 @@ $array = @(
72
60
- Skip conditions must evaluate to ` $true ` or ` $false ` , not strings
73
61
- Use ` $global: ` instead of ` $script: ` for test configuration variables when required for Pester v5 scoping
74
62
- Avoid script blocks in Where-Object when possible:
75
-
76
63
``` powershell
77
64
# Good - direct property comparison
78
65
$master = $databases | Where-Object Name -eq "master"
79
66
$systemDbs = $databases | Where-Object Name -in "master", "model", "msdb", "tempdb"
80
67
81
68
# Required - script block for Parameters.Keys
82
- $actualParameters = $command.Parameters.Keys | Where-Object { $PSItem -notin "WhatIf", "Confirm" }
69
+ $newParameters = $command.Parameters.Values.Name | Where-Object { $PSItem -notin "WhatIf", "Confirm" }
83
70
```
84
71
85
- ## DO NOT
86
- - DO NOT use ` $MyInvocation.MyCommand.Name ` to get command names
87
- - DO NOT use the old ` knownParameters ` validation approach
88
- - DO NOT include loose code outside of proper test blocks
89
- - DO NOT remove comments like "#TestConfig.instance3" or "#$TestConfig.instance2 for appveyor"
72
+ ### Parameter & Variable Naming Rules
73
+ - Use direct parameters for 1-2 parameters
74
+ - Use ` $splat<Purpose> ` for 3+ parameters (never plain ` $splat ` )
75
+
76
+ ``` powershell
77
+ # Direct parameters
78
+ $ag = Get-DbaLogin -SqlInstance $instance -Login $loginName
79
+
80
+ # Splat with purpose suffix
81
+ $splatPrimary = @{
82
+ Primary = $TestConfig.instance3
83
+ Name = $primaryAgName
84
+ ClusterType = "None"
85
+ FailoverMode = "Manual"
86
+ Certificate = "dbatoolsci_AGCert"
87
+ Confirm = $false
88
+ }
89
+ $primaryAg = New-DbaAvailabilityGroup @splatPrimary
90
+ ```
91
+
92
+ ### Unique names across scopes
93
+
94
+ - Use unique, descriptive variable names across scopes to avoid collisions
95
+ - Play particlar attention to variable names in the BeforeAll
96
+
97
+ ``` powershell
98
+ Describe "Add-DbaAgReplica" -Tag "IntegrationTests" {
99
+ BeforeAll {
100
+ $primaryAgName = "dbatoolsci_agroup"
101
+ $splatPrimary = @{
102
+ Primary = $TestConfig.instance3
103
+ Name = $primaryAgName
104
+ ...
105
+ }
106
+ $ag = New-DbaAvailabilityGroup @splatPrimary
107
+ }
108
+
109
+ Context "When adding AG replicas" {
110
+ BeforeAll {
111
+ $replicaAgName = "dbatoolsci_add_replicagroup"
112
+ $splatRepAg = @{
113
+ Primary = $TestConfig.instance3
114
+ Name = $replicaAgName
115
+ ...
116
+ }
117
+ $replicaAg = New-DbaAvailabilityGroup @splatRepAg
118
+ }
119
+ }
120
+ }
121
+ ```
90
122
91
123
## Examples
92
124
@@ -97,22 +129,23 @@ Describe "Get-DbaDatabase" -Tag "UnitTests" {
97
129
Context "Parameter validation" {
98
130
BeforeAll {
99
131
$command = Get-Command Get-DbaDatabase
100
- $expectedParameters = $TestConfig.CommonParameters
101
-
102
- $expectedParameters += @(
132
+ $expected = $TestConfig.CommonParameters
133
+ $expected += @(
103
134
"SqlInstance",
104
135
"SqlCredential",
105
- "Database"
136
+ "Database",
137
+ "Confirm",
138
+ "WhatIf"
106
139
)
107
140
}
108
141
109
- It "Should have exactly the expected parameters" {
110
- $actualParameters = $command.Parameters.Keys | Where-Object { $PSItem -notin "WhatIf", "Confirm" }
111
- Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $actualParameters | Should -BeNullOrEmpty
142
+ It "Has parameter: <_>" -ForEach $expected {
143
+ $command | Should -HaveParameter $PSItem
112
144
}
113
145
114
- It "Has parameter: <_>" -ForEach $expectedParameters {
115
- $command | Should -HaveParameter $PSItem
146
+ It "Should have exactly the number of expected parameters ($($expected.Count))" {
147
+ $hasparms = $command.Parameters.Values.Name
148
+ Compare-Object -ReferenceObject $expected -DifferenceObject $hasparms | Should -BeNullOrEmpty
116
149
}
117
150
}
118
151
}
@@ -139,35 +172,11 @@ Describe "Get-DbaDatabase" -Tag "IntegrationTests" {
139
172
}
140
173
```
141
174
142
- ### Parameter & Variable Naming Rules
143
- 1 . Use direct parameters for 1-3 parameters
144
- 2 . Use ` $splat<Purpose> ` for 4+ parameters (never plain ` $splat ` )
145
- 3 . Use unique, descriptive variable names across scopes
146
-
147
- ``` powershell
148
- # Direct parameters
149
- $ag = New-DbaLogin -SqlInstance $instance -Login $loginName -Password $password
175
+ ## Additional instructions
150
176
151
- # Splat with purpose suffix
152
- $splatPrimary = @{
153
- Primary = $TestConfig.instance3
154
- Name = $primaryAgName # Descriptive variable name
155
- ClusterType = "None"
156
- FailoverMode = "Manual"
157
- Certificate = "dbatoolsci_AGCert"
158
- Confirm = $false
159
- }
160
- $primaryAg = New-DbaAvailabilityGroup @splatPrimary
161
-
162
- # Unique names across scopes
163
- Describe "New-DbaAvailabilityGroup" {
164
- BeforeAll {
165
- $primaryAgName = "primaryAG"
166
- }
167
- Context "Adding replica" {
168
- BeforeAll {
169
- $replicaAgName = "replicaAG"
170
- }
171
- }
172
- }
173
- ```
177
+ - DO NOT use ` $MyInvocation.MyCommand.Name ` to get command names
178
+ - DO NOT use the old ` knownParameters ` validation approach
179
+ - DO NOT include loose code outside of proper test blocks
180
+ - DO NOT remove comments like "#TestConfig.instance3" or "#$TestConfig.instance2 for appveyor"
181
+ - DO NOT use $_ DO use $PSItem instead
182
+ - Parameter validation is ALWAYS tagged as a Unit Test
0 commit comments