我在CSV文件中解析数据遇到一些麻烦,可以使用Groovy通过REST API在Azure DevOps中创建测试运行
我从CSV文件中解析数据时遇到了一些麻烦,可以使用Groovy通过REST API在Azure DevOps中创建测试运行。
CSV文件被选为选项卡,因为其中一个列(rtestpoints)需要将测试点分组为测试运行。
这是我在CSV文件上遇到麻烦的列
rTestPoints
1754478, 1754479
,如下所示:
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
但是,当我将此数据传递给创建测试运行的方法时,测试点会在帖子呼叫的正文中解析为
"pointIds": [
"1754478, 1754479"
]
: T将测试运行与测试点关联。 报价需要如下:在:
"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
}
I'm having some trouble parsing data from a csv file to create test runs in Azure DevOps via the REST API using Groovy.
The csv file is tab delimited because one of the columns (rTestPoints) requires commas for grouping the test points to its test run.
This is the column I'm having trouble with
rTestPoints
1754478, 1754479
The csv file is formatted as follows:
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
but when I pass this data to the method to create the test run, the testPoints are getting parsed in the body of the Post call as:
"pointIds": [
"1754478, 1754479"
]
which isn't associating the test run to the test point. Quotes need to surround both values as in:
"pointIds": [
"1754478", "1754479"
]
}
I tried adding quotes to the csv input file but it results in:
"pointIds": [
"\"1754478\", \"1754479\""
]
Any ideas on how I can parse this data correctly by surrounding both values with quotes?
I'm sure there's something I can do to handle this.
Thanks!
Also here's some snippets of the code I'm using.
Here's how I've designed the csv file:
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())
}
here's the block of code where I'm passing in the data from 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}")
}
Here's the method in the TestManagementService to create the 'test run'
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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我要做的就是通过rtestpoints作为字符串传递,我必须将其作为“列表”传递
,然后将rtestpointslist传递给createTestrun方法
What I had to do was pass instead of passing the rTestPoints as a string, I had to pass it as a "List"
Then pass rTestPointsList to the createTestRun method