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
Support method of failing current keyword/test and continuing in python #3706
I have a Python function keyword described as A context manager that runs as a Robot keyword that supports continue on error.
@keyworddeftest(): # will show as passed in logwithkeyword("will fail", continue_on_error=True): # will show as failed in log and will fail the testBuiltIn().fail("foo")
logger.info("bar") # will run
But the report looks janky because it doesn't make test failed and the error needs to be raised in cleanup.
I guess it boils down to: I want a pure python implementation of "Run Keyword And Continue On Failure"
Here's a rough implementation of keyword:
fromcontextlibimportcontextmanagerfromrobot.libraries.BuiltInimportBuiltInfromrobot.running.statusreporterimportStatusReporterexception_store=None@contextmanagerdefkeyword(name, continue_on_error=False):
"""A context manager that runs as a Robot keyword continue_on_error: if an error occurs in this keyword, exit and report as failed but continue test """try:
withStatusReporter(BuiltIn()._context, Keyword(kwname=name)):
yieldexceptExceptionaserr:
ifcontinue_on_error:
globalexception_storeexception_store=errreturnraiseerrdefcleanup():
ifexception_store:
raiseexception_store
First of all, sorry for missing this issue earlier. Could you @KotlinIsland please explain what you try to accomplish? If you want a pure-Python Run Keyword And Continue On Failure, why isn't using BuiltIn.run_keyword_and_continue_on_failure() enough? Alternatively your libraries could use continuable failures as @bhirsz already mentioned. Starting from RF 4.0 you can simply import ContinuableFailure from robot.api (#3685).
I want nested keywords within python code where the report looks nice and the python looks nice. I also want to be able to raise assertion errors within those keywords that will fail the keyword, the keyword will exit early, but will not exit anything else, and will make the entire testcase fail.
I want this because it makes the report and code logically structured and look similar.
is totally out of scope of RF. I guess it could be possible with some Python magic but not something for us.
I'm still not sure what the latter example is supposed to do. We do have @keyword decorator but you need to explain what you try to do with with keyword(...).
@pekkaklarck So I guess the last piece of what I need is to add a wrapper to every user keyword. Is there a way I can hook the test library import process and wrap all the functions that get imported?
I still don't fully understand what you try to accomplish. Based on the above and newer #4038 it seems like you want to run Python code so that you get keywords created in the log file. As I just commented #4038, we could have an API for that purpose but someone needed to design (and implement it).
I cannot comment the example above much without trying it out myself and I don't have time for that. What I can say is that you are using some internal APIs (e.g. StatusReporter) that aren't guaranteed to be stable between major releases. An official stable and public API for this purpose would thus be a lot better.
It seems to me that what this issue originally asks i.e.
Support method of failing current keyword/test and continuing in python
isn't in out scope. Having an API to create keywords that are shown in the log file would probably be a good idea, though, and a separate issue should be submitted about that. Or did I miss something here?
This issue proposes adding new context manager that would create nested keywords under a library keyword exactly like #4252 proposes. I thought that was the main point.
Activity
KotlinIsland commentedon Oct 8, 2020
bhirsz commentedon Jan 8, 2021
Did you think about using custom exception?
There are also ROBOT_EXIT_ON_FAILURE and ROBOT_SUPPRESS_NAME attributes - check them out from UserGuide.
pekkaklarck commentedon Feb 26, 2021
First of all, sorry for missing this issue earlier. Could you @KotlinIsland please explain what you try to accomplish? If you want a pure-Python
Run Keyword And Continue On Failure, why isn't usingBuiltIn.run_keyword_and_continue_on_failure()enough? Alternatively your libraries could use continuable failures as @bhirsz already mentioned. Starting from RF 4.0 you can simply importContinuableFailurefromrobot.api(#3685).KotlinIsland commentedon Mar 18, 2021
@pekkaklarck
Run Keyword And Continue On Failuredoesn't continue the python function:What I want
I want to be able to write:
Currently my keyword function produces the following report:

Issues with this:
pekkaklarck commentedon Mar 18, 2021
It's certainly true that in the first example i.e. in
the last line won't be run. I don't know what we could do with that nor why you just don't use
try/exceptinstead.The second example I don't understand at all.
KotlinIsland commentedon Mar 18, 2021
I can't use
try/exceptbecause then the test would pass, that would involve wrapping everything with boilerplate eg:I want nested keywords within python code where the report looks nice and the python looks nice. I also want to be able to raise assertion errors within those keywords that will fail the keyword, the keyword will exit early, but will not exit anything else, and will make the entire testcase fail.
I want this because it makes the report and code logically structured and look similar.
Currently I would have to introduce boilerplate to achieve what I want.
pekkaklarck commentedon Mar 19, 2021
Supporting something like
is totally out of scope of RF. I guess it could be possible with some Python magic but not something for us.
I'm still not sure what the latter example is supposed to do. We do have
@keyworddecorator but you need to explain what you try to do withwith keyword(...).KotlinIsland commentedon Mar 19, 2021
Here is my keyword function:
This is how it's used:

The issue is that I'm not sure how to make
outer,Foo, and the test fail.So currently what I do is store a variable saying that the test should fail and raise it in cleanup.
KotlinIsland commentedon Mar 19, 2021
This works correctly and fails the test as I want, the only downside is that I need to put this boilerplate on every function
KotlinIsland commentedon Mar 21, 2021
@pekkaklarck So I guess the last piece of what I need is to add a wrapper to every user keyword. Is there a way I can hook the test library import process and wrap all the functions that get imported?
KotlinIsland commentedon May 26, 2021
@pekkaklarck I think I've found a way to get the desired outcome, does this look right to you?
pekkaklarck commentedon Sep 14, 2021
I still don't fully understand what you try to accomplish. Based on the above and newer #4038 it seems like you want to run Python code so that you get keywords created in the log file. As I just commented #4038, we could have an API for that purpose but someone needed to design (and implement it).
I cannot comment the example above much without trying it out myself and I don't have time for that. What I can say is that you are using some internal APIs (e.g.
StatusReporter) that aren't guaranteed to be stable between major releases. An official stable and public API for this purpose would thus be a lot better.pekkaklarck commentedon Sep 14, 2021
It seems to me that what this issue originally asks i.e.
isn't in out scope. Having an API to create keywords that are shown in the log file would probably be a good idea, though, and a separate issue should be submitted about that. Or did I miss something here?
pekkaklarck commentedon Mar 15, 2022
The underlying feature request seems to be same as proposed in #4252. Closing this one.
KotlinIsland commentedon Mar 15, 2022
@pekkaklarck Unfortunately #4252 is unrelated to the underlying request 😔. I have re-raised this issue as #4276.
pekkaklarck commentedon Mar 15, 2022
This issue proposes adding new context manager that would create nested keywords under a library keyword exactly like #4252 proposes. I thought that was the main point.
KotlinIsland commentedon Mar 15, 2022
No, the
keywordcontext manager was just to illustrate the desire to fail current keyword and continue, while also making parent keywords fail.