【发布时间】:2019-10-15 11:19:16
【问题描述】:
我有以下功能文件,它读取输入并将输入 id 附加到响应中,并以以下格式写入文本文件:
|123|{"products": [ { "pid": "1a"} ] }|
|124|{"products": [ { "pid": "1b"} ] }|
|125|{"products": [ { "pid": "1c"} ] }|
这样我就可以用它制作输入表,并且不需要以文本格式复制每个响应并粘贴来制作示例:
我尝试了以下方法:
Feature: sample karate test script
Background:
* url BaseUrl
* configure headers = read('classpath:headers.js')
* def jsonFromCsv = read('data.csv')
* def size = karate.sizeOf(jsonFromCsv)
* print size
Scenario Outline: Get All Tests
* def doStorage =
"""
function(args) {
var DataStorage = Java.type('DataStorage.DataStorage'); // Java class that writes to a text file
var dS = new DataStorage();
return dS.write(args);
}"""
Given path '/v1/path'
When method get
Then status 200
* def json = __row.expected
* def id = __row.id
* def app = '|'+<id>+'|'+json+'|'
* print app # prinst it in the expected format
* def result = call doStorage app
Examples:
| jsonFromCsv |
但问题是只有最后一次读取的数据被写入文件。
我也尝试过以下方法,但结果与上述相同:
* def js = function (app){karate.write(app,'input.txt')}
DataStorage.java:
package DataStorage;
import java.io.*;
public class DataStorage {
public void write( String text) throws IOException {
BufferedWriter output = null;
try {
File file = new File("input");
output = new BufferedWriter(new FileWriter(file));
output.append(text);
output.close();
} catch ( IOException e ) {
e.printStackTrace();
} finally {
if ( output != null ) {
output.close();
}
}
}
}
【问题讨论】:
-
您是否尝试从 JSON 构建示例表?
-
我正在尝试为回归套件构建示例表。
-
有什么方法可以一次性完成。我有 1000 多个测试用例,用于 Excel 表中的单个 API 以实现自动化!同样,我有几项服务。
标签: karate