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

如何在Azure Pipeline中排除标记@ignore的SpecFlow测试用例

解决方案

问题背景

我基于SpecFlow维护了多份特性文件与大量UI测试用例,每个特性文件都标记了如@Performance、@AutoComplete这类页面级标签,并将这些标签作为参数传入Azure DevOps YAML流水线。目前部分测试用例标记了@ignore标签,但流水线运行时并未排除这些用例,导致执行失败,需要实现自动跳过所有带@ignore标签的测试用例。

修改后的流水线代码

parameters:
- name: 'featuresToRun'
  type: object
  default:
  - Performance
  - AutoComplete
  - Benches
  - CATMI
  - Export
  - GemIntegration
  - Keyboard
  - MainMenu
  - NewVoyages
  - ReferenceData
  - Settings
  - SimilarVoyages
  - Validation
  - Views
  - VolumeConversion
  - Voyages
  - LaycanRanges

trigger: none

jobs:
- job: startVM
  timeoutInMinutes: 10
  pool:
    vmImage: 'windows-latest'

  steps:
  - checkout: none

- job: runTests
  timeoutInMinutes: 1800
  dependsOn: startVM
  condition: not(canceled())

  pool:
    name: 'UI Automation'

  steps:
  - task: ScreenResolutionUtility@1
    inputs:
      displaySettings: 'optimal'

  - task: VisualStudioTestPlatformInstaller@1
    inputs:
      packageFeedSelector: 'nugetOrg'
      versionSelector: 'latestStable'

  - task: NuGetCommand@2
    inputs:
      command: 'restore'
      restoreSolution: '**/*.sln'
      feedsToUse: 'config'

  - task: MSBuild@1
    inputs:
      solution: 'UIAutomation.sln'
      msbuildArchitecture: 'x64'
      clean: true

  - ${{each feature in parameters.featuresToRun}}:
    - task: VSTest@2
      displayName: ${{feature}} Tests
      inputs:
        testSelector: 'testAssemblies'
        testAssemblyVer2: |
          UIAutomation.Specs\bin\Debug\UIAutomation.Specs.dll
          !**\*TestAdapter.dll
          !**\obj\**
        searchFolder: '$(System.DefaultWorkingDirectory)'
        uiTests: true
        testRunTitle: '${{feature}}'
        # 关键修改:添加排除@ignore标签的过滤条件
        testFiltercriteria: 'Category=${{feature}}&Category!=ignore'
        rerunFailedTests: true
        rerunMaxAttempts: 2
        rerunFailedThreshold: 80
        codeCoverageEnabled: true
      continueOnError: true

修改说明

SpecFlow中的@标签会被自动映射为VSTest的Category属性,因此只需在testFiltercriteria中添加逻辑与条件:

  • 保留指定feature的测试用例:Category=${{feature}}
  • 排除标记了@ignore的用例:&Category!=ignore

通过这个组合条件,流水线在运行对应feature的测试时,会自动跳过所有带@ignore标签的用例,避免因忽略用例导致的执行失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 08:15:31