From ca05b62880cd94c6df6c1ab494e01f8bd7f4f2d6 Mon Sep 17 00:00:00 2001 From: "Amit.w" Date: Thu, 1 Aug 2019 09:03:28 +0300 Subject: [PATCH] Add test for soft_assert in a rcursive function --- tests/test_soft.py | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/tests/test_soft.py b/tests/test_soft.py index dd4289f..af0de8a 100644 --- a/tests/test_soft.py +++ b/tests/test_soft.py @@ -26,10 +26,9 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -import sys - from assertpy import assert_that, soft_assertions, fail + def test_success(): with soft_assertions(): assert_that('foo').is_length(3) @@ -44,6 +43,7 @@ def test_success(): assert_that('foo').is_equal_to_ignoring_case('FOO') assert_that({'a': 1}).has_a(1) + def test_failure(): try: with soft_assertions(): @@ -75,10 +75,11 @@ def test_failure(): assert_that(out).contains('Expected <1> to be equal to <2> on key , but was not.') assert_that(out).contains('Expected key , but val has no key .') + def test_failure_chain(): try: with soft_assertions(): - assert_that('foo').is_length(4).is_empty().is_false().is_digit().is_upper()\ + assert_that('foo').is_length(4).is_empty().is_false().is_digit().is_upper() \ .is_equal_to('bar').is_not_equal_to('foo').is_equal_to_ignoring_case('BAR') fail('should have raised error') except AssertionError as e: @@ -92,10 +93,12 @@ def test_failure_chain(): assert_that(out).contains('Expected to be not equal to , but was.') assert_that(out).contains('Expected to be case-insensitive equal to , but was not.') + def test_expected_exception_success(): with soft_assertions(): assert_that(func_err).raises(RuntimeError).when_called_with('foo').is_equal_to('err') + def test_expected_exception_failure(): try: with soft_assertions(): @@ -107,9 +110,11 @@ def test_expected_exception_failure(): assert_that(out).contains('Expected to be equal to , but was not.') assert_that(out).contains("Expected to raise when called with ('baz').") + def func_ok(arg): pass + def func_err(arg): raise RuntimeError('err') @@ -123,6 +128,7 @@ def test_fail(): out = str(e) assert_that(out).is_equal_to('Fail!') + def test_fail_with_msg(): try: with soft_assertions(): @@ -132,6 +138,7 @@ def test_fail_with_msg(): out = str(e) assert_that(out).is_equal_to('Fail: foobar!') + def test_fail_with_soft_failing_asserts(): try: with soft_assertions(): @@ -149,6 +156,7 @@ def test_fail_with_soft_failing_asserts(): assert_that(out).does_not_contain('Expected to be not equal to , but was.') assert_that(out).does_not_contain('Expected to be case-insensitive equal to , but was not.') + def test_double_fail(): try: with soft_assertions(): @@ -159,6 +167,7 @@ def test_double_fail(): out = str(e) assert_that(out).is_equal_to('Fail!') + def test_nested(): try: with soft_assertions(): @@ -177,3 +186,23 @@ def test_nested(): 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.') + + +def test_recursive_nesting(): + def recursive(number): + if number <= 0: + return + with soft_assertions(): + recursive(number-1) + assert_that(number).is_equal_to(7) + try: + recursive(6) + except AssertionError as e: + out = str(e) + assert_that(out).contains('1. Expected <1> to be equal to <7>, but was not.') + assert_that(out).contains('2. Expected <2> to be equal to <7>, but was not.') + assert_that(out).contains('3. Expected <3> to be equal to <7>, but was not.') + assert_that(out).contains('4. Expected <4> to be equal to <7>, but was not.') + assert_that(out).contains('5. Expected <5> to be equal to <7>, but was not.') + assert_that(out).contains('6. Expected <6> to be equal to <7>, but was not.') +