如何用空手道做软断言? [英] How to do softassertion with karate?
问题描述
我有一个功能,它使用了另外两个类似的功能:
When call read(ser.getCarList) headers
When call read(ser.getTaxes) headers
因此第一个功能getCarList有两个验证
When method Get
* configure continueOnStepFailure = true
Then status 200
And match response = read ('this:getCarAssertion')
* configure continueOnStepFailure = true
我已尝试使用new关键字,但当收到状态代码200但响应不佳时,下一个功能getTaxes不会继续执行
推荐答案
continueOnStepFailure
是一个新关键字,用于验证结果,并且不会在第一次失败时立即失败。其目的是用于断言或验证,以便在断言测试结果时可以验证更多的信息。
* continueOnStepFailure = true
用作几个步骤的纯IF条件(具有意外后果),* continueOnStepFailure = true
的默认行为仅在match
步骤中失败时才会继续执行,并且一旦使用* continueOnStepFailure = false
禁用该机制,测试将失败(但仍会提供continueOnStepFailure
块中每个步骤的详细信息)。这是因为match
是任何类型验证的推荐关键字,也是您如何利用强大的JSON断言库等的方式。
还建议在match
关键字集合之后明确设置* continueOnStepFailure = false
,以便在故障后有意识地决定继续评估关键字后不会有意外行为。
continueOnStepFailure
的行为。关键字还接受JSON输入,而不是布尔值,这允许更多的可扩展性。例如,关键字的默认行为可以表示为:
* configure continueOnStepFailure = { enabled: true, continueAfter: false, keywords: ['match'] }
这意味着continueOnStepFailure
机制将被启用,方案执行将在该机制被禁用后不继续,并且它将只接受在match
关键字中发生的失败。请注意,如果您将continueAfter
设置为True,则场景将继续执行其余步骤,但场景本身仍将标记为失败(在报告中有适当的输出,并且该场景的任何调用方的典型失败行为)。我极力反对将continueAfter
设置为True。
对于您的特定用例,status
关键字绝对在我所描述的断言的边界内。status 200
只是match responseStatus == 200
的快捷方式。我们很可能应该将状态添加到默认行为中,因为它是匹配断言。使用JSON中的扩展配置,您可以针对您的用例执行以下操作:
When method Get
And configure continueOnStepFailure = { enabled: true, continueAfter: false, keywords: ['match', 'status'] }
Then status 200
And match response = read ('this:getCarAssertion')
And configure continueOnStepFailure = false
在单元测试in this pull request中可以找到其他一些示例。为了快速参考,以下是您的空手道测试报告:
这篇关于如何用空手道做软断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!