hurl
hurl copied to clipboard
How to do if-then-else during testing
Hi Greetings,
I'm testing some APIs. sometimes, I need to call an API based on the response of the previous API. For example, in below hurl file, if the user with user_email is not available, I want to skip the DELETE entry.
GET {{host}}/users
Authorization: Bearer {{access_token_admin}}
[QueryStringParams]
filter[email][_eq]:{{user_email}}
HTTP 200
[Captures]
user_uuid: jsonpath "$.data[0].id"
DELETE {{host}}/users/{{user_uuid}}
Authorization: Bearer {{access_token_admin}}
For now, if I run above hurl file, it complains
austin@dev testing % hurl --variables-file dev.env ./tests/1.user.hurl
error: No query result
--> ./tests/1.user.hurl:54:13
|
54 | user_uuid: jsonpath "$.data[0].id"
| ^^^^^^^^^^^^^^^^^^^^^^^ The query didn't return any result
|
I am not sure if I can do this in hurl, or is there a good practice doing this?
BR,Austin
Supporting a skip
option could work (#1815)
DELETE {{host}}/users/{{user_uuid}}
Authorization: Bearer {{access_token_admin}}
[Options]
skip: user_uuid not exists
In the Postman world, this is solved with something that boils down to GOTO <requestName>
. The semantics aren't a great fit to hurl (which does not have request names), and programming with GOTOs is messy, but it allows if/else, do/while, and (by using dynamic variables decremented with test-javascript) for-loops.
A skip
would be less powerful -- but also less confusing!
This is the current challenge with hurl for me. I love the simplified DSL approach to scripting tests. But we're missing features to enable certain types of testing. Simple conditional logic or branching, full persisted captures for analysis & interrogation and hooks for cleanup.
Maybe people have a few tricks to achieve the above. I'd be interested to hear if they do
--skip
option has been implemented in #1815 that enable request skipping:
GET https://bar.com
[Options]
skip: true
HTTP 200
GET https://baz.com
HTTP 200