Skip to content

Commit 8e40775

Browse files
Refactor and modernize Pester tests for multiple commands
Updated Pester tests for several dbatools commands to use Pester 5 syntax, added param blocks for test configuration, improved parameter validation, and enhanced test structure and assertions. These changes improve test maintainability, consistency, and reliability across the test suite.
1 parent ee4e62c commit 8e40775

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2501
-1077
lines changed

tests/Get-DbaBuild.Tests.ps1

Lines changed: 138 additions & 83 deletions
Large diffs are not rendered by default.

tests/Get-DbaClientAlias.Tests.ps1

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,47 @@
1-
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
2+
param(
3+
$ModuleName = "dbatools",
4+
$CommandName = "Get-DbaClientAlias",
5+
$PSDefaultParameterValues = $TestConfig.Defaults
6+
)
7+
28
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
39
$global:TestConfig = Get-TestConfig
410

5-
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
11+
Describe $CommandName -Tag UnitTests {
612
Context "Validate parameters" {
7-
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
8-
[object[]]$knownParameters = 'ComputerName', 'Credential', 'EnableException'
9-
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
13+
BeforeAll {
14+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
15+
$expectedParameters = $TestConfig.CommonParameters
16+
$expectedParameters += @(
17+
"ComputerName",
18+
"Credential",
19+
"EnableException"
20+
)
21+
}
22+
1023
It "Should only contain our specific parameters" {
11-
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
24+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
1225
}
1326
}
1427
}
1528

16-
Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
29+
Describe $CommandName -Tag IntegrationTests {
1730
BeforeAll {
18-
$newalias = New-DbaClientAlias -ServerName sql2016 -Alias dbatoolscialias -Verbose:$false
31+
$newAlias = New-DbaClientAlias -ServerName sql2016 -Alias dbatoolscialias -Verbose:$false
1932
}
33+
2034
AfterAll {
21-
$newalias | Remove-DbaClientAlias
35+
$newAlias | Remove-DbaClientAlias -ErrorAction SilentlyContinue
2236
}
2337

2438
Context "gets the alias" {
25-
$results = Get-DbaClientAlias
39+
BeforeAll {
40+
$results = Get-DbaClientAlias
41+
}
42+
2643
It "returns accurate information" {
27-
$results.AliasName -contains 'dbatoolscialias' | Should Be $true
44+
$results.AliasName -contains "dbatoolscialias" | Should -Be $true
2845
}
2946
}
3047
}

tests/Get-DbaClientProtocol.Tests.ps1

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
1-
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
2+
param(
3+
$ModuleName = "dbatools",
4+
$CommandName = "Get-DbaClientProtocol",
5+
$PSDefaultParameterValues = $TestConfig.Defaults
6+
)
7+
28
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
39
$global:TestConfig = Get-TestConfig
410

5-
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
6-
Context "Validate parameters" {
7-
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
8-
[object[]]$knownParameters = 'ComputerName', 'Credential', 'EnableException'
9-
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
10-
It "Should only contain our specific parameters" {
11-
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
11+
Describe $CommandName -Tag UnitTests {
12+
Context "Parameter validation" {
13+
BeforeAll {
14+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
15+
$expectedParameters = $TestConfig.CommonParameters
16+
$expectedParameters += @(
17+
"ComputerName",
18+
"Credential",
19+
"EnableException"
20+
)
21+
}
22+
23+
It "Should have the expected parameters" {
24+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
1225
}
1326
}
1427
}
1528

16-
Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
29+
Describe $CommandName -Tag IntegrationTests {
1730
Context "Get some client protocols" {
18-
$results = Get-DbaClientProtocol
31+
BeforeAll {
32+
$results = @(Get-DbaClientProtocol)
33+
}
34+
1935
It "Should return some protocols" {
20-
$results.Count | Should BeGreaterThan 1
21-
$results | Where-Object { $_.ProtocolDisplayName -eq 'TCP/IP' } | Should Not Be $null
36+
$results.Status.Count | Should -BeGreaterThan 1
37+
$results | Where-Object ProtocolDisplayName -eq "TCP/IP" | Should -Not -BeNullOrEmpty
2238
}
2339
}
2440
}

tests/Get-DbaCmConnection.Tests.ps1

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,54 @@
1-
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
2-
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
3-
$global:TestConfig = Get-TestConfig
4-
5-
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
6-
Context "Validate parameters" {
7-
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
8-
[object[]]$knownParameters = 'ComputerName', 'UserName', 'EnableException'
9-
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
10-
It "Should only contain our specific parameters" {
11-
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
2+
param(
3+
$ModuleName = "dbatools",
4+
$CommandName = "Get-DbaCmConnection",
5+
$PSDefaultParameterValues = $TestConfig.Defaults
6+
)
7+
8+
Describe $CommandName -Tag UnitTests {
9+
Context "Parameter validation" {
10+
BeforeAll {
11+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
12+
$expectedParameters = $TestConfig.CommonParameters
13+
$expectedParameters += @(
14+
"ComputerName",
15+
"UserName",
16+
"EnableException"
17+
)
18+
}
19+
20+
It "Should have the expected parameters" {
21+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
1222
}
1323
}
1424
}
1525

16-
Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
26+
Describe $CommandName -Tag IntegrationTests {
1727
BeforeAll {
1828
New-DbaCmConnection -ComputerName $env:COMPUTERNAME
1929
}
30+
2031
AfterAll {
21-
Remove-DbaCmConnection -ComputerName $env:COMPUTERNAME -Confirm:$False
32+
Remove-DbaCmConnection -ComputerName $env:COMPUTERNAME -Confirm:$false
2233
}
34+
2335
Context "Returns DbaCmConnection" {
24-
$Results = Get-DbaCMConnection -ComputerName $env:COMPUTERNAME
36+
BeforeAll {
37+
$cmConnectionResults = Get-DbaCmConnection -ComputerName $env:COMPUTERNAME
38+
}
39+
2540
It "Results are not Empty" {
26-
$Results | should not be $null
41+
$cmConnectionResults | Should -Not -BeNullOrEmpty
2742
}
2843
}
44+
2945
Context "Returns DbaCmConnection for User" {
30-
$Results = Get-DbaCMConnection -ComputerName $env:COMPUTERNAME -UserName *
46+
BeforeAll {
47+
$userConnectionResults = Get-DbaCmConnection -ComputerName $env:COMPUTERNAME -UserName *
48+
}
49+
3150
It "Results are not Empty" {
32-
$Results | should not be $null
51+
$userConnectionResults | Should -Not -BeNullOrEmpty
3352
}
3453
}
3554
}

tests/Get-DbaCmObject.Tests.ps1

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,41 @@
1-
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
2+
param(
3+
$ModuleName = "dbatools",
4+
$CommandName = "Get-DbaCmObject",
5+
$PSDefaultParameterValues = $TestConfig.Defaults
6+
)
7+
28
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
39
$global:TestConfig = Get-TestConfig
410

5-
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
6-
Context "Validate parameters" {
7-
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
8-
[object[]]$knownParameters = 'ClassName', 'Query', 'ComputerName', 'Credential', 'Namespace', 'DoNotUse', 'Force', 'SilentlyContinue', 'EnableException'
9-
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
10-
It "Should only contain our specific parameters" {
11-
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
11+
Describe $CommandName -Tag UnitTests {
12+
Context "Parameter validation" {
13+
BeforeAll {
14+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
15+
$expectedParameters = $TestConfig.CommonParameters
16+
$expectedParameters += @(
17+
"ClassName",
18+
"Query",
19+
"ComputerName",
20+
"Credential",
21+
"Namespace",
22+
"DoNotUse",
23+
"Force",
24+
"SilentlyContinue",
25+
"EnableException"
26+
)
27+
}
28+
29+
It "Should have the expected parameters" {
30+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
1231
}
1332
}
1433
}
1534

16-
Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
17-
Context "returns proper information" {
18-
It "returns a bias that's an int" {
19-
(Get-DbaCmObject -ClassName Win32_TimeZone).Bias -is [int]
35+
Describe $CommandName -Tag IntegrationTests {
36+
Context "Returns proper information" {
37+
It "Returns a bias that's an int" {
38+
(Get-DbaCmObject -ClassName Win32_TimeZone).Bias | Should -BeOfType [int]
2039
}
2140
}
2241
}
Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,61 @@
1-
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
2+
param(
3+
$ModuleName = "dbatools",
4+
$CommandName = "Get-DbaComputerCertificate",
5+
$PSDefaultParameterValues = $TestConfig.Defaults
6+
)
7+
28
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
39
$global:TestConfig = Get-TestConfig
410

5-
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
6-
Context "Validate parameters" {
7-
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
8-
[object[]]$knownParameters = 'ComputerName', 'Credential', 'Store', 'Folder', 'Path', 'Thumbprint', 'EnableException', 'Type'
9-
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
10-
It "Should only contain our specific parameters" {
11-
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
11+
Describe $CommandName -Tag UnitTests {
12+
Context "Parameter validation" {
13+
BeforeAll {
14+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
15+
$expectedParameters = $TestConfig.CommonParameters
16+
$expectedParameters += @(
17+
"ComputerName",
18+
"Credential",
19+
"Store",
20+
"Folder",
21+
"Path",
22+
"Thumbprint",
23+
"EnableException",
24+
"Type"
25+
)
26+
}
27+
28+
It "Should have the expected parameters" {
29+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
1230
}
1331
}
1432
}
1533

16-
Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
34+
Describe $CommandName -Tag IntegrationTests {
1735
Context "Can get a certificate" {
1836
BeforeAll {
1937
$null = Add-DbaComputerCertificate -Path "$($TestConfig.appveyorlabrepo)\certificates\localhost.crt" -Confirm:$false
2038
$thumbprint = "29C469578D6C6211076A09CEE5C5797EEA0C2713"
39+
40+
# Get all certificates once for testing
41+
$allCertificates = Get-DbaComputerCertificate
42+
$specificCertificate = Get-DbaComputerCertificate -Thumbprint $thumbprint
2143
}
44+
2245
AfterAll {
23-
Remove-DbaComputerCertificate -Thumbprint $thumbprint -Confirm:$false
46+
Remove-DbaComputerCertificate -Thumbprint $thumbprint -Confirm:$false -ErrorAction SilentlyContinue
2447
}
2548

26-
$cert = Get-DbaComputerCertificate -Thumbprint $thumbprint
27-
2849
It "returns a single certificate with a specific thumbprint" {
29-
$cert.Thumbprint | Should Be $thumbprint
50+
$specificCertificate.Thumbprint | Should -Be $thumbprint
3051
}
3152

32-
$cert = Get-DbaComputerCertificate
33-
3453
It "returns all certificates and at least one has the specified thumbprint" {
35-
"$($cert.Thumbprint)" -match $thumbprint | Should Be $true
54+
"$($allCertificates.Thumbprint)" -match $thumbprint | Should -Be $true
3655
}
56+
3757
It "returns all certificates and at least one has the specified EnhancedKeyUsageList" {
38-
"$($cert.EnhancedKeyUsageList)" -match '1\.3\.6\.1\.5\.5\.7\.3\.1' | Should Be $true
58+
"$($allCertificates.EnhancedKeyUsageList)" -match "1\.3\.6\.1\.5\.5\.7\.3\.1" | Should -Be $true
3959
}
4060
}
41-
}
61+
}

0 commit comments

Comments
 (0)