我在解析csv文件中的数据以使用Groovy通过REST API在Azure DevOps中创建测试运行时遇到了一些问题。
csv文件以制表符分隔,因为其中一列(rTestPoints)需要逗号将测试点分组到其测试运行中。
这就是我遇到麻烦的专栏
rTestPoints
1754478, 1754479
csv文件的格式如下:
runIds runNames runOwners rTestPoints runStartDate runCreatedDate runCompletedDate
1463132 NewSuite1 (Manual) Test Owner 1754478, 1754479 2022-05-27T18:51:12Z 2022-05-27T18:51:12Z 2022-05-27T18:51:13.283Z
但是当我将这些数据传递给方法以创建测试运行时,testPoint在Post调用的主体中被解析为:
"pointIds": [
"1754478, 1754479"
]
这不会将测试回合与测试点产生相关。两个值都需要用引号括起来,如下所示:
"pointIds": [
"1754478", "1754479"
]
}
我尝试在csv输入文件中添加引号,但结果是:
"pointIds": [
"\"1754478\", \"1754479\""
]
有什么想法吗?我如何通过用引号将两个值括起来来正确地解析这些数据?
我相信我能做些什么来处理这件事。
谢谢你!
这里还有我正在使用的一些代码片段。
下面是我如何设计csv文件:
def csvdata = []
def columns = ['runIds','runNames', 'runOwners', 'rTestPoints', 'runStartDate', 'runCreatedDate', 'runCompletedDate']
BufferedReader br = new BufferedReader(new FileReader(fileName))
br.readLine(); // consume first line and ignore
br.splitEachLine(" ") {values ->
csvdata.add([columns, values].transpose().collectEntries())
}
这是我从csv传入数据的代码块
for (int i=0; i <csvdata.size(); i++) {
String runIds = csvdata.runIds[i]
String runName = csvdata.runNames[i]
String runOwner = csvdata.runOwners[i]
String rTestPoints = csvdata.rTestPoints[i]
String runState = "InProgress"
String runStartDate = csvdata.runStartDate[i]
String runCreateDate = csvdata.runCreatedDate[i]
String runCompletedDate = csvdata.runCompletedDate[i]
String comment = "test run copied from project ${srcproject} and test plan ${srcPlanId} from runID: ${runIds}"
try {
createTestRun = testManagementService.createTestRun(collection, targetProject, targetTestPlanId, rTestPoints, comment, runOwner, runName, runState, runCreateDate, runStartDate, runCompletedDate)
} catch (e) {
log.error("Unable to create test run ${runIds} for test point(s) ${rTestPoints} in project: ${targetProject}")
}
下面是TestManagementService中用于创建“测试运行”的方法
public def createTestRun(collection, project, testplanId, testpointIds, comment, owner, name, state, createdDate, startedDate, completedDate) {
def eproject = URLEncoder.encode(project, 'utf-8')
eproject = eproject.replace('+', '%20')
def uri = "${genericRestClient.getTfsUrl()}/${collection}/${eproject}/_apis/test/runs?api-version=6.0&bypassRules=True&suppressNotifications=true"
def body = ['name': name, 'state': state, 'comment': comment, 'createdDate': createdDate, 'starteDate': startedDate, 'completedDate': completedDate, 'owner': [ 'displayName': owner], 'plan': [ 'id': testplanId], 'pointIds': [ testpointIds ] ]
String sbody = new JsonBuilder(body).toPrettyString()
def result = genericRestClient.rateLimitPost(
requestContentType: ContentType.JSON,
contentType: ContentType.JSON,
uri: uri,
body: sbody,
//headers: [Accept: 'application/json'],
query: ['api-version': '5.1-preview.1' ]
)
return result
}
1条答案
按热度按时间uqjltbpv1#
我必须做的是传递rTestPoints而不是将其作为字符串传递,我必须将其作为“List”传递
然后将rTestPointsList传递给createTestRun方法