You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow raising ReturnFromKeyword in python keywords that are called from tests #4038
I want to be able to return early from a python keyword specifying a return value but also specifying test failures.
Looks like raising a ReturnFromKeyword is exactly what I want, but that only works when a robot keyword is in the stack, not from a python keyword that is called directly from a test case.
*** Test Cases ***with a wrapped call${it} = wrapped
log ${it}called directly${it} = fail_and_return
log ${it}*** keywords ***wrapped
fail_and_return
Could you clarify the use case for this issue? I think we actually did support returning from user keywords with custom exceptions but removed that support because it was strange that Python keywords could control execution like that.
Notice also that we are planning to enhance returning from keywords by introducing separate RETURN statement (#4078).
Hi @pekkaklarck, I have a need to report failures and inner keywords from within python code heres an example:
defsome_kw() ->int:
withkeyword("outer kw", allow_fails=True): # in the robot log will report as a keyword# allow_fails means that test assertions will fail the test but the keyword will continuewithkeyword("inner kw"): # in the robot log will report as a keywordrf.eq(1, 2) # in the robot log will fail 'inner kw', 'outer kw' and 'some kw' but will not raise an exceptionlogger.info("wow") # will be executedlogger.info("end of some kw") # will be executedreturn10# will be returned
I achieve all this with a mess of gross code that would be much simplified if I could use ReturnFromKeyword.
Currently I'm not able to return a value and the failure at the same time, ReturnFromKeyword would enable that.
Do I get it right that your real use case is executing things on Python so that you get keywords visible in log? That at least seems to be the underlying need in #3706 and this issue. That's something I've seen others wanting as well, especially in RPA context, and I guess something we could look at in the future. Having an API on Robot for that would be nice and then would probably remove the need for issues like this.
The problem obviously is how that kind of an API should look like. Context manager would probably be convenient, but do to use already having @keyword decorator it probably should be named keyword_call. Should then also think would be need context managers also for constructing suites and tests the same way. Looking at pytest integration would probably be a good idea as well. There's lot of API design involved and unfortunately I won't have time for that in RF 5.0 timeline. If someone else is interested to design this, I'd be happy to review the design and help with implementation, though. Possibly this could be an external project in the beginning as well.
The example above has these two lines that I don't understand:
rf.eq(1, 2) # in the robot log will fail 'inner kw', 'outer kw' and 'some kw' but will not raise an exceptionlogger.info("wow") # will be executed
I just don't see why you want this kind of behavior. That was already requested in #3706 and I didn't understand it then either. This is definitely something where I think try/except or separate with wrapping the code that can fail and you want to silence works much better. I don't actually even now how something like this could be implemented.
Like with #3706, my current thinking is that adding the functionality requested in this issue isn't a good idea. That said, an API that allows Python based keywords to created nested keywords in the log file sounds like a useful enhancement. My thinking is that a separate issue should be submitted about that and this and #3706 be closed.
for example when I am validating 10 things on a page I want to be able to write the 10 assertions without any boilerplate ( in my implementation allow_fails only applies to a TestAssertionError type that I have )
withkeyword("validate all the things on this page", allow_fails=True):
eq(a, b)
eq(c, d)
eq(e, f)
Such that each assertion will fail the test but in a continuable way
Instead of:
withkeyword("validate all the things on this page"):
try:
eq(a, b)
exceptAssertionErroraserr:
log_failure(e)
try:
eq(c, d)
exceptAssertionErroraserr:
log_failure(e)
try:
eq(e, f)
exceptAssertionErroraserr:
log_failure(e)
Activity
pekkaklarck commentedon Sep 13, 2021
Could you clarify the use case for this issue? I think we actually did support returning from user keywords with custom exceptions but removed that support because it was strange that Python keywords could control execution like that.
Notice also that we are planning to enhance returning from keywords by introducing separate
RETURNstatement (#4078).KotlinIsland commentedon Sep 14, 2021
Hi @pekkaklarck, I have a need to report failures and inner keywords from within python code heres an example:
I achieve all this with a mess of gross code that would be much simplified if I could use
ReturnFromKeyword.Currently I'm not able to return a value and the failure at the same time,
ReturnFromKeywordwould enable that.KotlinIsland commentedon Sep 14, 2021
Related to #3706
pekkaklarck commentedon Sep 14, 2021
Do I get it right that your real use case is executing things on Python so that you get keywords visible in log? That at least seems to be the underlying need in #3706 and this issue. That's something I've seen others wanting as well, especially in RPA context, and I guess something we could look at in the future. Having an API on Robot for that would be nice and then would probably remove the need for issues like this.
The problem obviously is how that kind of an API should look like. Context manager would probably be convenient, but do to use already having
@keyworddecorator it probably should be namedkeyword_call. Should then also think would be need context managers also for constructing suites and tests the same way. Looking at pytest integration would probably be a good idea as well. There's lot of API design involved and unfortunately I won't have time for that in RF 5.0 timeline. If someone else is interested to design this, I'd be happy to review the design and help with implementation, though. Possibly this could be an external project in the beginning as well.pekkaklarck commentedon Sep 14, 2021
The example above has these two lines that I don't understand:
I just don't see why you want this kind of behavior. That was already requested in #3706 and I didn't understand it then either. This is definitely something where I think
try/exceptor separatewithwrapping the code that can fail and you want to silence works much better. I don't actually even now how something like this could be implemented.pekkaklarck commentedon Sep 14, 2021
Like with #3706, my current thinking is that adding the functionality requested in this issue isn't a good idea. That said, an API that allows Python based keywords to created nested keywords in the log file sounds like a useful enhancement. My thinking is that a separate issue should be submitted about that and this and #3706 be closed.
KotlinIsland commentedon Sep 15, 2021
for example when I am validating 10 things on a page I want to be able to write the 10 assertions without any boilerplate ( in my implementation allow_fails only applies to a
TestAssertionErrortype that I have )Such that each assertion will fail the test but in a continuable way
Instead of: