Karate中能否用变量替代数字响应码验证接口请求状态?
在Karate中使用变量验证响应状态码
完全可以实现,你的写法问题在于Then status responseCode会被Karate当作字符串字面量处理,而非读取变量值。以下是两种正确的实现方式:
方式一:用括号包裹变量
通过括号让Karate解析变量表达式:
Scenario: test API * def responseCode = 200 Given path 'api/v1/testsample/' When method POST Then status (responseCode)
方式二:使用match语句验证
利用Karate内置的responseStatus变量(对应响应状态码),结合match做变量比较:
Scenario: test API * def responseCode = 200 Given path 'api/v1/testsample/' When method POST * match responseStatus == responseCode
这两种方式都能正确读取responseCode变量的值,完成响应状态码的验证。
内容的提问来源于stack exchange,提问作者vinu v nair
相关产品推荐
相关产品推荐

