Skip to content

Allow raising ReturnFromKeyword in python keywords that are called from tests #4038

Closed as not planned

Description

@KotlinIsland

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
from robot.errors import ExecutionFailed, ReturnFromKeyword

def fail_and_return():
    fail = ExecutionFailed("asdf", continue_on_failure=True)
    raise ReturnFromKeyword((10,), (fail,))

Activity

  1. pekkaklarck commented on Sep 13, 2021

    @pekkaklarck
    Member

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

  2. KotlinIsland commented on Sep 14, 2021

    @KotlinIsland
    ContributorAuthor

    Hi @pekkaklarck, I have a need to report failures and inner keywords from within python code heres an example:

    def some_kw() -> int:
        with keyword("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 continue
            with keyword("inner kw"):  # in the robot log will report as a keyword
                rf.eq(1, 2)  # in the robot log will fail 'inner kw', 'outer kw' and 'some kw' but will not raise an exception
                logger.info("wow")  # will be executed
        logger.info("end of some kw")  # will be executed
        return 10  # will be returned

    image

    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.

  3. KotlinIsland commented on Sep 14, 2021

    @KotlinIsland
    ContributorAuthor

    Related to #3706

  4. pekkaklarck commented on Sep 14, 2021

    @pekkaklarck
    Member

    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.

  5. pekkaklarck commented on Sep 14, 2021

    @pekkaklarck
    Member

    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 exception
    logger.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.

  6. pekkaklarck commented on Sep 14, 2021

    @pekkaklarck
    Member

    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.

  7. KotlinIsland commented on Sep 15, 2021

    @KotlinIsland
    ContributorAuthor

    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 )

    with keyword("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:

    with keyword("validate all the things on this page"):
        try:
           eq(a, b)
        except AssertionError as err:
           log_failure(e)
        try:
           eq(c, d)
        except AssertionError as err:
           log_failure(e)
        try:
           eq(e, f)
        except AssertionError as err:
           log_failure(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions