Skip to content

Commit 9931688

Browse files
committed
feat(测试): 添加详细测试脚本并优化网络测试
- 在scripts.json中添加test:detailed脚本用于详细输出测试结果 - 在Pester配置中添加Filter模块排除Slow标签测试 - 优化网络测试脚本,减少重复查询端口操作 - 为网络测试添加Slow标签并调整测试超时时间
1 parent a1b3804 commit 9931688

3 files changed

Lines changed: 40 additions & 35 deletions

File tree

PesterConfiguration.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ $config = @{
3131
MaxThreads = 4
3232
}
3333
}
34+
35+
# Filter 模块: 定义筛选规则
36+
Filter = @{
37+
ExcludeTag = @('Slow')
38+
}
3439
CodeCoverage = @{
3540
Enabled = $true
3641
Path = "./psutils/modules/*.psm1"
@@ -45,6 +50,10 @@ $config = @{
4550
)
4651

4752
}
53+
Output = @{
54+
# 使用详细输出,方便查看哪些测试被跳过了
55+
# Verbosity = 'Detailed'
56+
}
4857
TestResult = @{
4958
Enabled = $true
5059
OutputPath = "./testResults.xml"

psutils/tests/network.Tests.ps1

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
BeforeAll {
1010
# 导入被测试的模块
1111
Import-Module "$PSScriptRoot\..\modules\network.psm1" -Force
12+
13+
# 预先获取一个被占用的端口,避免在每个测试中重复查询
14+
$script:occupiedPort = Get-NetTCPConnection | Where-Object { $_.State -eq "Listen" } | Select-Object -First 1 -ExpandProperty LocalPort
1215
}
1316

14-
Describe "Test-PortOccupation 函数测试" {
17+
Describe "Test-PortOccupation 函数测试" -Tag 'Network', 'Slow' {
1518
Context "端口占用检测" {
1619
It "应该能够检测到已占用的端口" {
17-
# 测试一个通常被占用的端口(如果存在)
18-
# 这里使用一个动态方法来找到一个被占用的端口
19-
$occupiedPort = Get-NetTCPConnection | Where-Object { $_.State -eq "Listen" } | Select-Object -First 1 -ExpandProperty LocalPort
20-
21-
if ($occupiedPort) {
22-
$result = Test-PortOccupation -Port $occupiedPort
20+
if ($script:occupiedPort) {
21+
$result = Test-PortOccupation -Port $script:occupiedPort
2322
$result | Should -Be $true
24-
} else {
23+
}
24+
else {
2525
# 如果没有找到被占用的端口,跳过此测试
2626
Set-ItResult -Skipped -Because "没有找到被占用的端口进行测试"
2727
}
@@ -42,19 +42,17 @@ Describe "Test-PortOccupation 函数测试" {
4242
}
4343
}
4444

45-
Describe "Get-PortProcess 函数测试" {
45+
Describe "Get-PortProcess 函数测试" -Tag 'Network', 'Slow' {
4646
Context "进程信息获取" {
4747
It "应该能够获取占用端口的进程信息" {
48-
# 找到一个被占用的端口
49-
$occupiedPort = Get-NetTCPConnection | Where-Object { $_.State -eq "Listen" } | Select-Object -First 1 -ExpandProperty LocalPort
50-
51-
if ($occupiedPort) {
52-
$result = Get-PortProcess -Port $occupiedPort
48+
if ($script:occupiedPort) {
49+
$result = Get-PortProcess -Port $script:occupiedPort
5350
($result) | Should -Not -Be $null
54-
$result.Port | Should -Be $occupiedPort
51+
$result.Port | Should -Be $script:occupiedPort
5552
$result.ProcessId | Should -BeGreaterThan 0
5653
$result.ProcessName | Should -Not -BeNullOrEmpty
57-
} else {
54+
}
55+
else {
5856
Set-ItResult -Skipped -Because "没有找到被占用的端口进行测试"
5957
}
6058
}
@@ -67,54 +65,51 @@ Describe "Get-PortProcess 函数测试" {
6765
}
6866

6967
It "返回的对象应该包含所有必需的属性" {
70-
# 找到一个被占用的端口
71-
$occupiedPort = Get-NetTCPConnection | Where-Object { $_.State -eq "Listen" } | Select-Object -First 1 -ExpandProperty LocalPort
72-
73-
if ($occupiedPort) {
74-
$result = Get-PortProcess -Port $occupiedPort
68+
if ($script:occupiedPort) {
69+
$result = Get-PortProcess -Port $script:occupiedPort
7570
if ($result) {
7671
$result.PSObject.Properties.Name | Should -Contain "Port"
7772
$result.PSObject.Properties.Name | Should -Contain "ProcessId"
7873
$result.PSObject.Properties.Name | Should -Contain "ProcessName"
7974
$result.PSObject.Properties.Name | Should -Contain "Path"
8075
$result.PSObject.Properties.Name | Should -Contain "CommandLine"
8176
}
82-
} else {
77+
}
78+
else {
8379
Set-ItResult -Skipped -Because "没有找到被占用的端口进行测试"
8480
}
8581
}
8682
}
8783
}
8884

89-
Describe "Wait-ForURL 函数测试" {
85+
Describe "Wait-ForURL 函数测试" -Tag 'Network', 'Slow' {
9086
Context "URL 可达性测试" {
91-
It "应该能够检测到可达的URL" {
92-
# 测试一个通常可达的URL(如果网络连接正常)
93-
$result = Wait-ForURL -DevToolsUrl "http://www.google.com" -Timeout 10 -Interval 1
94-
# 注意:这个测试可能因为网络环境而失败,所以我们不强制要求结果
87+
It "应该返回布尔值类型" {
88+
# 测试函数返回值类型,使用快速超时和短间隔避免长时间等待
89+
$result = Wait-ForURL -DevToolsUrl "http://localhost:99999" -Timeout 1 -Interval 0.5
9590
$result | Should -BeOfType [bool]
9691
}
9792

9893
It "应该对不可达的URL返回false" {
99-
# 使用一个不存在的本地地址
100-
$result = Wait-ForURL -DevToolsUrl "http://localhost:99999" -Timeout 5 -Interval 1
94+
# 使用一个不存在的本地地址,快速超时和短间隔
95+
$result = Wait-ForURL -DevToolsUrl "http://localhost:99999" -Timeout 1 -Interval 0.5
10196
$result | Should -Be $false
10297
}
10398

10499
It "应该正确处理超时" {
105-
# 测试超时功能
100+
# 测试超时功能,使用更短的超时时间和间隔
106101
$startTime = Get-Date
107-
$result = Wait-ForURL -DevToolsUrl "http://localhost:99998" -Timeout 3 -Interval 1
102+
$result = Wait-ForURL -DevToolsUrl "http://localhost:99998" -Timeout 1 -Interval 0.5
108103
$endTime = Get-Date
109104
$elapsed = ($endTime - $startTime).TotalSeconds
110105

111106
$result | Should -Be $false
112-
$elapsed | Should -BeGreaterOrEqual 3
113-
$elapsed | Should -BeLessThan 5 # 允许一些误差
107+
$elapsed | Should -BeGreaterOrEqual 1
108+
$elapsed | Should -BeLessThan 2 # 允许一些误差
114109
}
115110

116111
It "应该使用默认参数" {
117-
# 测试默认参数
112+
# 测试默认参数,使用快速超时
118113
{ Wait-ForURL -Timeout 1 } | Should -Not -Throw
119114
}
120115
}

scripts.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"scripts": {
33
"install": "pwsh -File ./install.ps1",
4-
"test": "Invoke-Pester -Configuration ( ./PesterConfiguration.ps1 )"
4+
"test": "Invoke-Pester -Configuration ( ./PesterConfiguration.ps1 )",
5+
"test:detailed": "Invoke-Pester -Output Detailed"
56
}
67
}

0 commit comments

Comments
 (0)