KarateAPI中有类似POJO的功能吗? [英] Having a POJO like feature in KarateAPI?
问题描述
一段时间以来,我一直在使用空手道和RestAssured.当然,这两种工具都有其优点和缺点.现在,我有一个RestAssured项目,其中有请求和响应"对象以及POJO.我的请求包装了我的端点,并将我的POJO发送到那些端点.我在抽象层中完成所有Header等配置.如果需要覆盖它们,则在测试期间覆盖它们.如果没有,那么我需要两行代码来触发端点.
I have been using Karate and RestAssured for sometime. There are advantages and downside of both tools of course. Right now I have a RestAssured project where I have Request and Response object and POJOs. My requests wraps my endpoint and send my POJOs to those endpoint. I do all my Headers, etc configuration in an abstract layer. In case I need to override them, I override them during the test. If not, Its a two lines of code for me to trigger an endpoint.
我使用edpoint的快乐路径和负路径的方式是,在每次测试之前,我都使用构造函数中的新值初始化POJO.然后,我将覆盖测试范围中想要的值.例如,如果要测试否定的密码字段,请在测试过程中将此字段设置为空字符串.但是在测试之前,其他字段已经设置为一些随机的东西.
My way of working with happy path and negative path of an edpoint is that I initialize the POJO before every test with new values in the constructor. Then I override the value that I want in test scope. For example, if I want to test a negative case for password field, I set this field to empty string during the test. But other fields are already set to some random stuff before the test.
但是我不知道如何用空手道来做到这一点.
But I dont know how to achieve this with Karate.
空手道允许我创建请求正文的JSON表示并定义我的参数,如下面的示例所示.
Karate allows me to create a JSON representation of my request body and define my parameters as seen below example.
{
"firstName": "<name>",
"lastName": "<lastName>",
"email": "<email>",
"role": <role>
}
然后在每次测试中,我都必须用一些数据填充所有字段.
Then in every test I have to fill all the fields with some data.
|token |value|
|name |'canberk'|
|lastName |''|
|email |'canberk@blbabla.com'|
|role |'1'|
和
|token |value|
|name |''|
|lastName |'akduygu'|
|email |'canberk@blbabla.com'|
|role |'1'|
它是这样的.
使用4个字段的JSON正文是可以的,但是当该正文开始具有20多个字段时,很难为每个测试初始化每个字段.
It's ok with a 4 fields JSON body but when the body starts to have more than 20 fields, it become a pain to initialise every field for every test.
空手道是否可以通过预定义的步骤来解决此问题,我需要提出解决方案?
Does Karate have a way of achieving this problem with a predefined steps of I need to come up with a solution?
推荐答案
这两种工具都有优点和缺点.
There are advantages and downside of both tools of course.
我肯定有偏见,但是恕我直言,与REST相比,空手道的唯一缺点是您没有获得编译时安全性:)我希望您已经看过
I'm definitely biased, but IMHO the only disadvantage of Karate compared to REST-assured is that you don't get compile time safety :) I hope that you have seen this comparison.
空手道有多种方法来做您想做的事.这就是我要做的.
Karate has multiple ways to do what you want. Here's what I would do.
- 创建一个设置了所有快乐路径"值的JSON文件
- 使用
read()
语法加载文件(这意味着该文件可在多个测试中重复使用) - 使用
set
关键字仅更新您的方案或否定测试的字段
- create a JSON file that has all your "happy path" values set
- use the
read()
syntax to load the file (which means this is re-usable across multiple tests) - use the
set
keyword to update only the field for your scenario or negative test
如果使用嵌入式表达式,您将获得更多的乐趣.
You can get even more fancier if you use embedded expressions.
- 创建一个设置了所有快乐路径"值的JSON文件,您想要更改的值看起来像
foo: '##(foo)'
- 在使用
read()
之前,您需要初始化一些变量,例如* def foo = 'bar'
,如果您使用null
,则该JSON密钥甚至会从JSON中删除 -
read()
JSON.准备使用了!
- create a JSON file that has all your "happy path" values set and the values you want to vary look like
foo: '##(foo)'
- before using
read()
you init some variables for e.g.* def foo = 'bar'
and if you usenull
that JSON key will even be removed from the JSON read()
the JSON. it is ready for use !
您可以参考此文件,其中演示了XML的一些概念,您可能会得到更多的想法:
You can refer to this file that demonstrates some of these concepts for XML, and you may get more ideas: xml.feature
这篇关于KarateAPI中有类似POJO的功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!