[英]How to compare 2 JSON objects that contains array using Karate tool and feature files
场景文件
标题更新请求.json
{id: 12, name: 'Old Hello', config:[{username: 'qwe', password: 'tyu'},{username: 'abc', password: 'xyz'}]}
标题更新响应.json
{id: 12, name: 'New Hello', config:[{username: 'qwe', password: 'tyu'},{username: 'abc', password: 'xyz'}]}
标题更新错误请求.json
{id: 00, name: 'Old Hello', config:[{username: 'qwe', password: 'tyu'},{username: 'abc', password: 'xyz'}]}
标题更新错误响应.json
{Error: 'not found', Message: 'The provided Book is not found.'}
图书记录功能
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 并且响应 == _arg.ExpectedResponse 不适用于场景:1?
这是典型的过度设计测试。 如果有人告诉你测试需要“重复使用”,请不要听那个人的。
你有两种情况,一种是快乐的路径,一种是消极的路径。 我在下面提供了您应该如何写出负面路径,其余的取决于您。
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.'}
看看它有多干净? 在测试中不需要“极端”重用。 如果您仍然坚持想要一个能够处理所有边缘情况的超通用可重用功能文件,那么您只是在给自己带来麻烦。 看看你现有的测试变得多么不可读!!
编辑:由于我经常将这个问题作为如何不编写测试的示例,因此我想使我的观点更清楚并添加几个链接以供参考。
有时在测试中“重复自己”是可以的。 测试不必是DRY 。 Karate 是一种DSL ,使您能够在一两行中进行 HTTP 调用或 JSON 操作。 当您开始尝试像这样“重复使用”时,实际上弊大于利。 例如,您现在需要查看多个文件以了解您的测试在做什么。
如果你不相信我,也许你会相信谷歌: https : //testing.googleblog.com/2019/12/testing-on-toilet-tests-too-dry-make.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.