|
1 | 1 | import unittest |
2 | 2 | import pytest |
3 | | -from dicts import create_inventory, add_items, decrement_items, remove_item, list_inventory |
| 3 | +from dicts import (create_inventory, |
| 4 | + add_items, |
| 5 | + decrement_items, |
| 6 | + remove_item, |
| 7 | + list_inventory) |
4 | 8 |
|
5 | 9 |
|
6 | 10 | class InventoryTest(unittest.TestCase): |
7 | 11 |
|
8 | 12 | @pytest.mark.task(taskno=1) |
9 | 13 | def test_create_inventory(self): |
10 | | - self.assertEqual(create_inventory(["wood", "iron", "iron", "diamond", "diamond"]), |
11 | | - {"wood": 1, "iron": 2, "diamond": 2}) |
| 14 | + |
| 15 | + actual_result = create_inventory(["wood", "iron", "iron", "diamond", "diamond"]) |
| 16 | + expected = {"wood": 1, "iron": 2, "diamond": 2} |
| 17 | + error_message = ('Called create_inventory(["wood", "iron", "iron", "diamond", "diamond"]). ' |
| 18 | + f'The function returned {actual_result}, but the tests expected {expected}.') |
| 19 | + |
| 20 | + self.assertEqual(actual_result, expected, msg=error_message) |
12 | 21 |
|
13 | 22 | @pytest.mark.task(taskno=2) |
14 | 23 | def test_add_one_item(self): |
15 | | - self.assertEqual(add_items({"wood": 4, "iron": 2}, ["iron", "iron"]), |
16 | | - {"wood": 4, "iron": 4}) |
| 24 | + actual_result = add_items({"wood": 4, "iron": 2}, ["iron", "iron"]) |
| 25 | + expected = {"wood": 4, "iron": 4} |
| 26 | + error_message = ('Called add_items({"wood": 4, "iron": 2}, ["iron", "iron"]). ' |
| 27 | + f'The function returned {actual_result}, but the tests expected {expected}.') |
| 28 | + |
| 29 | + self.assertEqual(actual_result, expected, msg=error_message) |
17 | 30 |
|
18 | 31 | @pytest.mark.task(taskno=2) |
19 | 32 | def test_add_multiple_items(self): |
20 | | - self.assertEqual(add_items({"wood": 2, "gold": 1, "diamond": 3}, ["wood", "gold", "gold"]), |
21 | | - {"wood": 3, "gold": 3, "diamond": 3}) |
| 33 | + actual_result = add_items({"wood": 2, "gold": 1, "diamond": 3}, ["wood", "gold", "gold"]) |
| 34 | + expected = {"wood": 3, "gold": 3, "diamond": 3} |
| 35 | + error_message = ('Called add_items({"wood": 2, "gold": 1, "diamond": 3}, ["wood", "gold", "gold"]). ' |
| 36 | + f'The function returned {actual_result}, but the tests expected {expected}.') |
| 37 | + |
| 38 | + self.assertEqual(actual_result, expected, msg=error_message) |
22 | 39 |
|
23 | 40 | @pytest.mark.task(taskno=2) |
24 | 41 | def test_add_new_item(self): |
25 | | - self.assertEqual(add_items({"iron": 1, "diamond": 2}, ["iron", "wood", "wood"]), |
26 | | - {"iron": 2, "diamond": 2, "wood": 2}) |
| 42 | + actual_result = add_items({"iron": 1, "diamond": 2}, ["iron", "wood", "wood"]) |
| 43 | + expected = {"iron": 2, "diamond": 2, "wood": 2} |
| 44 | + error_message = ('Called add_items({"iron": 1, "diamond": 2}, ["iron", "wood", "wood"]). ' |
| 45 | + f'The function returned {actual_result}, but the tests expected {expected}.') |
| 46 | + |
| 47 | + self.assertEqual(actual_result, expected, msg=error_message) |
27 | 48 |
|
28 | 49 | @pytest.mark.task(taskno=2) |
29 | 50 | def test_add_from_empty_dict(self): |
30 | | - self.assertEqual(add_items({}, ["iron", "iron", "diamond"]), |
31 | | - {"iron": 2, "diamond": 1}) |
| 51 | + actual_result = add_items({}, ["iron", "iron", "diamond"]) |
| 52 | + expected = {"iron": 2, "diamond": 1} |
| 53 | + error_message = ('Called add_items({}, ["iron", "iron", "diamond"]). ' |
| 54 | + f'The function returned {actual_result}, but the tests expected {expected}.') |
| 55 | + |
| 56 | + self.assertEqual(actual_result, expected, msg=error_message) |
32 | 57 |
|
33 | 58 | @pytest.mark.task(taskno=3) |
34 | 59 | def test_decrement_items(self): |
35 | | - self.assertEqual(decrement_items({"iron": 3, "diamond": 4, "gold": 2}, |
36 | | - ["iron", "iron", "diamond", "gold", "gold"]), |
37 | | - {"iron": 1, "diamond": 3, "gold": 0}) |
| 60 | + actual_result = decrement_items({"iron": 3, "diamond": 4, "gold": 2}, |
| 61 | + ["iron", "iron", "diamond", "gold", "gold"]) |
| 62 | + expected = {"iron": 1, "diamond": 3, "gold": 0} |
| 63 | + error_message = ('Called decrement_items({"iron": 3, "diamond": 4, "gold": 2},' |
| 64 | + '["iron", "iron", "diamond", "gold", "gold"]). The function ' |
| 65 | + f'returned {actual_result}, but the tests expected {expected}.') |
| 66 | + |
| 67 | + self.assertEqual(actual_result, expected, msg=error_message) |
38 | 68 |
|
39 | 69 | @pytest.mark.task(taskno=3) |
40 | 70 | def test_not_below_zero(self): |
41 | | - self.assertEqual(decrement_items({"wood": 2, "iron": 3, "diamond": 1}, |
42 | | - ["wood", "wood", "wood", "iron", "diamond", "diamond"]), |
43 | | - {"wood": 0, "iron": 2, "diamond": 0}) |
| 71 | + actual_result = decrement_items({"wood": 2, "iron": 3, "diamond": 1}, |
| 72 | + ["wood", "wood", "wood", "iron", "diamond", "diamond"]) |
| 73 | + expected = {"wood": 0, "iron": 2, "diamond": 0} |
| 74 | + error_message = ('Called decrement_items({"wood": 2, "iron": 3, "diamond": 1}, ' |
| 75 | + '["wood", "wood", "wood", "iron", "diamond", "diamond"]). The ' |
| 76 | + f'function returned {actual_result}, but the tests expected {expected}.') |
| 77 | + |
| 78 | + self.assertEqual(actual_result, expected, msg=error_message) |
44 | 79 |
|
45 | 80 | @pytest.mark.task(taskno=4) |
46 | 81 | def test_remove_item(self): |
47 | | - self.assertEqual(remove_item({"iron": 1, "diamond": 2, "gold": 1}, "diamond"), |
48 | | - {"iron": 1, "gold": 1}) |
| 82 | + actual_result = remove_item({"iron": 1, "diamond": 2, "gold": 1}, "diamond") |
| 83 | + expected = {"iron": 1, "gold": 1} |
| 84 | + error_message = ('Called remove_item({"iron": 1, "diamond": 2, "gold": 1}, "diamond"). ' |
| 85 | + f'The function returned {actual_result}, but the tests expected {expected}.') |
| 86 | + |
| 87 | + self.assertEqual(actual_result, expected, msg=error_message) |
49 | 88 |
|
50 | 89 | @pytest.mark.task(taskno=4) |
51 | 90 | def test_remove_item_not_in_inventory(self): |
52 | | - self.assertEqual(remove_item({"iron": 1, "diamond": 2, "gold": 1}, "wood"), |
53 | | - {"iron": 1, "gold": 1, "diamond": 2}) |
| 91 | + actual_result = remove_item({"iron": 1, "diamond": 2, "gold": 1}, "wood") |
| 92 | + expected = {"iron": 1, "gold": 1, "diamond": 2} |
| 93 | + error_message = ('Called remove_item({"iron": 1, "diamond": 2, "gold": 1}, "wood"). ' |
| 94 | + f'The function returned {actual_result}, ' |
| 95 | + f'but the tests expected {expected}.') |
| 96 | + |
| 97 | + self.assertEqual(actual_result, expected, msg=error_message) |
54 | 98 |
|
55 | 99 | @pytest.mark.task(taskno=5) |
56 | 100 | def test_list_inventory(self): |
57 | | - self.assertEqual(list_inventory({"coal": 15, "diamond": 3, "wood": 67, "silver": 0}), |
58 | | - [("coal", 15), ("diamond", 3), ("wood", 67)]) |
59 | | - |
| 101 | + actual_result = list_inventory({"coal": 15, "diamond": 3, "wood": 67, "silver": 0}) |
| 102 | + expected = [("coal", 15), ("diamond", 3), ("wood", 67)] |
| 103 | + error_message = ('Called list_inventory({"coal": 15, "diamond": 3, "wood": 67, "silver": 0}). ' |
| 104 | + f'The function returned {actual_result}, ' |
| 105 | + f'but the tests expected {expected}.') |
60 | 106 |
|
61 | | -if __name__ == "__main__": |
62 | | - unittest.main() |
| 107 | + self.assertEqual(actual_result, expected, msg=error_message) |
0 commit comments