Lesson 33 of 55
PythonUnit Testing with unittest
20 minutes
Course progress60%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 33 of 55
Python20 minutes
Keep these ideas in mind as you work through the lesson.
Python’s standard library includes unittest, a framework for writing repeatable tests. Test cases are classes that inherit from unittest.TestCase.
import unittest
def add(a, b):
return a + b
class TestMath(unittest.TestCase):
def test_add(self):
self.assertEqual(add(2, 3), 5)
self.assertEqual(add(-1, 1), 0)
if __name__ == '__main__':
unittest.main()
Run the script (python test_math.py) and unittest will report any failures.