You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 11:17:07