简体   繁体   中英

How to do softassertion with karate?

I have a feature that use other two features something like that:

When call read(ser.getCarList) headers
When call read(ser.getTaxes) headers

So the first feature getCarList has two validations

When method Get
 * configure continueOnStepFailure = true
Then status 200
And match response = read ('this:getCarAssertion')
 * configure continueOnStepFailure = true

I have tried with the new keyword but when I get a status code 200 but a bad response the next feature getTaxes does not continue in the execution

The continueOnStepFailure is a new keyword that was meant to be used when looking to validate results and not fail immediately on the first failure. The purpose was for assertions or validations so as much information can be validated when asserting results of tests.

To avoid its usage to be as a pure if condition for several steps (with unexpected consequences), the default behavior of * continueOnStepFailure = true will only continue the execute if the fails happen in a match step and once you disable the mechanism with * continueOnStepFailure = false the test will fail (but still provide details for each step within the continueOnStepFailure block). This is because match is the recommended keyword for any sort of validations and is how you can leverage the powerful JSON assertions library etc.

It is also recommended to also explicity set * continueOnStepFailure = false after the set of match keywords so there are no unexpected behaviors after that conscious decision of continuing to evaluate keywords after a failure.

That being said there are ways to extend and configure the behavior of the continueOnStepFailure beyond the default behavior. The keyword also takes a JSON input, instead of a boolean, that allows some more extensibility. Eg the default behavior of the keyword can be represented as follows:

* configure continueOnStepFailure = { enabled: true, continueAfter: false, keywords: ['match'] }

This means the continueOnStepFailure mechanism will be enabled , the scenario execution will not continue after the mechanism is disabled and it'll only accept failures if those happen in the match keyword. Note that if you set continueAfter to true the scenario will continue to execute the remaining steps but the scenario itself will still be marked as failed (with appropriate output in the report and typical failed behavior for any caller of that scenario). I highly discourage to set continueAfter to true.

For your specific use case, the status keyword is definitely within the boundaries of assertions that I've described. status 200 is just a shortcut for match responseStatus == 200 . Very likely we should add status to the default behavior, given that it's a match assertion. With the extended configuration in JSON you can do the following for your use-case:

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

Some additional examples can be found in the unit tests in this pull request . For quick reference, this is how your Karate Test report will look like:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM