Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Unit Testing with unittest | Python Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. Python Fundamentals

Lesson 33 of 55

·Python

Unit Testing with unittest

20 minutes

Course progress60%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • Create a subclass of `unittest.TestCase`.
  • Use assertion methods like `assertEqual`, `assertTrue`, `assertRaises`.
  • Run tests via `python -m unittest discover` for larger suites.

Unit Testing with unittest

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.

Make it stickPractice what you've learned1 quiz · 5 practical tests · Optional activities from Python FundamentalsBrowse activities
Previous lessonContext ManagersContinue to lesson 34LoggingBack to Python Fundamentals curriculum