27. Introduction to unittest
The unittest module is Python's built-in testing framework.
Writing a Test Case
import unittest
def add(a, b):
return a + b
class TestAdd(unittest.TestCase):
def test_addition(self):
self.assertEqual(add(2, 3), 5)
if __name__ == "__main__":
unittest.main()
Common Assertions
- assertEqual(a, b)
- assertTrue(x) / assertFalse(x)
- assertRaises(Exception, func, *args)
Wrap-Up
unittest is included with Python and provides a structured way to test your code.