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

如何在GitHub Workflow中用NUnit重跑单个SpecFlow场景大纲

问题:SpecFlow场景大纲单示例重跑的过滤器格式错误

场景大纲代码

@DOC-136
@DOC-137
@DOC-138
@DOC-139
@Browser:Chrome
Scenario Outline: AT01.01 - Test Workflow - Create a case
    When I open Create New Case
        And I fill the required fields with Zirconia, Monolithic, E2E Test CaseItems
        | Field              | Value                   |
        | Items              | <CaseItems>             |
Examples:
    | Name           | CaseItems                                                                                                                                                                    | Teeth                         | Jama doc    |
    | Removable      | Test Partial denture                                                                                                                                                         | 17-31                         | DOC-136 |
    | Implant Studio | Surgical guide                                                                                                                                                               | 10-15                         | DOC-137 |
    | Full Denture   | Full denture                                                                                                                                                                 | 2                             | DOC-138 |
    | Copy Denture   | Copy denture                                                                                                                                                                 | 2                             | DOC-139 |
    | Appliance      | Splints  

当前重跑逻辑与错误

尝试通过获取失败测试名称构建过滤器,重跑DOC-136对应的示例,构建的过滤器内容如下:

(FullyQualifiedName~AT01_01_TestWorkflow_CreateACase('Removable','Test Partial denture','17-31','DOC-136',null)) & Category=Browser:Chrome

执行时触发NUnit错误:

An exception occurred while invoking executor 'executor://nunit3testexecutor/': Incorrect format for TestCaseFilter Missing Operator '|' or '&'. Specify the correct format and try again. Note that the incorrect format can lead to no test getting executed.

错误原因

NUnit的测试过滤器语法要求:当匹配的字符串包含括号、逗号、单引号等特殊字符时,必须用双引号将匹配值包裹,否则NUnit无法正确解析过滤器的语法边界,导致运算符识别错误。

解决方案

1. 修正过滤器格式

将FullyQualifiedName的匹配值用双引号包裹,修正后的过滤器示例:

(FullyQualifiedName~"AT01_01_TestWorkflow_CreateACase('Removable','Test Partial denture','17-31','DOC-136',null)") & Category=Browser:Chrome

2. 调整PowerShell代码中的过滤器构建逻辑

修改重跑脚本中过滤器的生成部分,确保为匹配值添加双引号,并正确转义内部的特殊字符:

$currentRerun = 1
$testFilter = $env:FAILED_TEST_FILTER
$folderName = "${{ steps.run-tests.outputs.folderName }}"
Write-Host "Original Test Filter: $testFilter"

# 为测试名称添加双引号包裹,并转义内部的双引号(如果有的话)
$escapedTestFilter = "`"$($testFilter.Replace('"', '\"'))`""
Write-Host "Escaped test filter for command line: $escapedTestFilter"

$finalFilter = "(FullyQualifiedName~$escapedTestFilter) & ${{ inputs.Browser }}"
Write-Host "Final filter being passed to dotnet: $finalFilter"

if('${{inputs.Environment}}' -eq 'Linux')
{
  xvfb-run dotnet test `
    --no-build `
    --verbosity normal `
    --configuration Release `
    --filter "$finalFilter" `
    --results-directory TestResults/$folderName `
    --logger "trx;LogFileName=TestResults_Rerun_$currentRerun.trx"
}

3. 关键修改点说明

  • 用""$($testFilter.Replace('"', '"'))""为测试名称添加外层双引号,并转义名称内部可能存在的双引号,避免语法冲突。
  • 确保最终的过滤器字符串整体被双引号包裹(--filter "$finalFilter"),让shell正确传递完整的过滤器表达式。

验证

执行修正后的脚本,NUnit将能正确解析过滤器,仅重跑DOC-136对应的场景大纲示例。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 15:43:13