You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell/Pester函数调用无日志,如何实现交互式步骤查看?

实现交互式查看Pester测试每一步执行详情的方法

给你几个实用的方法,帮你在运行这个Pester测试时,交互式查看每一行的执行细节:

1. 开启Pester的详细输出模式

直接调整Pester的输出级别,就能看到更多执行日志:

  • 在脚本开头添加配置:
$PesterPreference = @{
    OutputMode = 'Diagnostic' # 最详细的输出,包含每一步执行信息;也可以用'Detailed'
}

或者用Invoke-Pester命令运行脚本时指定输出模式:

Invoke-Pester -Path .\你的测试脚本.ps1 -Output Diagnostic

这样会输出BeforeAll、It块的执行流程,以及每个命令的执行状态。

2. 在测试代码中添加调试输出

在关键步骤插入Write-Verbose,配合-Verbose参数运行脚本,就能看到每一步的执行提示:
修改你的测试块代码:

Run-Test `
    -Name "Testing ..." `
    -ResourceGroupName $primaryResourceGroupName, $recoveryResourceGroupName -Test {
 # Create primary and recovery resource groups
 Write-Verbose "[步骤1] 开始创建恢复资源组: $recoveryResourceGroupName"
 New-AzResourceGroup -name $recoveryResourceGroupName -location $location -force -Verbose
 Write-Verbose "[步骤2] 恢复资源组创建完成"

 Write-Verbose "[步骤3] 开始创建主资源组: $primaryResourceGroupName"
 New-AzResourceGroup -name $primaryResourceGroupName -Location $location -Force -Verbose
 Write-Verbose "[步骤4] 主资源组创建完成"

 Write-Verbose "[步骤5] 获取主资源组ID"
 $primaryResourceGroupId = (Get-AzResourceGroup -Name $primaryResourceGroupName -Verbose).ResourceId
 Write-Verbose "[步骤6] 主资源组ID: $primaryResourceGroupId"

 Write-Verbose "[步骤7] 获取恢复资源组ID"
 $recoveryResourceGroupId = (Get-AzResourceGroup -Name $recoveryResourceGroupName -Verbose).ResourceId
 Write-Verbose "[步骤8] 恢复资源组ID: $recoveryResourceGroupId"
}

然后运行脚本时加上-Verbose参数:

.\你的测试脚本.ps1 -Verbose

3. 使用PowerShell交互式调试器

方式一:插入手动断点

在测试块的关键位置添加Write-Debug,运行时用-Debug参数进入交互式调试:

Run-Test `
    -Name "Testing ..." `
    -ResourceGroupName $primaryResourceGroupName, $recoveryResourceGroupName -Test {
 # Create primary and recovery resource groups
 Write-Debug "即将创建恢复资源组,按回车继续"
 New-AzResourceGroup -name $recoveryResourceGroupName -location $location -force

 Write-Debug "即将创建主资源组,按回车继续"
 New-AzResourceGroup -name $primaryResourceGroupName -Location $location -Force

 # 后续步骤...
}

运行脚本时输入:

.\你的测试脚本.ps1 -Debug

此时会在每个Write-Debug处暂停,你可以输入y进入调试会话,用s单步执行,v 变量名查看变量值,c继续执行。

方式二:设置断点命令

运行脚本前,先给目标函数或行号设置断点:

# 给Run-Test函数设置断点,执行到函数时暂停
Set-PSBreakpoint -Command Run-Test

# 或者给测试块里的特定行设置断点(先查看行号)
Get-Content .\你的测试脚本.ps1 | Select-String "New-AzResourceGroup"
# 假设行号是28,设置行断点
Set-PSBreakpoint -Path .\你的测试脚本.ps1 -Line 28

然后运行脚本,会自动停在断点处,进入交互式调试模式。

4. 启用Pester内置调试

通过PesterPreference开启调试模式,会在每个Pester块(BeforeAll、It等)的执行前暂停:

$PesterPreference = @{
    EnableDebugging = $true
}

运行脚本后,会进入调试会话,你可以用单步命令逐步执行每一行代码。


内容的提问来源于stack exchange,提问作者Node.JS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 18:40:45