26
26
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
27
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
28
29
- import sys
30
-
31
29
from assertpy import assert_that , soft_assertions , fail
32
30
31
+
33
32
def test_success ():
34
33
with soft_assertions ():
35
34
assert_that ('foo' ).is_length (3 )
@@ -44,6 +43,7 @@ def test_success():
44
43
assert_that ('foo' ).is_equal_to_ignoring_case ('FOO' )
45
44
assert_that ({'a' : 1 }).has_a (1 )
46
45
46
+
47
47
def test_failure ():
48
48
try :
49
49
with soft_assertions ():
@@ -75,10 +75,11 @@ def test_failure():
75
75
assert_that (out ).contains ('Expected <1> to be equal to <2> on key <a>, but was not.' )
76
76
assert_that (out ).contains ('Expected key <foo>, but val has no key <foo>.' )
77
77
78
+
78
79
def test_failure_chain ():
79
80
try :
80
81
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 () \
82
83
.is_equal_to ('bar' ).is_not_equal_to ('foo' ).is_equal_to_ignoring_case ('BAR' )
83
84
fail ('should have raised error' )
84
85
except AssertionError as e :
@@ -92,10 +93,12 @@ def test_failure_chain():
92
93
assert_that (out ).contains ('Expected <foo> to be not equal to <foo>, but was.' )
93
94
assert_that (out ).contains ('Expected <foo> to be case-insensitive equal to <BAR>, but was not.' )
94
95
96
+
95
97
def test_expected_exception_success ():
96
98
with soft_assertions ():
97
99
assert_that (func_err ).raises (RuntimeError ).when_called_with ('foo' ).is_equal_to ('err' )
98
100
101
+
99
102
def test_expected_exception_failure ():
100
103
try :
101
104
with soft_assertions ():
@@ -107,9 +110,11 @@ def test_expected_exception_failure():
107
110
assert_that (out ).contains ('Expected <err> to be equal to <bar>, but was not.' )
108
111
assert_that (out ).contains ("Expected <func_ok> to raise <RuntimeError> when called with ('baz')." )
109
112
113
+
110
114
def func_ok (arg ):
111
115
pass
112
116
117
+
113
118
def func_err (arg ):
114
119
raise RuntimeError ('err' )
115
120
@@ -123,6 +128,7 @@ def test_fail():
123
128
out = str (e )
124
129
assert_that (out ).is_equal_to ('Fail!' )
125
130
131
+
126
132
def test_fail_with_msg ():
127
133
try :
128
134
with soft_assertions ():
@@ -132,6 +138,7 @@ def test_fail_with_msg():
132
138
out = str (e )
133
139
assert_that (out ).is_equal_to ('Fail: foobar!' )
134
140
141
+
135
142
def test_fail_with_soft_failing_asserts ():
136
143
try :
137
144
with soft_assertions ():
@@ -149,6 +156,7 @@ def test_fail_with_soft_failing_asserts():
149
156
assert_that (out ).does_not_contain ('Expected <foo> to be not equal to <foo>, but was.' )
150
157
assert_that (out ).does_not_contain ('Expected <foo> to be case-insensitive equal to <BAR>, but was not.' )
151
158
159
+
152
160
def test_double_fail ():
153
161
try :
154
162
with soft_assertions ():
@@ -159,6 +167,7 @@ def test_double_fail():
159
167
out = str (e )
160
168
assert_that (out ).is_equal_to ('Fail!' )
161
169
170
+
162
171
def test_nested ():
163
172
try :
164
173
with soft_assertions ():
@@ -177,3 +186,23 @@ def test_nested():
177
186
assert_that (out ).contains ('3. Expected <c> to be equal to <C>, but was not.' )
178
187
assert_that (out ).contains ('4. Expected <b> to be equal to <B2>, but was not.' )
179
188
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