Skip to content

Commit ca05b62

Browse files
author
Amit.w
committed
Add test for soft_assert in a rcursive function
1 parent bc70d53 commit ca05b62

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

tests/test_soft.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@
2626
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2727
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828

29-
import sys
30-
3129
from assertpy import assert_that, soft_assertions, fail
3230

31+
3332
def test_success():
3433
with soft_assertions():
3534
assert_that('foo').is_length(3)
@@ -44,6 +43,7 @@ def test_success():
4443
assert_that('foo').is_equal_to_ignoring_case('FOO')
4544
assert_that({'a': 1}).has_a(1)
4645

46+
4747
def test_failure():
4848
try:
4949
with soft_assertions():
@@ -75,10 +75,11 @@ def test_failure():
7575
assert_that(out).contains('Expected <1> to be equal to <2> on key <a>, but was not.')
7676
assert_that(out).contains('Expected key <foo>, but val has no key <foo>.')
7777

78+
7879
def test_failure_chain():
7980
try:
8081
with soft_assertions():
81-
assert_that('foo').is_length(4).is_empty().is_false().is_digit().is_upper()\
82+
assert_that('foo').is_length(4).is_empty().is_false().is_digit().is_upper() \
8283
.is_equal_to('bar').is_not_equal_to('foo').is_equal_to_ignoring_case('BAR')
8384
fail('should have raised error')
8485
except AssertionError as e:
@@ -92,10 +93,12 @@ def test_failure_chain():
9293
assert_that(out).contains('Expected <foo> to be not equal to <foo>, but was.')
9394
assert_that(out).contains('Expected <foo> to be case-insensitive equal to <BAR>, but was not.')
9495

96+
9597
def test_expected_exception_success():
9698
with soft_assertions():
9799
assert_that(func_err).raises(RuntimeError).when_called_with('foo').is_equal_to('err')
98100

101+
99102
def test_expected_exception_failure():
100103
try:
101104
with soft_assertions():
@@ -107,9 +110,11 @@ def test_expected_exception_failure():
107110
assert_that(out).contains('Expected <err> to be equal to <bar>, but was not.')
108111
assert_that(out).contains("Expected <func_ok> to raise <RuntimeError> when called with ('baz').")
109112

113+
110114
def func_ok(arg):
111115
pass
112116

117+
113118
def func_err(arg):
114119
raise RuntimeError('err')
115120

@@ -123,6 +128,7 @@ def test_fail():
123128
out = str(e)
124129
assert_that(out).is_equal_to('Fail!')
125130

131+
126132
def test_fail_with_msg():
127133
try:
128134
with soft_assertions():
@@ -132,6 +138,7 @@ def test_fail_with_msg():
132138
out = str(e)
133139
assert_that(out).is_equal_to('Fail: foobar!')
134140

141+
135142
def test_fail_with_soft_failing_asserts():
136143
try:
137144
with soft_assertions():
@@ -149,6 +156,7 @@ def test_fail_with_soft_failing_asserts():
149156
assert_that(out).does_not_contain('Expected <foo> to be not equal to <foo>, but was.')
150157
assert_that(out).does_not_contain('Expected <foo> to be case-insensitive equal to <BAR>, but was not.')
151158

159+
152160
def test_double_fail():
153161
try:
154162
with soft_assertions():
@@ -159,6 +167,7 @@ def test_double_fail():
159167
out = str(e)
160168
assert_that(out).is_equal_to('Fail!')
161169

170+
162171
def test_nested():
163172
try:
164173
with soft_assertions():
@@ -177,3 +186,23 @@ def test_nested():
177186
assert_that(out).contains('3. Expected <c> to be equal to <C>, but was not.')
178187
assert_that(out).contains('4. Expected <b> to be equal to <B2>, but was not.')
179188
assert_that(out).contains('5. Expected <a> to be equal to <A2>, but was not.')
189+
190+
191+
def test_recursive_nesting():
192+
def recursive(number):
193+
if number <= 0:
194+
return
195+
with soft_assertions():
196+
recursive(number-1)
197+
assert_that(number).is_equal_to(7)
198+
try:
199+
recursive(6)
200+
except AssertionError as e:
201+
out = str(e)
202+
assert_that(out).contains('1. Expected <1> to be equal to <7>, but was not.')
203+
assert_that(out).contains('2. Expected <2> to be equal to <7>, but was not.')
204+
assert_that(out).contains('3. Expected <3> to be equal to <7>, but was not.')
205+
assert_that(out).contains('4. Expected <4> to be equal to <7>, but was not.')
206+
assert_that(out).contains('5. Expected <5> to be equal to <7>, but was not.')
207+
assert_that(out).contains('6. Expected <6> to be equal to <7>, but was not.')
208+

0 commit comments

Comments
 (0)