Unit Testing with unittest
Discover how to write tests to validate your code.
Unit Testing with unittest is a free Learn AI with Python lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Unit Testing with unittest
Unit testing is a method of testing individual units of code to ensure they work as expected. Python’s unittest module is a built-in library for creating and running unit tests.
In this lesson, you’ll learn how to create and execute unit tests using the unittest framework.

2
What Is Unit Testing?
Unit testing involves testing individual functions or methods in isolation to ensure they behave correctly. It helps catch bugs early in the development process and ensures code reliability.
3
Setting Up unittest
The unittest module is included in Python’s standard library, so you don’t need to install anything. To use it, import the module and create test cases by subclassing unittest.TestCase.
# Setting up unittest
import unittest
class TestExample(unittest.TestCase):
def test_addition(self):
self.assertEqual(1 + 1, 2) # Passes
if __name__ == '__main__':
unittest.main()4
Writing Test Cases
To write a test case, define a method in your test class. Each method should start with test_ so that the unittest framework can recognize it as a test case.
# Writing test cases
class TestMathOperations(unittest.TestCase):
def test_multiplication(self):
self.assertEqual(2 * 3, 6) # Passes5
Running Tests
Run tests by executing the script containing your test cases. The unittest framework will automatically discover and execute all test methods.
# Running tests
# Save this file and run it using:
# python test_script.py6
Common Assertions
The unittest module provides several assertion methods:
assertEqual(a, b): Checks ifais equal tob.assertTrue(x): Checks ifxis true.assertIn(a, b): Checks ifais inb.assertRaises(exception): Checks if an exception is raised.
7
Testing Edge Cases
It’s important to test edge cases, such as empty inputs, large numbers, or invalid data, to ensure the robustness of your code.
# Testing edge cases
class TestEdgeCases(unittest.TestCase):
def test_empty_input(self):
self.assertEqual(len([]), 0) # Passes8
Organizing Test Files
For larger projects, organize test cases in a separate folder, typically named tests/, and name test files with the test_ prefix.
9
10
Common Mistakes with unittest
Here are some mistakes to avoid:
- Not naming test methods with the
test_prefix. - Forgetting to handle edge cases in test cases.
- Not running tests regularly during development.
11
What Did We Learn?
In this lesson, you learned:
- What unit testing is and why it’s important.
- How to use the
unittestmodule to write and execute test cases. - The common assertions available in the
unittestframework. - Best practices for organizing test files and handling edge cases.
Great job! Let’s move to the next topic.

Frequently asked questions
Is the “Unit Testing with unittest” lesson free?
Yes — the full text of “Unit Testing with unittest” is free to read here on the web, and the Learn AI with Python course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Unit Testing with unittest”?
Discover how to write tests to validate your code. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Unit Testing with unittest” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn AI with Python lesson?
Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Unit Testing with unittest
- Debugging Techniques
- Using Debugging Tools