Reputation: 147
How can I make a test fail using javascript?
As per documentation this should do:
* if(true) throw 'custom message'
but I'm getting this error:
findAuthDataByUserAndApp.feature:20 - javascript evaluation failed: if(true) throw 'custom message', custom message in <eval> at line number 1 at column number 9
(What I would like to do is conditionally match response code based on __arg in a reusable feature like)
* if(__arg.statusCode != null && responseStatus != __arg.statusCode) throw 'custom message
Thanks
Upvotes: 1
Views: 2512
Reputation: 58058
You can't, and the throw
is just a workaround for users who insist on some "extra" message in the log. The "error" is how it is designed currently, the test fails, it just "looks" like a JS error. We recommend other patterns.
* def failed = true
* if (failed) karate.log('custom message')
* assert !failed
If you insist, I can open a feature request for a karate.fail(condition, message)
method that would do the above. IMHO it shouldn't be used, because tests should ideally be deterministic.
EDIT: karate.fail('message')
will be available in 0.9.6 final
Upvotes: 0
Reputation: 147
I found an elegant solution for my original problem:
* def expectedStatusCode = req.statusCode || responseStatus
* match responseStatus == expectedStatusCode
(sometimes the solutions is just the simpler...)
Upvotes: 1