如何使用空手道工具和特征文件比较包含数组的 2 个 JSON 对象 [英] How to compare 2 JSON objects that contains array using Karate tool and feature files

查看:26
本文介绍了如何使用空手道工具和特征文件比较包含数组的 2 个 JSON 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景文件

  • 所有文件都在同一目录中.

title-update-request.json

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

title-update-response.json

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

title-update-error-request.json

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

title-update-error-response.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 }

更新功能

功能:更新图书记录.

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

场景的实际 API 响应:1 是:

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

场景的实际 API 响应:2 是:

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

问题: 我应该如何验证 update.feature 文件中的响应,因为问题是如果我使用 #^^config 进行更改,这不适用于场景 :2 和 response == _arg.ExpectedResponse 不适用于场景:1?

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?

推荐答案

这是典型的过度设计测试.如果有人告诉你重复使用"测试需要,请不要听那个人的.

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 !!

由于我经常将这个问题作为如何不编写测试的示例,因此我想使我的观点更清楚并添加几个链接以供参考.

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.

有时重复自己"是可以的;在测试中.测试不必DRY.Karate 是一个 DSL,它使您能够在一两行中进行 HTTP 调用或 JSON 操作.当您开始尝试重用"时像这样,实际上弊大于利.例如,您现在需要查看多个文件以了解您的测试在做什么.

Sometimes it is okay to "repeat yourself" in tests. Tests don't have to be DRY. Karate is a DSL that enables you to make HTTP calls or JSON manipulation in one or two lines. 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.

如果你不相信我,也许你会相信谷歌:https://testing.googleblog.com/2019/12/testing-on-toilet-tests-too-dry-make.html

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

这篇关于如何使用空手道工具和特征文件比较包含数组的 2 个 JSON 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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