Skip to content

Support reporting multiple failures with one exception #4277

Description

@pekkaklarck

It is pretty common that a single library keyword detects multiple exceptions. For example, there could be multiple failures here:

for act, exp in zip(actual, expected):
    if act != exp:
        raise AssertionError(f"Expected '{exp]', got '{act}'.")

A problem with the above is that it would fail for the first failure and possible other failures wouldn't be reported. This problem is relatively easy to avoid by collecting errors and reporting them in one go:

failures = []
for act, exp in zip(actual, expected):
    if act != exp:
        failures.append(f"Expected '{exp]', got '{act}'.")
if failures:
    if len(falures) == 1:
        raise Assertion(failures[0])
    raise AssertionError('\n- '.join(['Multiple failures occurred:'] + failures))

Although the above works, it still has some problems:

  • There's some extra work in creating the final error message.
  • There's only one failure reported inside the keyword.

We could easily avoid both of the above problems by making it possible to report multiple failures using one exception. The code would change to this:

failures = []
for act, exp in zip(actual, expected):
    if act != exp:
        failures.append(f"Expected '{exp]', got '{act}'.")
if failures:
    raise Assertion(failures)

How Robot should handle the above:

  • Report each failure in the list as a separate FAIL message in the log file.
  • Construct the final failure message from individual failures automatically.

This would be a simple syntax addition and the feature would be easy to use. The main open design decision I see is that we needed to design should we support this with all exceptions or only with some. Supporting it with all sounds a bit risky for me and I believe we should limit it somehow. If we limit, we could make it so that we only do this with AssertionError and possibly also with RuntimeError. Alternatively we could only support this with out custom exceptions like robot.api.Failure (#3685), but I believe that's too limiting.

Activity

  1. pekkaklarck commented on Mar 15, 2022

    @pekkaklarck
    MemberAuthor

    Issue #4276 proposes similar functionality with a different API. It has a benefit that each failure would get a timestamp that would match the time the failure actually was detected. If that's considered an important feature, I'm sure we could support it also with the approach proposed here.

  2. KotlinIsland commented on Mar 15, 2022

    @KotlinIsland
    Contributor

    pep-654 is highly relevant here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions