复杂的数据驱动测试,读取多个文件 [英] Complex data-driven test reading multiple files

查看:105
本文介绍了复杂的数据驱动测试,读取多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have one example where i was not able to achieve my problem with Karate. I have 25 ecg files where I should upload them to a patient. There's an index file which contains the right order of those files. But before uploading via API, I need read that index file, find the next file to upload.

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

Also i need to rename those file with a specific naming convention cause every file i upload via api should.be unique. The name consist of patient email, a timestamp and an index. Also timestamp in the name should be increased for each file with 25500ms. After the upload, i should delete those files.

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

I tried to write a java interop,etc but then it became so complicated.

推荐答案

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

This is actually quite simple using Karate. First I assume you have an index.json with the following:

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

ecg-01.json的内容:

{ "email": "john@ecg.com" }

ecg-02.json的内容:

{ "email": "smith@ecg.com" }  

请注意,在空手道中,您无需保存临时文件即可上传文件,您可以获取现有文件并执行不同名称的上传,请参考:

Note that in Karate you don't need to save temporary files for file-upload, you can take an existing file and perform an upload with a different name, refer: https://github.com/intuit/karate#multipart-file

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

We will do the main work in upload.feature, here we just use https://httpbin.org/ as a demo endpoint:

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

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

So finally all you need to do is a loop from your main feature, which can be as simple as this:

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

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

When I ran this example, it made 2 POST requests like this:

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":"john@ecg.com_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":"smith@ecg.com_1582424429663_1"}

也就是说,假设您想将文件创建逻辑编写为纯Java.然后,您可以在一行中将其传递给空手道,而在另一行中将其删除.不,空手道中的Java互操作不是那么复杂".

That said, suppose you wanted to write the file creation logic as pure Java. You could have then passed it to Karate in one line, and deleted it in another line. No, Java interop in Karate is not "so complicated".

这篇关于复杂的数据驱动测试,读取多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆