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

Pester 5.4生成空代码覆盖率文件问题求助

Pester生成空Coverage.xml问题排查及解决

你的问题核心是代码覆盖率配置指向了错误的文件路径:

  • $conf.CodeCoverage.Path参数的作用是指定需要统计覆盖率的业务源代码文件,而你设置成了测试脚本路径(.\Tests\*.Unit.Test.ps1)。测试脚本是用来执行测试的,本身不需要统计覆盖率,这就导致Pester没有找到需要分析的目标代码,最终生成空的Coverage.xml。

修复步骤

  1. 修改$conf.CodeCoverage.Path为你的业务源代码路径,比如:
    • 如果源代码在./Src/目录下:$conf.CodeCoverage.Path=".\Src\*.ps1"
    • 如果有多个源代码目录,可以用数组指定:$conf.CodeCoverage.Path=@(".Src\*.ps1", ".\Modules\*.ps1")
  2. 提前创建TestResults目录(避免Pester因目录不存在无法生成文件):
    New-Item -Path ./TestResults -ItemType Directory -Force
    

修改后的完整配置示例

Write-Verbose -Message "Running Pester Tests"
# 提前创建结果目录
New-Item -Path ./TestResults -ItemType Directory -Force

$conf = New-PesterConfiguration
$conf.Run.Path= ".\Tests\"
$conf.Run.TestExtension = '.Unit.Test.ps1'
$conf.Output.CIFormat = "AzureDevops"
$conf.TestResult.Enabled = $true
$conf.TestResult.OutputPath = './TestResults/Testresult.xml'
$conf.CodeCoverage.Enabled = $true
$conf.CodeCoverage.OutputFormat = 'JaCoCo'
$conf.CodeCoverage.CoveragePercentTarget = 70
$conf.CodeCoverage.RecursePaths = $true
$conf.CodeCoverage.OutputPath="./TestResults/Coverage.xml"
# 修改为业务源代码路径
$conf.CodeCoverage.Path=".\Src\*.ps1"
    
Invoke-Pester -Configuration $conf

额外验证点

  • 确认源代码路径正确,Pester能访问到这些文件
  • 确保你的测试脚本确实执行了源代码中的函数或逻辑,否则覆盖率会显示0,但不会生成空文件

内容的提问来源于stack exchange,提问作者JDC

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 09:20:21