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

Azure Pipeline CmdLine@2调用OpenCover.Console报错求助

解决Azure Pipeline中OpenCover运行报错的问题

修正后的任务代码

- task: CmdLine@2
  displayName: Test solution
  inputs:
    script: |
      "%OPENCOVER%\opencover.console.exe" "-target:vstest.console.exe" "-targetargs:\"$(Build.BinariesDirectory)\**\*Tests.dll\"" "-output:$(Agent.BuildDirectory)\TestResults\$(Build.Repository.Name).coverage.xml" "-excludebyfile:**/Migrations/*;**moq*;**FluentAssertions*" "-excludebyattribute:*.ExcludeFromCodeCoverage;*.ExcludeFromCodeCoverageAttribute;*.ExcludeFromCoverage;*.ExcludeFromCoverageAttribute;*.GeneratedCode;*.GeneratedCodeAttribute" -register:path64 -mergebyhash -hideskipped:all

问题分析与修复说明

  1. YAML多行字符串格式:原代码未用|标记多行字符串,YAML解析器会将整个脚本视为单行字符串,其中的转义字符(如\")被错误解析,触发"unknown escape character"报错。添加|后,YAML会保留脚本的原始换行与转义结构,交由Cmd.exe处理。
  2. 路径与变量转义:%OPENCOVER%是Cmd环境变量,无需额外转义;路径中的反斜杠\作为Cmd路径分隔符,在YAML多行字符串中保持原始写法即可。
  3. 参数引号处理:-targetargs:\"...\"中的转义双引号用于确保测试dll路径作为整体参数传递给vstest.console.exe,在YAML多行字符串中该写法合法。

替代方案(单引号包裹脚本)

也可将整个脚本用单引号包裹,避免YAML解析转义问题:

- task: CmdLine@2
  displayName: Test solution
  inputs:
    script: '"%OPENCOVER%\opencover.console.exe" "-target:vstest.console.exe" "-targetargs:\"$(Build.BinariesDirectory)\**\*Tests.dll\"" "-output:$(Agent.BuildDirectory)\TestResults\$(Build.Repository.Name).coverage.xml" "-excludebyfile:**/Migrations/*;**moq*;**FluentAssertions*" "-excludebyattribute:*.ExcludeFromCodeCoverage;*.ExcludeFromCodeCoverageAttribute;*.ExcludeFromCoverage;*.ExcludeFromCoverageAttribute;*.GeneratedCode;*.GeneratedCodeAttribute" -register:path64 -mergebyhash -hideskipped:all'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 08:40:09