8000 Add test for soft_assert in a recursive function by amitwer · Pull Request #94 · assertpy/assertpy · GitHub
[go: up one dir, main page]

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions tests/test_soft.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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():
Expand Down Expand Up @@ -75,10 +75,11 @@ def test_failure():
assert_that(out).contains('Expected <1> to be equal to <2> on key <a>, but was not.')
assert_that(out).contains('Expected key <foo>, but val has no key <foo>.')


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:
Expand All @@ -92,10 +93,12 @@ def test_failure_chain():
assert_that(out).contains('Expected <foo> to be not equal to <foo>, but was.')
assert_that(out).contains('Expected <foo> to be case-insensitive equal to <BAR>, 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():
Expand All @@ -107,9 +110,11 @@ def test_expected_exception_failure():
assert_that(out).contains('Expected <err> to be equal to <bar>, but was not.')
assert_that(out).contains("Expected <func_ok> to raise <RuntimeError> when called with ('baz').")


def func_ok(arg):
pass


def func_err(arg):
raise RuntimeError('err')

Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -149,6 +156,7 @@ def test_fail_with_soft_failing_asserts():
assert_that(out).does_not_contain('Expected <foo> to be not equal to <foo>, but was.')
assert_that(out).does_not_contain('Expected <foo> to be case-insensitive equal to <BAR>, but was not.')


def test_double_fail():
try:
with soft_assertions():
Expand All @@ -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():
Expand All @@ -177,3 +186,23 @@ def test_nested():
assert_that(out).contains('3. Expected <c> to be equal to <C>, but was not.')
assert_that(out).contains('4. Expected <b> to be equal to <B2>, but was not.')
assert_that(out).contains('5. Expected <a> to be equal to <A2>, 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.')