Skip to content

soft_assertions does not handle nesting #92

Description

@amitwer

Nesting with soft_assertions(): statement breaks the "soft" attribute of the parent (the inner soft assertion is terminating the process.

Code to reproduce the scenario:

def nested():
    with soft_assertions():
        for i in range (1,10):
            with soft_assertions():
                print(i)
                assert_that(i).is_equal_to(12)
                assert_that(i).is_equal_to(14)

nested()

This code stops after printing 1, resulting in the 2 assertion errors, but does not continue the for loop.

2nd use case - recursive function (which was my starting state), behaves in a similar way:

def assertme(i):
    if i == 0:
        return
    with soft_assertions():
        print(f'asserting number {i}')
        assert_that(i).is_equal_to(16)
        assertme(i-1)

assertme(12)

Once the context closes with an error (i=1), this error is raised immediately, and breaches the entire call stack, resulting in:

AssertionError: soft assertion failures:
1. Expected <1> to be equal to <16>, but was not.

I was expecting the entire 1, 2, ..., 11, 12 assertion failures to be rolled up in a single soft assertion failure message.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions