Java读取Excel上传Azure DevOps测试用例遇405错误及需求求助
Azure DevOps批量上传测试用例问题解决方案
1. 正确的API URL及操作流程
要实现创建测试套件并关联用户故事,同时批量上传测试用例,需分三步执行API调用(单一POST请求无法完成所有操作):
步骤1:创建测试套件
POST请求URL:
https://dev.azure.com/{organization}/{project}/_apis/test/plans/{planId}/suites?api-version=7.1-preview.3
请求体示例(JSON格式):
{ "name": "关联用户故事的测试套件", "suiteType": "StaticTestSuite" }
替换说明:
{organization}为你的Azure DevOps组织名,{project}为项目名,{planId}为目标测试计划的ID(可从测试计划页面URL中获取)。
步骤2:关联用户故事到测试套件
使用PATCH请求更新测试套件的工作项关联:
URL:
https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{suiteId}?api-version=7.1-preview.3
请求头需设置Content-Type: application/json-patch+json,请求体示例:
[ { "op": "add", "path": "/relations/-", "value": { "rel": "System.LinkTypes.Hierarchy-Reverse", "url": "https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{userStoryId}", "attributes": { "comment": "关联用户故事到测试套件" } } } ]
替换说明:
{suiteId}是步骤1生成的测试套件ID,{userStoryId}为目标用户故事的工作项ID。
步骤3:批量创建测试用例并关联到测试套件
POST请求URL(支持批量创建):
https://dev.azure.com/{organization}/{project}/_apis/wit/workitemsbatch?api-version=7.1-preview.3
请求体示例:
{ "requests": [ { "method": "POST", "url": "/_apis/wit/workitems/$Test%20Case?api-version=7.1-preview.3", "body": [ { "op": "add", "path": "/fields/System.Title", "value": "测试用例标题1" }, { "op": "add", "path": "/fields/Microsoft.VSTS.TCM.Steps", "value": "<steps><step><parameterizedString isformatted=\"true\">步骤1:执行操作</parameterizedString><parameterizedString isformatted=\"true\">预期结果1</parameterizedString></step></steps>" }, { "op": "add", "path": "/relations/-", "value": { "rel": "System.LinkTypes.Hierarchy-Forward", "url": "https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{suiteId}" } } ] }, { // 第二个测试用例配置,结构与上述一致 } ] }
405错误通常因HTTP方法误用(如用GET替代POST/PATCH)或URL路径错误导致,需确保每个步骤使用对应方法。
2. Azure DevOps Maven依赖
使用官方Java SDK简化API调用,可添加以下Maven依赖:
<dependencies> <!-- Azure DevOps核心API依赖 --> <dependency> <groupId>com.microsoft.azure</groupId> <artifactId>azure-devops-api</artifactId> <version>7.1.0</version> </dependency> <!-- Azure核心依赖,用于认证与HTTP请求处理 --> <dependency> <groupId>com.azure</groupId> <artifactId>azure-core</artifactId> <version>1.40.0</version> </dependency> <!-- PAT认证所需依赖(使用个人访问令牌时) --> <dependency> <groupId>com.azure</groupId> <artifactId>azure-identity</artifactId> <version>1.12.0</version> </dependency> </dependencies>
版本号可根据官方最新稳定版调整,Maven中央仓库已收录上述依赖,无需额外配置仓库。
3. 可行的替代解决方案
方案1:Azure DevOps CLI批量操作
无需编写Java代码,用CLI快速批量导入:
- 安装Azure DevOps CLI并登录:
az login,配置默认组织与项目:az devops configure --defaults organization=https://dev.azure.com/{organization} project={project} - 编写CSV文件存储测试用例字段(标题、步骤等),通过脚本循环执行命令创建并关联测试用例:
az boards work item create --title "测试用例标题" --type "Test Case" --fields "Microsoft.VSTS.TCM.Steps=<steps>...</steps>" --link-type "Hierarchy-Forward" --link-id {suiteId}
方案2:Azure DevOps Excel直接导入
- 进入Azure DevOps测试计划页面的目标测试套件
- 点击「导入测试用例」→「从Excel导入」
- 下载官方模板,填充测试用例数据(含关联的用户故事ID),上传后系统自动完成关联
方案3:PowerShell脚本批量处理
利用Azure DevOps REST API编写PowerShell脚本,读取Excel数据并批量上传,无需Java环境,适合熟悉脚本开发的场景。
内容的提问来源于stack exchange,提问作者Shyam Sundar V
相关产品推荐
相关产品推荐

