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

如何在Azure DevOps中自动重命名关联千余测试用例的共享步骤参数

共享步骤参数批量重命名解决方案(基于Azure DevOps API)

核心思路

直接修改共享步骤的参数名会导致关联测试用例丢失参数数据,必须走先更新共享步骤参数名,再批量同步所有关联测试用例的参数映射的流程,通过Azure DevOps API实现自动化操作,确保测试数据完整保留。

具体步骤

1. 准备工作

  • 生成Azure DevOps个人访问令牌(PAT),权限需包含Work Items (Read & Write)和Test Plans (Read & Write)
  • 确认目标共享步骤的ID,以及需要替换的旧参数名、新参数名
  • 准备Python或PowerShell环境(以Python为例,使用官方azure-devops库简化操作)

2. 更新共享步骤的参数名

调用Work Items API修改共享步骤的步骤内容,替换参数名:

  • 接口:PATCH https://dev.azure.com/{组织名}/{项目名}/_apis/wit/workitems/{共享步骤ID}?api-version=7.1-preview.3
  • 请求体示例(JSON):
[
  {
    "op": "replace",
    "path": "/fields/Microsoft.VSTS.TCM.Steps",
    "value": "<steps id=\"0\"><step id=\"1\"><parameterizedString isformatted=\"true\">输入参数*{{新参数名}}</parameterizedString><parameterizedString isformatted=\"true\">验证结果</parameterizedString><description/></step></steps>"
  }
]

注意:需将原始步骤XML中所有{{旧参数名}}替换为{{新参数名}},其他内容保持不变

3. 批量获取所有关联测试用例

通过Wiql查询筛选出引用目标共享步骤的所有测试用例:

  • 调用Wiql查询接口:POST https://dev.azure.com/{组织名}/{项目名}/_apis/wit/wiql?api-version=7.1-preview.2
  • 查询语句示例:
SELECT [System.Id], [System.Title]
FROM WorkItems
WHERE [System.WorkItemType] = 'Test Case'
AND [Microsoft.VSTS.TCM.SharedStepReferencedBy] CONTAINS '{共享步骤ID}'

返回结果将包含所有关联测试用例的ID列表。

4. 同步测试用例的参数映射与测试数据

对每个测试用例,同步参数引用并保留原有测试数据:

  1. 调用API获取测试用例的Microsoft.VSTS.TCM.Steps字段XML内容,将其中的parameter=\"旧参数名\"替换为parameter=\"新参数名\"
  2. 若测试用例包含本地测试数据(Microsoft.VSTS.TCM.LocalDataSource字段),将数据中的旧参数名替换为新参数名
  3. 调用Work Items的PATCH接口提交更新

5. Python脚本示例片段

from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication

# 配置参数
org_url = "https://dev.azure.com/你的组织名"
proj_name = "你的项目名"
pat = "你的PAT令牌"
shared_step_id = 1234  # 替换为目标共享步骤ID
old_param = "旧参数名"
new_param = "新参数名"

# 建立连接
credentials = BasicAuthentication("", pat)
connection = Connection(base_url=org_url, creds=credentials)
wit_client = connection.clients.get_work_item_tracking_client()

# 更新共享步骤
shared_step = wit_client.get_work_item(shared_step_id)
steps_xml = shared_step.fields["Microsoft.VSTS.TCM.Steps"]
updated_steps_xml = steps_xml.replace(f"{{{{{old_param}}}}}", f"{{{{{new_param}}}}}")
update_body = [{"op": "replace", "path": "/fields/Microsoft.VSTS.TCM.Steps", "value": updated_steps_xml}]
wit_client.update_work_item(update_body, shared_step_id)

# 查询关联测试用例
wiql_query = f"""
SELECT [System.Id]
FROM WorkItems
WHERE [System.WorkItemType] = 'Test Case'
AND [Microsoft.VSTS.TCM.SharedStepReferencedBy] CONTAINS '{shared_step_id}'
"""
query_result = wit_client.query_by_wiql(wiql_query)
test_case_ids = [item.id for item in query_result.work_items]

# 批量更新测试用例
for tc_id in test_case_ids:
    tc = wit_client.get_work_item(tc_id)
    update_ops = []
    # 更新步骤中的参数引用
    tc_steps_xml = tc.fields["Microsoft.VSTS.TCM.Steps"]
    updated_tc_steps = tc_steps_xml.replace(f"parameter=\"{old_param}\"", f"parameter=\"{new_param}\"")
    update_ops.append({"op": "replace", "path": "/fields/Microsoft.VSTS.TCM.Steps", "value": updated_tc_steps})
    # 更新测试数据中的参数名
    if "Microsoft.VSTS.TCM.LocalDataSource" in tc.fields:
        data_source = tc.fields["Microsoft.VSTS.TCM.LocalDataSource"]
        updated_data = data_source.replace(old_param, new_param)
        update_ops.append({"op": "replace", "path": "/fields/Microsoft.VSTS.TCM.LocalDataSource", "value": updated_data})
    # 提交更新
    wit_client.update_work_item(update_ops, tc_id)

注意事项

  • 操作前务必在测试环境验证,或选择少量测试用例做试点
  • 批量更新时建议添加1秒左右的延时,避免触发API限流
  • 测试用例数量超过1000时,建议分批次处理(如每次处理100个)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 04:55:33