如何使用 Karate 为单个功能文件中的场景重用断言?
How to reuse assertions for scenarios in single feature file using Karate?
我想断言所有场景的responseTime。但我不想在每种情况下都重复断言代码。以下是我的功能文件:
Feature: Reqres api test cases
Background: base url
Given url base_url
Scenario: list single user get request
Given path single_user_path
When method get
Then status 200
And assert responseTime < 4000
Scenario: create user using post and inline json payload
* def path = '/users'
Given path path
And request {"name": "morpheus","job": "leader"}
When method post
Then status 201
And assert responseTime < 4000
在上面的代码块中,我想避免 responseTime 断言重复。如何在空手道中实现这一目标?请帮忙。
不,这不受支持,也没有计划。不太可能每个 API 调用都具有完全相同的 SLA。这也是 Gatling 集成的用途:
编辑作为如何“重用”响应断言的示例:
Feature:
Background:
* def validateResponse =
"""
function() {
var contentType = karate.get("responseHeaders['Content-Type'][0]");
if (contentType !== 'application/json') {
karate.fail('content type is not json');
}
var responseType = karate.get('responseType');
if (responseType !== 'json') {
karate.fail('response type is not json');
}
}
"""
Scenario:
* url 'https://httpbin.org/post'
* request { foo: 'bar' }
* method post
* validateResponse()
请注意,我绝对不推荐上述方法,原因最好在此处解释:
我想断言所有场景的responseTime。但我不想在每种情况下都重复断言代码。以下是我的功能文件:
Feature: Reqres api test cases
Background: base url
Given url base_url
Scenario: list single user get request
Given path single_user_path
When method get
Then status 200
And assert responseTime < 4000
Scenario: create user using post and inline json payload
* def path = '/users'
Given path path
And request {"name": "morpheus","job": "leader"}
When method post
Then status 201
And assert responseTime < 4000
在上面的代码块中,我想避免 responseTime 断言重复。如何在空手道中实现这一目标?请帮忙。
不,这不受支持,也没有计划。不太可能每个 API 调用都具有完全相同的 SLA。这也是 Gatling 集成的用途:
编辑作为如何“重用”响应断言的示例:
Feature:
Background:
* def validateResponse =
"""
function() {
var contentType = karate.get("responseHeaders['Content-Type'][0]");
if (contentType !== 'application/json') {
karate.fail('content type is not json');
}
var responseType = karate.get('responseType');
if (responseType !== 'json') {
karate.fail('response type is not json');
}
}
"""
Scenario:
* url 'https://httpbin.org/post'
* request { foo: 'bar' }
* method post
* validateResponse()
请注意,我绝对不推荐上述方法,原因最好在此处解释: