复杂的数据驱动测试,读取多个文件

问题描述 投票:0回答:1

我有一个例子,我无法解决空手道问题。我有25个心电图文件,应将其上传给患者。有一个索引文件,其中包含这些文件的正确顺序。但是在通过API上传之前,我需要阅读该索引文件,找到要上传的下一个文件。

另外,我需要使用特定的命名约定重命名这些文件,因为我通过api上传的每个文件都应该是唯一的。名称由患者电子邮件,时间戳和索引组成。每个文件的名称中的时间戳也应增加25500毫秒。上传后,我应该删除那些文件。

我试图编写一个Java互操作程序,但是后来变得如此复杂。

karate
1个回答
0
投票

使用空手道实际上很简单。首先,我假设您有一个index.json,其内容如下:

[
  { "file": "ecg-01.json" },
  { "file": "ecg-02.json" }
]  

ecg-01.json的内容:

{ "email": "[email protected]" }

ecg-02.json的内容:

{ "email": "[email protected]" }  

请注意,在空手道中,您无需保存临时文件即可进行文件上传,您可以获取现有文件并使用其他名称执行上传,请参考:https://github.com/intuit/karate#multipart-file

我们将在upload.feature中进行主要工作,这里我们仅将https://httpbin.org/用作演示端点:

Scenario:
* def patient = read(file)
* def timeStamp = startTime + __loop * 25500
* def name = patient.email + "_" + timeStamp + "_" + __loop

* url 'https://httpbin.org/anything'
* request { name: '#(name)' }
* method post

因此,最后您需要做的就是从您的邮件功能开始循环,它可以像这样简单:

* def startTime = java.lang.System.currentTimeMillis()
* def data = read('index.json')
* call read('upload.feature') data

当我运行此示例时,它发出了2个POST请求,如下所示:

1 > POST https://httpbin.org/anything
1 > Accept-Encoding: gzip,deflate
1 > Connection: Keep-Alive
1 > Content-Length: 39
1 > Content-Type: application/json; charset=UTF-8
1 > Host: httpbin.org
1 > User-Agent: Apache-HttpClient/4.5.11 (Java/1.8.0_231)
{"name":"[email protected]_1582424404163_0"}

1 > POST https://httpbin.org/anything
1 > Accept-Encoding: gzip,deflate
1 > Connection: Keep-Alive
1 > Content-Length: 40
1 > Content-Type: application/json; charset=UTF-8
1 > Host: httpbin.org
1 > User-Agent: Apache-HttpClient/4.5.11 (Java/1.8.0_231)
{"name":"[email protected]_1582424429663_1"}
© www.soinside.com 2019 - 2024. All rights reserved.