hurl icon indicating copy to clipboard operation
hurl copied to clipboard

How to do if-then-else during testing

Open xu4wang opened this issue 1 year ago • 4 comments

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

xu4wang avatar Aug 24 '23 03:08 xu4wang

Supporting a skip option could work (#1815)

DELETE {{host}}/users/{{user_uuid}}
Authorization: Bearer {{access_token_admin}}
[Options]
skip: user_uuid not exists

fabricereix avatar Aug 31 '23 13:08 fabricereix

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!

mjhcorporate avatar Sep 20 '23 19:09 mjhcorporate

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

chrisguest75 avatar Oct 11 '23 10:10 chrisguest75

--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

jcamiel avatar Nov 06 '23 12:11 jcamiel