#!/usr/bin/python3 # -*- coding: utf-8 -*- """ unittest This one requires the most learning, and cognitive overhead. While it is most powerful, it is not as elegant or simple as doctest. It is also possible to debug this one easily (breakpoint or 't') Many more types of assert statements than we show below: Method Checks that assertEqual(a, b) a == b assertNotEqual(a,b) a != b assertTrue(x) bool(x) is True assertFalse(x) bool(x) is False assertIs(a, b) a is b assertIsNot(a,b) a is not b assertIsNone(x) x is None assertIsNotNone(x) x is not None assertIn(a, b) a in b assertNotIn(a, b) a not in b assertAlmostEqual(a, b) round(a - b, 7) == 0 assertGreater(a, b) a > b assertGreaterEqual(a, b) a >= b assertLess(a, b) a < b assertLessEqual(a, b) a <= b """ from typing import List import unittest # %% Example 1: sum def sum_arr(x: List[float]) -> float: # If you want to debug these with a debugger, # you can set a breakpoint within the function you want to test, # or just step into the function in main. return sum(x) # tests (one of these is wrong) class TestSum(unittest.TestCase): def test_sum(self) -> None: self.assertEqual(sum_arr([1, 2, 3]), 6, "Should be 6") self.assertEqual(sum_arr([5, 2, 3]), 10, "Should be 10") def test_sum_2(self) -> None: self.assertEqual(sum_arr([1, 2, 5]), 7, "Should be 8") if __name__ == "__main__": # exits after tests with return code for pass/fail unittest.main(verbosity=2) """ Can also be run as: python -m unittest -v 02_unittest_a """