Skip to content

Commit 540f0f9

Browse files
committed
Rework unittest content and tests. Add mock testing to the demo
1 parent 6559119 commit 540f0f9

File tree

4 files changed

+42
-27
lines changed

4 files changed

+42
-27
lines changed

05_testing_and_ci/examples/python_testing/test_operations_unittests.py

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""
22
Tests for mathematical operations functions.
33
"""
4-
from operations import find_max, find_mean
4+
from operations import MathOperations
55
import unittest
66
from unittest import TestCase
7+
from unittest.mock import MagicMock
78
import csv
89

910

@@ -13,8 +14,8 @@ class TestOperations(TestCase):
1314
"""
1415
def setUp(self):
1516
# Fixture
16-
self.data1 = [43, 32, 167, 18, 1, 209]
17-
self.data2 = [3, 13, 33, 23, 498]
17+
self._data = [43, 32, 167, 18, 1, 209, 17]
18+
self._math_ops = MathOperations(self._data)
1819

1920
# Unit test
2021
def test_find_max(self):
@@ -25,7 +26,7 @@ def test_find_max(self):
2526
expected_max = 209
2627

2728
# Actual result
28-
actual_max = find_max(self.data1)
29+
actual_max = self._math_ops.find_max()
2930

3031
# Test
3132
self.assertEqual(actual_max, expected_max)
@@ -36,49 +37,62 @@ def test_find_mean(self):
3637
Test operations.find_mean
3738
"""
3839
# Expected result
39-
expected_mean = 78.33
40+
expected_mean = 69.57
4041

4142
# Actual result
42-
actual_mean = find_mean(self.data1)
43+
actual_mean = self._math_ops.find_mean()
4344

4445
# Test
4546
self.assertAlmostEqual(actual_mean, expected_mean, 2)
4647

47-
# Integration test
48-
def test_mean_of_max(self):
48+
# Unit test
49+
def test_unit_find_median(self):
4950
"""
50-
Test operations.find_max and operations.find_mean
51+
Test operations.find_median
5152
"""
5253
# Expected result
53-
expected_mean_of_max = 353.5
54+
expected_median = 32
5455

55-
maximum1 = find_max(self.data1)
56-
maximum2 = find_max(self.data2)
56+
# Mock reorder_data to isolate the test
57+
self._math_ops.reorder_data = MagicMock(return_value=[1, 17, 18, 32, 43, 167, 209])
5758

5859
# Actual result
59-
actual_mean_of_max = find_mean([maximum1, maximum2])
60-
60+
actual_median = self._math_ops.find_median()
61+
62+
# Test
63+
self.assertEqual(actual_median, expected_median)
64+
65+
# Integration test
66+
def test_median(self):
67+
"""
68+
Test operations.find_median
69+
"""
70+
# Expected result
71+
expected_median = 32
72+
73+
# Actual result
74+
actual_median = self._math_ops.find_median()
75+
6176
# Test
62-
self.assertEqual(actual_mean_of_max, expected_mean_of_max)
77+
self.assertEqual(actual_median, expected_median)
6378

6479
# Regression test
65-
def test_regression_mean(self):
80+
def test_reg_reorder_data(self):
6681
"""
67-
Test operations.find_mean on a previously generated dataset
82+
Test operations.reorder_data with data from CSV file
6883
"""
69-
with open("mean_data.csv") as f:
84+
with open("reordered_data.csv") as f:
7085
rows = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
71-
# Fixture
72-
data = next(rows)
73-
74-
# Expected result
75-
reference_mean = next(rows)
86+
87+
for row in rows:
88+
expected_reordered_data = row
7689

7790
# Actual result
78-
actual_mean = find_mean(data)
91+
self._math_ops.reorder_data()
92+
actual_reordered_data = self._math_ops._data
7993

8094
# Test
81-
self.assertAlmostEqual(actual_mean, reference_mean[0], 2)
95+
self.assertEqual(actual_reordered_data, expected_reordered_data)
8296

8397
if __name__ == "__main__":
8498
# Run the tests

05_testing_and_ci/python_testing_demo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ tests/
4949
- `unittest.main()` provides an option to run the tests from a command-line interface and also from a file.
5050
- `setUp` function is executed before all the tests. Similar a clean up function `tearDown` exists.
5151
- The intention is to group together sets of similar tests in an instant of `unittest.TestCase` and have multiple such instances.
52+
- A unit test for the function `find_median` is written by mocking the function `reorder_data` using [MagicMock](https://docs.python.org/3/library/unittest.mock.html#magic-mock) from unittest. The function `reorder_data` is mocked so that the function `find_median` can be tested in isolation.
5253
- Decorators such as `@unittest.skip`, `@unittest.skipIf`, `@unittest.expectedFailure` can be used to gain flexibility over working of tests.
53-
- `unittest.TestCase.subTest` can be used to distinguish parameters inside the body of a test.
5454

5555
## coverage
5656

05_testing_and_ci/python_testing_slides.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ slideOptions:
4747
- Python framework specifically designed to run, monitor and automate unit tests.
4848
- Many features like test automation, sharing of setup and shutdown of tests, etc.
4949
- Use the base class `unittest.TestCase` to create a test suite.
50+
- `MagicMock` from `unittest.mock` used for mock testing.
5051
- Command-line interface: `python -m unittest test_module1 test_module2 ...`.
5152
- Part of the Python standard library.
5253

timetable.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,4 @@
120120

121121
## 12.2 – Wed, January 14, 2025
122122

123-
- **90** min.: [Exercise: Testing Python Code](https://github.com/Simulation-Software-Engineering/Lecture-Material/blob/main/05_testing_and_ci/python_testing_exercise.md)
123+
- **90** min.: [Exercise: Testing Python Code](https://github.com/Simulation-Software-Engineering/Lecture-Material/blob/main/05_testing_and_ci/python_testing_exercise.md)

0 commit comments

Comments
 (0)