Azure Pipeline执行Publish JUnit Tests with Attachments任务遇length属性未定义报错
Azure Pipeline中使用Publish JUnit Tests with Attachments任务时报错:Cannot read property 'length' of undefined
问题场景
我在Azure Pipeline中配置了Publish JUnit Tests with Attachments (MetLife Cornerstone) 任务,用于将测试运行附件关联到Azure Test Plan的测试用例。任务的Test Case Mapping Type配置了以下JSON映射:
[{ "className": "main.java.com.Tests.DMA_Smoke_SimpleLogin", "methodName": "simpleLogin", "testSuiteId": 5729, "testCaseId": 5730 },{ "className": "main.java.com.Tests.DMA_Regression_VerifyRequiredCode", "methodName": "verifyRequiredCode", "testSuiteId": 5729, "testCaseId": 5731 }]
运行Maven流水线时,该任务抛出错误:Cannot read property 'length' of undefined,完整任务日志如下:
Starting: Publish Results w Attch: 2702 Clone ============================================================================== Task : Publish JUnit Tests with Attachments (MetLife Cornerstone) Description : Publish test results with attachments tied to Azure Test Plan Test Cases Version : 1.4.2 Author : MetLife Cornerstone Help : ============================================================================== Parsing JSON mapping: [{ "className": "main.java.com.Tests.DMA_Smoke_SimpleLogin", "methodName": "simpleLogin", "testSuiteId": 5729, "testCaseId": 5730, "length":3534 },{ "className": "main.java.com.Tests.DMA_Regression_VerifyRequiredCode", "methodName": "verifyRequiredCode", "testSuiteId": 5729, "testCaseId": 5731, "length":3535 }] Parsing JUnit Test Results file: D:\AzDOAgents\Agent1-MetLife-US-POC\_work\25\s\test-output\testng-results.xml Creating test run entry. 200 Retrieved test run results Unable to update test case results: Cannot read property 'length' of undefined Completing testrun 1001010 200 { id: 1001010, name: 'TestRun_QE framework mobile test_20221114.7', url: 'https://dev.azure.com/MetLife-US-POC/TechQA/_apis/test/Runs/1001010', build: { id: '2947' }, isAutomated: true, owner: { displayName: 'TechQA Build Service (MetLife-US-POC)', id: '81631c3a-706c-47af-bfee-a028667511b8' }, project: { id: '8f053f76-b7f0-477f-9998-6a342ed0963d', name: 'TechQA' }, startedDate: '2022-11-14T21:43:19.89Z', completedDate: '2022-11-14T21:43:20.603Z', state: 'Aborted', plan: { id: '5728' }, totalTests: 2, incompleteTests: 0, notApplicableTests: 0, passedTests: 0, unanalyzedTests: 2, revision: 4, webAccessUrl: 'https://dev.azure.com/MetLife-US-POC/TechQA/_TestManagement/Runs?runId=1001010&_a=runCharts', pipelineReference: { pipelineId: 2947, stageReference: {}, phaseReference: {}, jobReference: {} } } Completed test run ##[error]Cannot read property 'length' of undefined
已尝试多种排查方式但未解决,求可行的解决方案。
排查方向与解决方案
1. 移除映射JSON中自动添加的length字段
从日志可以看到,任务解析时的映射JSON比初始配置多了length字段(如"length":3534),这是任务内部解析异常或配置框自动注入的无效数据,会导致脚本读取未定义对象的length属性。
操作步骤:
- 重新编辑
Test Case Mapping Type配置,删除所有length键值对,确保JSON结构与初始配置一致:[{ "className": "main.java.com.Tests.DMA_Smoke_SimpleLogin", "methodName": "simpleLogin", "testSuiteId": 5729, "testCaseId": 5730 },{ "className": "main.java.com.Tests.DMA_Regression_VerifyRequiredCode", "methodName": "verifyRequiredCode", "testSuiteId": 5729, "testCaseId": 5731 }] - 保存配置后重新运行流水线。
2. 检查TestNG测试结果文件的合法性
任务解析testng-results.xml时若遇到格式问题,会导致后续处理中预期的数组/字符串对象未定义,触发length读取错误。
检查要点:
- 确认
testng-results.xml路径正确,文件存在且格式合法; - 核对XML中测试用例的
class和method名称,确保与映射JSON中的className、methodName完全匹配(注意大小写、包路径); - 确保XML中没有缺失测试结果的必要字段(如
test-name、status等)。
3. 升级任务版本
当前使用的任务版本为1.4.2,可能存在已知bug。尝试升级到最新版本:
- 在Azure Pipeline编辑页面,找到该任务,点击版本选择器切换到最新可用版本;
- 重新运行流水线验证问题是否解决。
4. 验证服务账号权限
确保流水线使用的服务账号(TechQA Build Service (MetLife-US-POC))拥有以下权限:
- Azure Test Plan测试计划、测试套件、测试用例的读写权限;
- 测试运行的创建、更新权限。
内容的提问来源于stack exchange,提问作者Rajesh Nichenametla
相关产品推荐
相关产品推荐

