- Phase: 8. Data, Web & APIs
- Duration: 2 hours
- Understand why testing is important
- Write tests using the unittest module
- Use TestCase and assert methods
- Set up test fixtures with setUp/tearDown
- Discover and run tests with python -m unittest
- Organize tests in a project
- Why testing matters
- unittest module and TestCase class
- Assert methods (assertEqual, assertTrue, assertRaises, etc.)
- setUp and tearDown methods
- Test discovery
- Organizing test files
Modules 000-073.
import unittest
def add(a: int, b: int) -> int:
return a + b
class TestMath(unittest.TestCase):
def test_add(self) -> None:
self.assertEqual(add(2, 3), 5)
self.assertNotEqual(add(2, 2), 5)
if __name__ == '__main__':
unittest.main()- Python docs: unittest module
- Python Testing with unittest (Real Python)