Python > Testing in Python > Unit Testing with `unittest` > Writing Test Cases
Basic Unit Test with `unittest`
This example demonstrates a basic unit test using Python's `unittest` framework. It covers defining a test class, writing test methods, and using assertions to verify expected outcomes.
Code Example
The code defines a simple `add` function and a `TestAdd` class that inherits from `unittest.TestCase`. Each method in the `TestAdd` class starting with `test_` represents a test case. The `assertEqual` method is used to assert that the result of `add` function matches the expected value. `unittest.main()` is called to run the tests when the script is executed.
import unittest
def add(x, y):
return x + y
class TestAdd(unittest.TestCase):
def test_add_positive_numbers(self):
self.assertEqual(add(2, 3), 5)
def test_add_negative_numbers(self):
self.assertEqual(add(-1, -2), -3)
def test_add_mixed_numbers(self):
self.assertEqual(add(5, -2), 3)
if __name__ == '__main__':
unittest.main()
Concepts Behind the Snippet
This snippet showcases the core principles of unit testing. It involves isolating a specific unit of code (the `add` function in this case), defining different scenarios (positive, negative, mixed numbers), and using assertions to confirm that the unit behaves as expected under those scenarios. Unit tests aim to find bugs early in the development process and make the code more robust and reliable.
Real-Life Use Case
Imagine you are building a complex calculator application. You would write unit tests for each function (addition, subtraction, multiplication, division) to ensure they return correct results for a wide range of inputs. This helps catch errors early and prevent them from propagating to higher-level features.
Best Practices
Interview Tip
Be prepared to discuss the importance of unit testing, how to write effective test cases, and different types of assertions available in `unittest`. You might be asked to write a unit test for a given function or class during the interview.
When to Use Them
Use unit tests for:
Memory Footprint
Unit tests generally have a small memory footprint because they operate on small, isolated units of code. The memory used by the test framework and the test data is typically minimal.
Alternatives
Other Python testing frameworks include:
Pros
Cons
FAQ
-
How do I run the unit tests?
Save the code as a Python file (e.g., `test_add.py`) and run it from the command line using `python test_add.py`. -
What if a test fails?
The `unittest` framework will provide a traceback indicating which test failed and the reason for the failure. Examine the traceback and the code to identify the bug and fix it. -
How do I test code that depends on external resources?
Use techniques like mocking to replace the external resource with a controlled substitute during testing. The `unittest.mock` module provides tools for mocking.