Using "Fail" keyword to stop test case execution

6,429 views
Skip to first unread message

Nesat Ufuk

unread,
May 26, 2016, 11:41:57 AM5/26/16
to robotframework-users
Hi robot folks,

I am trying to use Fail keyword to stop test case execution. But, I guess I am missing a part or something else.

Robot Code:
    Charge With Token  sale=${sale_uri}  payment_token=${payment_token}  sale_risk=${risk_uri}
   
# Charge With Token returns a list including http status codes as first element
   
Run Keyword If   @{returned}[0] != 201 or @{returned}[0] != 400   Fail   msg=Samething went wrong, please look at the log file

Herebelow is the trace:

Documentation:    
Runs the given keyword with the given arguments, if condition is true.
Start / End / Elapsed:    20160526 17:01:44.010 / 20160526 17:01:44.018 / 00:00:00.008
00:00:00.001KEYWORD BuiltIn . Fail Samething went wrong, please look at the log file
Documentation:    
Fails the test with the given message and optionally alters its tags.
Start / End / Elapsed:    20160526 17:01:44.017 / 20160526 17:01:44.018 / 00:00:00.001
17:01:44.017    TRACE    Arguments: [ 'Samething went wrong, please look at the log file' ]    
17:01:44.018    FAIL    Samething went wrong, please look at the log file    
17:01:44.018    DEBUG    Traceback (most recent call last):
 
None    
17:01:44.011    TRACE    Arguments: [ '400 != 201 or 400 != 400' | 'Fail' | 'Samething went wrong, please look at the log file' | 'ELSE' | 'Get Sale Info' | 'token=${sale_token}' ]

Sincerely,
Nesat

Kevin O.

unread,
May 26, 2016, 12:00:18 PM5/26/16
to robotframework-users
Fail does stop the execution of the test case (but still run teardown). If you want to stop execution completely, Fatal Error is the keyword you should use.

Nesat Ufuk

unread,
May 27, 2016, 4:54:46 AM5/27/16
to robotframework-users
Thanks Kevin, but I don't want to stop/Fail whole Test Suite execution with Fatal Error.

Actually, some different behaviour is occurring with Run Keyword If and Fail.

As you can see from logs. @{returned}[0] is coming as 400 but it was not able to verified with

17:01:44.011    TRACE    Arguments: [ '400 != 201 or 400 != 400' | 'Fail' | 'Samething went wrong, please look at the log file' | 'ELSE' | 'Get Sale Info' | 'token=${sale_token}' ]

or am I missing something different.

J Wipf

unread,
May 27, 2016, 6:53:30 AM5/27/16
to robotframe...@googlegroups.com
My assumption based on your logic of:

@{returned}[0] != 201 or @{returned}[0] != 400

Is that if the returned code is 400 or 201 you want it to not fail. The problem is in your logic:

((400 does not equal 201) or (400 does not equal 400)) resolves as follows:

((True) or (False)) == (True) which executes the keyword Fail.

I believe you want the logic to read with an and, such as:

@{returned}[0] != 201 and @{returned}[0] != 400

That way if the returned value is 201 or 400 the test will not execute the Fail keyword but if the value is anything else the Fail keyword executes.

Pekka Klärck

unread,
May 27, 2016, 7:38:19 AM5/27/16
to j.wi...@gmail.com, robotframework-users

Yes, the original expression can never be true. Another way to write it would be `@{returned}[0] not in [201, 400]`. Additionally, you could simply use Should Be True keyword instead of Run Keyword If in combination with Fail.

Sent from my mobile.

27.5.2016 1.53 ip. "J Wipf" <j.wi...@gmail.com> kirjoitti:
My assumption based on your logic of:

@{returned}[0] != 201 or @{returned}[0] != 400

Is that if the returned code is 400 or 201 you want it to not fail. The problem is in your logic:

((400 does not equal 201) or (400 does not equal 400)) resolves as follows:

((True) or (False)) == (True) which executes the keyword Fail.

I believe you want the logic to read with an and, such as:

@{returned}[0] != 201 and @{returned}[0] != 400

That way if the returned value is 201 or 400 the test will not execute the Fail keyword if the returned value is 201 or 400 but if the value is anything else the keyword executes.



On Thursday, May 26, 2016 at 10:41:57 AM UTC-5, Nesat Ufuk wrote:
Hi robot folks,

I am trying to use Fail keyword to stop test case execution. But, I guess I am missing a part or something else.

Robot Code:
    Charge With Token  sale=${sale_uri}  payment_token=${payment_token}  sale_risk=${risk_uri}
   
# Charge With Token returns a list including http status codes as first element
   
Run Keyword If   @{returned}[0] != 201 or @{returned}[0] != 400   Fail   msg=Samething went wrong, please look at the log file

Herebelow is the trace:

Documentation:    
Runs the given keyword with the given arguments, if condition is true.
Start / End / Elapsed:    20160526 17:01:44.010 / 20160526 17:01:44.018 / 00:00:00.008
00:00:00.001KEYWORD BuiltIn . Fail Samething went wrong, please look at the log file
Documentation:    
Fails the test with the given message and optionally alters its tags.
Start / End / Elapsed:    20160526 17:01:44.017 / 20160526 17:01:44.018 / 00:00:00.001
17:01:44.017    TRACE    Arguments: [ 'Samething went wrong, please look at the log file' ]    
17:01:44.018    FAIL    Samething went wrong, please look at the log file    
17:01:44.018    DEBUG    Traceback (most recent call last):
 
None    
17:01:44.011    TRACE    Arguments: [ '400 != 201 or 400 != 400' | 'Fail' | 'Samething went wrong, please look at the log file' | 'ELSE' | 'Get Sale Info' | 'token=${sale_token}' ]

Sincerely,
Nesat

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at https://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

Nesat Ufuk

unread,
May 27, 2016, 8:39:22 AM5/27/16
to robotframework-users
Oh, my bad.
Thank you guys, to help correct my logic.

BTW, Pekka I didn't get the Should Be True keyword method. Can you please elaborate please.

Thanks again.


On Thursday, 26 May 2016 18:41:57 UTC+3, Nesat Ufuk wrote:

Pekka Klärck

unread,
May 27, 2016, 8:45:37 AM5/27/16
to Nesat Ufuk, robotframework-users
2016-05-27 15:39 GMT+03:00 Nesat Ufuk <nes...@gmail.com>:
>
> BTW, Pekka I didn't get the Should Be True keyword method. Can you please
> elaborate please.

You can use `| Should Be True | expr | msg |` instead of `|Run Keyowrd
If | expr | Fail | msg |`. See the docs for more details about the
usage:
http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Should%20Be%20True

J Wipf

unread,
May 27, 2016, 9:03:17 AM5/27/16
to robotframe...@googlegroups.com, nes...@gmail.com
That's a very elegant solution. We use some run keyword if's with fail ourselves but it would be much easier to use should be true since the fail path of the if is built in and then the positive of the if is another line. Much easier to read and thus maintain in code, I like it!

Pekka Klärck

unread,
May 27, 2016, 9:50:37 AM5/27/16
to J Wipf, robotframework-users, Nesat Ufuk
2016-05-27 16:03 GMT+03:00 J Wipf <j.wi...@gmail.com>:
> That's a very elegant solution. We use a some run keyword if's with fail
> ourselves but it would be much easier to use should be true since the fail
> path of the if is built in and then the positive of the if is another line.
> Much easier to read and thus maintain in code, I like it!

I've actually noticed others using Run Keyword If with Fail like:

Run Keyword If expression Fail message

I guess the reason is that people are so used to typical using `if` in
programming like:

if expression:
raise AssertionError(message)

With Robot Framework you generally should use a keyword directly, and
in this particular case Should Be True works nicely:

Should Be True expression message

Cheers,
.peke
--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org

Nesat Ufuk

unread,
May 30, 2016, 5:59:35 AM5/30/16
to robotframework-users
Thanks Pekka for the clarification.

Regards


On Thursday, 26 May 2016 18:41:57 UTC+3, Nesat Ufuk wrote:
Reply all
Reply to author
Forward
0 new messages