Skip to content

(🎁) Support continuable failures in python code #4276

Closed as not planned
Closed as not planned

Description

@KotlinIsland

Desired functionality: perform "run keyword and continue on failure" and support continuable failures in python code

I have many instances where I want to report multiple failures in python code, (and want the code to flow naturally). I have created all sorts of custom implementations to get close to the desired behavior.

Continue Example
    Validate All Things On Page
    Log    end

(psuedo code moment)
Introduce a function that will log a failure to the log, but will also affect the status of the test:

def validate_all_things_on_page():
  continuable_validate("expected a", get_a())
  continuable_validate("expected b", get_b())
  continuable_validate("expected c", get_c())
  print("Done!")

def continuable_validate(expected, actual):
    if expected != actual:
        robot.api.fail("Unfortunately a '!=' moment was detected in your code 😳", continue=True)

An alternate(or additional) solution would be to introduce a context manager that controls the state of failures being reported:

def validate_all_things_on_page():
  with robot.api.continue_on_failures():
    validate("expected a", get_a())
    validate("expected b", get_b())
    validate("expected c", get_c())
  print("Done!")

def validate(expected, actual):
    if expected != actual:
        robot.api.fail("Unfortunately a '!=' moment was detected in your code 😳")

image

(from #4237 (comment))

Being able to log multiple individual failure messages under a single keyword could be nice, though. That we could easily support by making it possible to raise an exception so that its message is actually a list of messages. In the simplest form it could just mean AssertiongError(['First error', 'Second error']). We could log each error separately and then generate "Several failures occurred" error based on the errors automatically. This would be easy to implement and doesn't have that much to do with an API to run other keywords. It would need its own issue, though, so please submit one!

Duplicate of: #3706 #4038

Activity

  1. pekkaklarck commented on Mar 15, 2022

    @pekkaklarck
    Member

    If I understand this correctly, the idea is to report multiple errors from one keyword so that

    • each error is logged separately and
    • Robot constructs the final "Multiple failures occurred" message automatically.

    If the above is the use case, what's proposed looks really complicated to use and to implement. I don't understand why this would require adding both continue_on_failures context manager an new robot.api.fail method. Could you are both needed or what the API should be?

  2. pekkaklarck commented on Mar 15, 2022

    @pekkaklarck
    Member

    As you probably already noticed, I submitted #4277 about an alternative way to report multiple failures. That approach would be easier to use because you would use raise AssertionError(...) as with normal failures and it ought to be pretty easy to implement as well. The only downside I see compared to what's proposed here is that it doesn't, in the current form, support accurate timestamps with each failure.

  3. KotlinIsland commented on Mar 15, 2022

    @KotlinIsland
    ContributorAuthor

    I don't understand why this would require adding both continue_on_failures´ context manager an new robot.api.fail` method.

    I'm just providing possible solutions, I have custom functionality for both approaches.

    it doesn't, in the current form, support accurate timestamps with each failure.

    I think that preserving log order is highly important. Also I can see that your approach would involve heavy boilerplate with high potential for inadvertently introducing bugs.

  4. pekkaklarck commented on Mar 15, 2022

    @pekkaklarck
    Member

    Do you mean both continue_on_failures and robot.api.fail would be need to accomplish this or that they would provide two alternative ways to do that? I value your proposals, but I would very highly appreciate if you could spend a bit longer time describing them so that it would be easier for me to understand them.

  5. KotlinIsland commented on Mar 15, 2022

    @KotlinIsland
    ContributorAuthor

    Yeah, two different approaches. Sorry about that, I'll update the OP

  6. pekkaklarck commented on Mar 15, 2022

    @pekkaklarck
    Member

    Do you want both or are you just proposing two alternatives?

  7. KotlinIsland commented on Mar 15, 2022

    @KotlinIsland
    ContributorAuthor

    I have both and tend to use the context manager solution a lot more, but it's entirely up to you 😀.

    I have sub-typed BuiltIn and wrapped all the assertion function in this custom failure mechanism I have made.

  8. KotlinIsland commented on Mar 15, 2022

    @KotlinIsland
    ContributorAuthor

    And maybe my ideas aren't optimal, but hopefully they add to the general discussion.

  9. pekkaklarck commented on Mar 15, 2022

    @pekkaklarck
    Member

    So the main idea is adding robot.api.fail() that can be called with continue=True to make the failure continuable? And then robot.api.continue_on_failures context manager would just change the default behavior of robot.api.fail?

  10. pekkaklarck commented on Mar 15, 2022

    @pekkaklarck
    Member

    Do you have implementation somewhere so that I could look at what you've actually done? I cannot think any really clean way how to implement this exact API. I do have an idea about a similar API that could probably be implemented pretty easily and cleanly.

  11. pekkaklarck commented on Mar 15, 2022

    @pekkaklarck
    Member

    Here's an example of using an API that ought to be pretty easy to implement:

    from robot.api import logger
    
    def kw(expected, actual):
        with logger.failure_reporter() as reporter:
            for exp, act in zip(expected, actual):
                if exp != act:
                    reporter.fail(f'{exp} != {act}')

    The idea is that the failure_reporter context manager would support reporting failures so that they are handled immediately. In its __exit__ it would then check were any failures reported and raise an exception if here were. The context manager itself would be easy to implement otherwise, but we needed to exposefail so that it could be used by libraries.

    This API could be even nice to use:

    from robot.api import logger
    
    def kw(expected, actual):
        with logger.continue_on_failure:
            for exp, act in zip(expected, actual):
                if exp != act:
                    logger.fail(f'{exp} != {act}')

    Now logger itself would get fail function. It could just raise AssertionError by default, but continue_on_failure context manager would change the behavior. The annoying thing here is that because logger is a module, this approach would require storing some information in module global state. That's not nice in general and it would cause problems with threads.

  12. pekkaklarck commented on May 20, 2025

    @pekkaklarck
    Member

    Simply collecting all failures within a keyword and reporting them in one go is so easy that adding built-in functionality to Robot isn't, in my opinion, worth the effort amd added complexity.

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