代码之家  ›  专栏  ›  技术社区  ›  Peter Thomas

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

  •  0
  • Peter Thomas  · 技术社区  · 5 年前

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

    我试着写一个java互操作等等,但后来变得非常复杂。

    1 回复  |  直到 5 年前
        1
  •  0
  •   Peter Thomas    5 年前

    使用空手道其实很简单。首先我假设你有一个 index.json

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

    内容 ecg-01.json :

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

    ecg-02.json

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

    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
    

    当我运行这个示例时,它发出了如下两个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":"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互操作并不“那么复杂”。