空手道:使用数据驱动的嵌入式模板方法进行API测试 [英] Karate: Using data-driven embedded template approach for API testing

查看:121
本文介绍了空手道:使用数据驱动的嵌入式模板方法进行API测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写通过从外部文件(csv)读取动态值的数据驱动测试. 可以通过csv为简单字符串传递动态值(下面的帐号和联盟ID).但是,如何使用嵌入式表达式从csv文件中为下面的"DealerReportFormats" json数组传递动态值?

I want to write data-driven tests passing dynamic values reading from external file (csv). Able to pass dynamic values from csv for simple strings (account number & affiliate id below). But, using embedded expressions, how can I pass dynamic values from csv file for "DealerReportFormats" json array below?

任何帮助都受到高度赞赏!

Any help is highly-appreciated!!

Scenario Outline: Dealer dynamic requests
    Given path '/dealer-reports/retrieval'
    And request read('../DealerTemplate.json')   
  When method POST
    Then status 200
    Examples: 
      | read('../DealerData.csv') | 


DealerTemplate.json is below
{    
  "DealerId": "FIXED",
  "DealerName": "FIXED",
  "DealerType": "FIXED",

  "DealerCredentials": {
    "accountNumber": "#(DealerCredentials_AccountNumber)",
    "affiliateId": "#(DealerCredentials_AffiliateId)"
  },
"DealerReportFormats": [
    {
      "name": "SalesReport",
      "format": "xml"
    },
    {
      "name": "CustomerReport",
      "format": "txt"
    }
  ]
}

DealerData.csv: 

DealerCredentials_AccountNumber,DealerCredentials_AffiliateId
testaccount1,123
testaccount2,12345
testaccount3,123456

推荐答案

CSV仅适用于扁平"结构,因此,老实说,尝试将其与JSON混合起来实在是太宏大了.如果需要,请寻找其他框架:)

CSV is only for "flat" structures, so trying to mix that with JSON is too ambitious in my honest opinion. Please look for another framework if needed :)

说我看到2个选项:

a)在CSV中使用正确的引号和转义符

a) use proper quoting and escaping in the CSV

b)参考JSON文件

b) refer to JSON files

这里是一个例子:

Scenario Outline:
* json foo = foo
* print foo

Examples:
| read('test.csv') |

test.csv是:

foo,bar
"{ a: 'a1', b: 'b1' }",test1
"{ a: 'a2', b: 'b2' }",test2

如果您想转义双引号,我将其留给您练习.有可能.

I leave it as an exercise to you if you want to escape double-quotes. It is possible.

选项(b)是您可以引用独立的JSON文件并读取它们:

Option (b) is you can refer to stand-alone JSON files and read them:

foo,bar
j1.json,test1
j2.json,test2

您可以在功能中执行* def foo = read(foo).

这篇关于空手道:使用数据驱动的嵌入式模板方法进行API测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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