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

如何在TFS中仅针对最新构建运行失败测试用例而非全量回归?

Running Only Failed Test Cases on Latest Build in TFS/Azure DevOps Server

Absolutely, you can pull this off—there are a few practical approaches to avoid rerunning your full regression suite and instead target only the failed tests when deploying to any environment. Let’s break down your options:

Option 1: Use Azure DevOps Server Web APIs to Trigger Targeted Test Runs

This gives you full control to fetch failed tests and trigger a focused run. Here’s how to implement it:

  • Step 1: Fetch failed tests from the latest build
    Use the Test Runs API to get all failed test results from your most recent regression build. You’ll need the build ID of your daily regression run, then call:
    GET https://{tfs-server-url}/{project-name}/_apis/test/runs?buildId={buildId}&resultFilter=Failed&api-version=7.1-preview.3
    
    Parse the response to extract the test case IDs (or fully qualified test names—IDs are more reliable to avoid name collisions) of the failed tests.
  • Step 2: Trigger a new test run with only those tests
    Use the Create Test Run API to launch a new run targeting just the failed tests. You’ll need to define the test points or test cases to include in the request body. For example, a simplified JSON payload might look like this:
    {
      "name": "Failed Tests Retry for Deployment",
      "build": {"id": {buildId}},
      "testCaseIds": [123, 456, 789],
      "state": "InProgress"
    }
    
    Call this via POST https://{tfs-server-url}/{project-name}/_apis/test/runs?api-version=7.1-preview.3
  • Integrate into your deployment pipeline
    Wrap this logic into a PowerShell or Python script, add it as a step in your deployment pipeline, and set it to run before/after deploying to your target environment. Make sure the service account running the pipeline has Test Management permissions to access and trigger test runs.

Option 2: Leverage Built-in VSTest Task Parameters (No Custom Code Needed)

If you prefer to avoid writing API scripts, you can use the VSTest task in your deployment pipeline to target specific tests:

  • First, in your daily regression pipeline, add a step to export the list of failed test names/IDs to a file or a pipeline variable. For example, use the Publish Test Results task, then parse the generated TRX file to extract failed tests and store them in a variable (like $(FailedTests)).
  • Then, in your deployment pipeline’s VSTest task, use the Tests parameter to specify the failed tests. If you’re using fully qualified names, the parameter would look like:
    /tests "$(FailedTests)"
    
    This tells the VSTest runner to only execute those specific tests instead of the full suite.

Option 3: Use Test Plans "Rerun Failed Tests" Feature

If you’re using Azure DevOps Test Plans to manage your test cases, you can configure a test suite that automatically includes only failed tests from the latest build:

  • Create a dynamic test suite in your Test Plan, set the filter to include tests that failed in the most recent regression build.
  • When deploying, trigger a test run for this dynamic suite instead of the full regression suite. This is a low-code option that’s great if you already use Test Plans for test management.

Key Notes

  • Always prefer test case IDs over names when targeting tests—names can be duplicated, leading to incorrect test runs.
  • Make sure your pipeline service account has the necessary permissions to access test results, create test runs, and execute tests in your target environments.
  • If you’re using on-prem TFS (pre-2019), some API versions might differ—check the TFS API documentation for your specific version.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:55:18