diff --git a/assertpy/assertpy.py b/assertpy/assertpy.py index 645dba9..a6c2b7b 100644 --- a/assertpy/assertpy.py +++ b/assertpy/assertpy.py @@ -58,7 +58,7 @@ ### soft assertions ### -_soft_ctx = False +_soft_ctx = 0 _soft_err = [] @contextlib.contextmanager @@ -67,16 +67,17 @@ def soft_assertions(): global _soft_err # init ctx - _soft_ctx = True - _soft_err = [] + if _soft_ctx == 0: + _soft_err = [] + _soft_ctx += 1 try: yield finally: # reset ctx - _soft_ctx = False + _soft_ctx -= 1 - if _soft_err: + if _soft_err and _soft_ctx == 0: out = 'soft assertion failures:' for i,msg in enumerate(_soft_err): out += '\n%d. %s' % (i+1, msg) diff --git a/tests/test_soft.py b/tests/test_soft.py index 855c857..dd4289f 100644 --- a/tests/test_soft.py +++ b/tests/test_soft.py @@ -159,3 +159,21 @@ def test_double_fail(): out = str(e) assert_that(out).is_equal_to('Fail!') +def test_nested(): + try: + with soft_assertions(): + assert_that('a').is_equal_to('A') + with soft_assertions(): + assert_that('b').is_equal_to('B') + with soft_assertions(): + assert_that('c').is_equal_to('C') + assert_that('b').is_equal_to('B2') + assert_that('a').is_equal_to('A2') + fail('should have raised error') + except AssertionError as e: + out = str(e) + assert_that(out).contains('1. Expected to be equal to , but was not.') + assert_that(out).contains('2. Expected to be equal to , but was not.') + assert_that(out).contains('3. Expected to be equal to , but was not.') + assert_that(out).contains('4. Expected to be equal to , but was not.') + assert_that(out).contains('5. Expected to be equal to , but was not.')