AssertionError: tests
what is an assertion?: tests
The code in assertion_error/tests/test_assertion_error.py from what is an assertion?
1def test_assert_keyword():
2 assert 1 + 1 == 2
3
4 assert '1' + '1' == '11'
5
6 assert 'I am' + ' alive' == 'I am alive'
9def test_assertion_error_w_none():
10 assert None is None
11
12 assert False is not None
13
14 assert True is not None
15
16 assert 0 is not None
17
18 assert 0.0 is not None
19
20 assert '' is not None
21
22 assert () is not None
23
24 assert [] is not None
25
26 assert set() is not None
27
28 assert {} is not None
31def test_assertion_error_w_false():
32 assert None is not False
33
34 assert False is False
35
36 assert True is not False
37
38 assert 0 is not False
39
40 assert 0.0 is not False
41
42 assert '' is not False
43
44 assert () is not False
45
46 assert [] is not False
47
48 assert set() is not False
49
50 assert {} is not False
53def test_assertion_error_w_true():
54 assert None is not True
55
56 assert False is not True
57
58 assert True is True
59
60 assert 0 is not True
61
62 assert 0.0 is not True
63
64 assert '' is not True
65
66 assert () is not True
67
68 assert [] is not True
69
70 assert set() is not True
71
72 assert {} is not True
75def test_assertion_error_w_equality():
76 assert None == None
77
78 assert False != None
79
80 assert False != True
81
82 assert False == False
83
84 assert True != None
85
86 assert True == True
89def test_assertion_error_w_is_vs_equal():
90 assert 0 is not 0.0
91
92 assert 0 == 0.0
93
94
95def will_not_run():
96 # will not run because
97 # the name does not start with test
98 assert False == True
99
100
101def test_failure():
102 # assert False == True
103 assert False != True
106# NOTES
107# a dictionary is not the same object as True
108# a dictionary is not the same object as False
109# a dictionary is not the same object as None
110# a set is not the same object as True
111# a set is not the same object as False
112# a set is not the same object as None
113# a list is not the same object as True
114# a list is not the same object as False
115# a list is not the same object as None
116# a tuple is not the same object as True
117# a tuple is not the same object as False
118# a tuple is not the same object as None
119# a string is not the same object as True
120# a string is not the same object as False
121# a string is not the same object as None
122# a float is not the same object as True
123# a float is not the same object as False
124# a float is not the same object as None
125# an integer is not the same object as True
126# an integer is not the same object as False
127# an integer is not the same object as None
128# True is True and equal to True
129# True is not equal to False
130# True is not the same object as False
131# True is not equal to None
132# True is not the same object as None
133# False is not equal to True
134# False is not the same object as True
135# False is False and equal to False
136# False is not equal to None
137# False is not the same object as None
138# None is not equal to True
139# None is not the same object as True
140# None is not equal to False
141# None is not the same object as False
142# None is None and equal to None
143
144# Exceptions seen
145# AssertionError
146# IndentationError
test AssertionError with functions: tests
The code in assertion_error/tests/test_assertion_error.py from test AssertionError with functions
1def assert_is_not(x, y):
2 assert x is not y
3
4
5def assert_is_not_none(x):
6 assert_is_not(x, None)
7
8
9def assert_is_not_false(x):
10 assert_is_not(x, False)
13def assert_is_not_true(x):
14 assert_is_not(x, True)
15
16
17def assert_not_equal(x, y):
18 assert x != y
19
20
21def assert_equal(x, y):
22 assert x == y
25def test_assert_keyword():
26 assert_equal(1+1, 2)
27 assert_equal('1'+'1', '11')
28 assert_equal('I am'+' alive', 'I am alive')
31def test_assertion_error_w_none():
32 assert None is None
33
34 assert_is_not_none(False)
35 assert_is_not_none(True)
36 assert_is_not_none(0)
37 assert_is_not_none(0.0)
38 assert_is_not_none('')
39 assert_is_not_none(())
40 assert_is_not_none([])
41 assert_is_not_none(set())
42 assert_is_not_none({})
45def test_assertion_error_w_false():
46 assert False is False
47
48 assert_is_not_false(None)
49 assert_is_not_false(True)
50 assert_is_not_false(0)
51 assert_is_not_false(0.0)
52 assert_is_not_false('')
53 assert_is_not_false(())
54 assert_is_not_false([])
55 assert_is_not_false(set())
56 assert_is_not_false({})
59def test_assertion_error_w_true():
60 assert True is True
61
62 assert_is_not_true(None)
63 assert_is_not_true(False)
64 assert_is_not_true(0)
65 assert_is_not_true(0.0)
66 assert_is_not_true('')
67 assert_is_not_true(())
68 assert_is_not_true([])
69 assert_is_not_true(set())
70 assert_is_not_true({})
73def test_assertion_error_w_equality():
74 assert_equal(None, None)
75 assert_equal(False, False)
76 assert_equal(True, True)
77
78 assert_not_equal(False, None)
79 assert_not_equal(False, True)
80 assert_not_equal(True, None)
83def test_assertion_error_w_is_vs_equal():
84 assert_is_not(0, 0.0)
85 assert_equal(0, 0.0)
86
87
88def will_not_run():
89 # will not run because
90 # the name does not start with test
91 assert False == True
92
93
94def test_failure():
95 # assert False == True
96 assert_not_equal(False, True)
97
98
99# NOTES
test AssertionError with loops: tests
The code in assertion_error/tests/test_assertion_error.py from test AssertionError with loops
1import unittest
2
3
4def assert_not_equal(x, y):
5 assert x != y
6
7
8def assert_equal(x, y):
9 assert x == y
12class TestAssertionError(unittest.TestCase):
13
14 basic_objects = (
15 None, False, True,
16 0, 0.0, '', (), [], set(), {},
17 )
19 def test_assertion_error(self):
20 for first in self.basic_objects:
21 for second in self.basic_objects:
22 with self.subTest(pair=(first, second)):
23 try:
24 assert first is second
25 except AssertionError:
26 assert first is not second
27
28 try:
29 self.assertIsNot(first, second)
30 except AssertionError:
31 self.assertIs(first, second)
32
33 try:
34 self.assertEqual(first, second)
35 except AssertionError:
36 self.assertNotEqual(first, second)
38 def test_assert_keyword(self):
39 for reality, my_expectation in (
40 (1 + 1, 2),
41 ('1' + '1', '11'),
42 ('I am' + ' alive', 'I am alive')
43 ):
44 with self.subTest((reality, my_expectation)):
45 assert_equal(reality, my_expectation)
46 self.assertEqual(reality, my_expectation)
47
48 def test_assertion_error_w_none(self):
49 for an_object in self.basic_objects:
50 with self.subTest(object=an_object):
51 try:
52 self.assertIsNotNone(an_object)
53 except AssertionError:
54 self.assertIsNone(an_object)
55
56 def will_not_run():
57 # will not run because
58 # the name does not start with test
59 assert False == True
60 self.assertEqual(False, True)
61
62 def test_failure(self):
63 assert_not_equal(False, True)
64 self.assertEqual(False, False)
65
test AssertionError with None: tests
test_assertion_error_w_none looks like this by the end of test AssertionError with None
54 def test_assertion_error_w_none(self):
55 assert None is None
56 self.assertIs(None, None)
57 self.assertIsNone(None)
58
59 assert_is_not_none(False)
60 self.assertIsNot(False, None)
61 self.assertIsNotNone(False)
62
63 assert_is_not_none(True)
64 self.assertIsNot(True, None)
65 self.assertIsNotNone(True)
66
67 assert_is_not_none(self.an_integer)
68 self.assertIsNot(self.an_integer, None)
69 self.assertIsNotNone(self.an_integer)
70
71 assert_is_not_none(self.a_float)
72 self.assertIsNot(self.a_float, None)
73 self.assertIsNotNone(self.a_float)
74
75 assert_is_not_none(self.a_string)
76 self.assertIsNot(self.a_string, None)
77 self.assertIsNotNone(self.a_string)
78
79 assert_is_not_none(self.a_tuple)
80 self.assertIsNot(self.a_tuple, None)
81 self.assertIsNotNone(self.a_tuple)
82
83 assert_is_not_none(self.a_list)
84 self.assertIsNot(self.a_list, None)
85 self.assertIsNotNone(self.a_list)
86
87 assert_is_not_none(self.a_set)
88 self.assertIsNot(self.a_set, None)
89 self.assertIsNotNone(self.a_set)
90
91 assert_is_not_none(self.a_dictionary)
92 self.assertIsNot(self.a_dictionary, None)
93 self.assertIsNotNone(self.a_dictionary)