Skip to content

Commit 4ae10d2

Browse files
committed
rework assertion messages
Fixes #4
1 parent 2ce24b6 commit 4ae10d2

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

asserts.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,36 @@ def assert_true(expr, msg=None):
2020
"""Fail the test unless the expression is truthy."""
2121
if not expr:
2222
if not msg:
23-
msg = repr(expr) + " is not true"
23+
msg = repr(expr) + " is not truthy"
2424
fail(msg)
2525

2626

2727
def assert_false(expr, msg=None):
2828
"""Fail the test unless the expression is falsy."""
2929
if expr:
3030
if not msg:
31-
msg = repr(expr) + " is not false"
31+
msg = repr(expr) + " is not falsy"
3232
fail(msg)
3333

3434

3535
def assert_boolean_true(expr, msg=None):
3636
"""Fail the test unless the expression is the constant True."""
37-
assert_is(True, expr, msg)
37+
assert_is(expr, True, msg)
3838

3939

4040
def assert_boolean_false(expr, msg=None):
4141
"""Fail the test unless the expression is the constant False."""
42-
assert_is(False, expr, msg)
42+
assert_is(expr, False, msg)
4343

4444

4545
def assert_is_none(expr, msg=None):
4646
"""Fail if actual is not None."""
47-
if expr is not None:
48-
fail(msg or "{!r} is not None".format(expr))
47+
assert_is(expr, None, msg)
4948

5049

5150
def assert_is_not_none(expr, msg=None):
5251
"""Fail if actual is None."""
53-
if expr is None:
54-
fail(msg or "{!r} is None".format(expr))
52+
assert_is_not(expr, None, msg)
5553

5654

5755
def assert_equal(first, second, msg=None):
@@ -105,7 +103,7 @@ def assert_is(first, second, msg=None):
105103
def assert_is_not(first, second, msg=None):
106104
"""Fail if the two objects are the same object."""
107105
if first is second:
108-
fail(msg or "{!r} is {!r}".format(first, second))
106+
fail(msg or "expected value not to be {!r}".format(first))
109107

110108

111109
def assert_in(first, second, msg=None):
@@ -131,7 +129,7 @@ def assert_is_instance(obj, cls, msg=None):
131129
if not isinstance(obj, cls):
132130
msg = (msg if msg is not None else
133131
repr(obj) + " is of " + repr(obj.__class__) +
134-
" not of " + repr(cls))
132+
", expected " + repr(cls))
135133
fail(msg)
136134

137135

@@ -223,7 +221,8 @@ def assert_raises_errno(exception, errno, msg=None):
223221
"""
224222

225223
def check_errno(exc):
226-
assert_equal(errno, exc.errno, msg)
224+
if errno != exc.errno:
225+
fail(msg or "wrong errno: {!r} != {!r}".format(errno, exc.errno))
227226
context = AssertRaisesContext(exception, msg)
228227
context.add_test(check_errno)
229228
return context

test_asserts.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def test_assert_true__truthy_value(self):
8787
assert_true("Hello World!")
8888

8989
def test_assert_true__falsy_value__default_message(self):
90-
with _assert_raises_assertion("'' is not true"):
90+
with _assert_raises_assertion("'' is not truthy"):
9191
assert_true("")
9292

9393
def test_assert_true__falsy_value__custom_message(self):
@@ -98,7 +98,7 @@ def test_assert_false__falsy_value(self):
9898
assert_false("")
9999

100100
def test_assert_false__truthy_value__default_message(self):
101-
with _assert_raises_assertion("25 is not false"):
101+
with _assert_raises_assertion("25 is not falsy"):
102102
assert_false(25)
103103

104104
def test_assert_false__truthy_value__custom_message(self):
@@ -113,15 +113,15 @@ def test_assert_boolean_true__false__custom_message(self):
113113
assert_boolean_true(False, msg="test message")
114114

115115
def test_assert_boolean_true__truthy__default_message(self):
116-
with _assert_raises_assertion("True is not 1"):
116+
with _assert_raises_assertion("1 is not True"):
117117
assert_boolean_true(1)
118118

119119
def test_assert_boolean_false__false(self):
120120
assert_boolean_false(False)
121121

122122
def test_assert_boolean_false__true__default_message(self):
123-
with _assert_raises_assertion("False is not True"):
124-
assert_boolean_false(True)
123+
with _assert_raises_assertion("'foo' is not False"):
124+
assert_boolean_false("foo")
125125

126126
def test_assert_boolean_false__falsy__custom_message(self):
127127
with _assert_raises_assertion("test message"):
@@ -142,7 +142,7 @@ def test_assert_is_not_none__string(self):
142142
assert_is_not_none("")
143143

144144
def test_assert_is_not_none__none__default_message(self):
145-
with _assert_raises_assertion("None is None"):
145+
with _assert_raises_assertion("expected value not to be None"):
146146
assert_is_not_none(None)
147147

148148
def test_assert_is_not_none__none__custom_message(self):
@@ -234,7 +234,7 @@ def test_assert_regex__does_not_match_string__default_message(self):
234234
assert_regex("This is a test text", "XXX")
235235

236236
def test_assert_regex__does_not_match_regex__default_message(self):
237-
regex = re.compile("XXX")
237+
regex = re.compile(r"XXX")
238238
with _assert_raises_assertion(
239239
"'This is a test text' does not match 'XXX'"):
240240
assert_regex("This is a test text", regex)
@@ -263,7 +263,7 @@ def test_assert_is_not__not_same(self):
263263
assert_is_not(x, y)
264264

265265
def test_assert_is_not__same__default_message(self):
266-
with _assert_raises_assertion("5 is 5"):
266+
with _assert_raises_assertion("expected value not to be 5"):
267267
assert_is_not(5, 5)
268268

269269
def test_assert_is_not__same__custom_message(self):
@@ -313,8 +313,8 @@ def test_assert_is_instance__is_sub_class(self):
313313
assert_is_instance(IOError(), Exception)
314314

315315
def test_assert_is_instance__not_instance__default_message(self):
316-
expected_message = ("'my string' is of <class 'str'> "
317-
"not of <class 'int'>")
316+
expected_message = ("'my string' is of <class 'str'>, "
317+
"expected <class 'int'>")
318318
if sys.version_info[0] < 3:
319319
expected_message = expected_message.replace("class", "type")
320320
with _assert_raises_assertion(expected_message):
@@ -452,7 +452,7 @@ class WrongClass(OSError):
452452
raise AssertionError("WrongClass was not raised")
453453

454454
def test_assert_raises_errno__wrong_errno__default_message(self):
455-
with _assert_raises_assertion("20 != 1"):
455+
with _assert_raises_assertion("wrong errno: 20 != 1"):
456456
with assert_raises_errno(OSError, 20):
457457
raise OSError(1, "Test error")
458458

0 commit comments

Comments
 (0)