简体   繁体   English

如何使用空手道工具和特征文件比较包含数组的 2 个 JSON 对象

[英]How to compare 2 JSON objects that contains array using Karate tool and feature files

Files for the scenario场景文件

  • All the files are on same directory.所有文件都在同一目录中。

title-update-request.json标题更新请求.json

{id: 12, name: 'Old Hello', config:[{username: 'qwe', password: 'tyu'},{username: 'abc', password: 'xyz'}]}

title-update-response.json标题更新响应.json

{id: 12, name: 'New Hello', config:[{username: 'qwe', password: 'tyu'},{username: 'abc', password: 'xyz'}]}

title-update-error-request.json标题更新错误请求.json

{id: 00, name: 'Old Hello', config:[{username: 'qwe', password: 'tyu'},{username: 'abc', password: 'xyz'}]}

title-update-error-response.json标题更新错误响应.json

{Error: 'not found', Message: 'The provided Book is not found.'}

book-record.feature图书记录功能

Feature: CRUD operation on the book records.

Background:
        * def signIn = call read('classpath:login.feature')
        * def accessToken = signIn.accessToken
        * url baseUrl

 Scenario: Change title of book in the single book-record.
    * json ExpResObject = read('classpath:/book-records/title-update-response.json')
    * json ReqObject = read('classpath:/book-records/title-update-request.json')
    * call read('classpath:/book-records/update.feature') { Token: #(accessToken), ReqObj: #(ReqObject), ResObj: #(ExpResObject), StatusCode: 200 }

  Scenario: Change title of book in the non-existing book-record.
    * json ExpResObject = read('classpath:/book-records/title-update-error-request.json')
    * json ReqObject = read('classpath:/book-records/title-update-error-response.json')
    * call read('classpath:/book-records/update.feature') { Token: #(accessToken), ReqObj: #(ReqObject), ResObj: #(ExpResObject), StatusCode: 400 }

update.feature更新功能

Feature: Update the book record.功能:更新图书记录。

Scenario: Update single book-record.
    Given path '/book-record'
    And header Authorization = 'Bearer ' + __arg.Token
    And header Content-Type = 'application/json'
    And request __arg.ReqObj
    When method put
    Then status __arg.StatusCode
    And response == __arg.ExpectedResponse

Actual API response for scenario: 1 is :场景的实际 API 响应:1 是:

{name: 'New Hello', config:[{username: 'abc', password: 'xyz'},{username: 'qwe', password: 'tyu'}]}

Actual API response for scenario: 2 is :场景的实际 API 响应:2 是:

 {Error: 'not found', Message: 'The provided Book is not found.'}

Question: How should I validate the response in update.feature file since problem is if I make change s as using #^^config that will not works for scenario :2 and response == _arg.ExpectedResponse is not working for Scenario: 1?问题:我应该如何验证 update.feature 文件中的响应,因为问题是如果我使用 #^^config 进行更改,这将不适用于场景:2 并且响应 == _arg.ExpectedResponse 不适用于场景:1?

This is classic over-engineering of tests.这是典型的过度设计测试。 If someone has told you that "re-use" is needed for tests, please don't listen to that person.如果有人告诉你测试需要“重复使用”,请不要听那个人的。

You have two scenarios, one happy path and one negative path.你有两种情况,一种是快乐的路径,一种是消极的路径。 I am providing how you should write the negative path here below, the rest is up to you.我在下面提供了您应该如何写出负面路径,其余的取决于您。

Scenario: Change title of book in the non-existing book-record.
Given path 'book-record'
And header Authorization = 'Bearer ' + accessToken
And request {id: 00, name: 'Old Hello', config:[{username: 'qwe', password: 'tyu'},{username: 'abc', password: 'xyz'}]}
When method put
Then status 400
And response == {Error: 'not found', Message: 'The provided Book is not found.'} 

See how clean it is ?看看它有多干净? There is no need for "extreme" re-use in tests.在测试中不需要“极端”重用。 If you still insist that you want a super-generic re-usable feature file that will handle ALL your edge cases, you are just causing trouble for yourself.如果您仍然坚持想要一个能够处理所有边缘情况的超通用可重用功能文件,那么您只是在给自己带来麻烦。 See how un-readable your existing tests have become !!看看你现有的测试变得多么不可读!!

EDIT: Since I refer the question to others often as an example of how NOT to write tests, I wanted to make my point more clear and add a couple of links for reference.编辑:由于我经常将这个问题作为如何不编写测试的示例,因此我想使我的观点更清楚并添加几个链接以供参考。

Sometimes it is okay to "repeat yourself" in tests.有时在测试中“重复自己”是可以的。 Tests don't have to be DRY .测试不必是DRY Karate is a DSL that enables you to make HTTP calls or JSON manipulation in one or two lines. Karate 是一种DSL ,使您能够在一两行中进行 HTTP 调用或 JSON 操作。 When you start attempting "re-use" like this, it actually leads to more harm than good.当您开始尝试像这样“重复使用”时,实际上弊大于利。 For example, you now need to look at multiple files to understand what your test is doing.例如,您现在需要查看多个文件以了解您的测试在做什么。

If you don't believe me, maybe you will believe Google: https://testing.googleblog.com/2019/12/testing-on-toilet-tests-too-dry-make.html如果你不相信我,也许你会相信谷歌: https : //testing.googleblog.com/2019/12/testing-on-toilet-tests-too-dry-make.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM