Writing Unit Tests in Python
Unit tests are written to verify the functionality of a specific piece of code, ensuring that it works as intended. These tests confirm that your code functions correctly and that the inputs and outputs align with your expectations.
Writing unit tests in Python is an essential part of ensuring the correctness and robustness of your code. Python provides a built-in module called unittest for writing and running unit tests.
Here's how to write unit tests in Python using the unittest framework:
- Importing the unittest module:
- Creating a test Class:
- Writing test methods:
- Using set up and tear down methods (Optional):
- Running the tests:
Start by importing the unittest module at the beginning of your test file:
import unittest
Create a test class that inherits from unittest.TestCase. This class will contain your test methods. For example:
class MyTestCase(unittest.TestCase):
Create individual test methods within your test class. Test method names should start with the word "test" to be automatically discovered by the test runner. For example:
def test_addition(self):
expected_result = 14
result = 10 + 4
self.assertEqual(result, expected_result)
def test_subtraction(self):
expected_result = 10
result = 15 - 5
self.assertEqual(result, expected_result)
In these examples, assertEqual is an assertion method provided by unittest to check if the given expression is true.
If your tests require some common setup or cleanup, you can use setUp and tearDown methods within the test class. These methods will be called before and after each test method, respectively. For example:
def setUp(self):
# Set up any resources or configurations needed for the tests.
def tearDown(self):
# Clean up after each test.
To run your unit tests in Python, you can run the tests from the command line using the following command:
python -m unittest your_test_file.py
Complete Example
Here's a complete example that demonstrates how to create unit tests using the Python unittest library for a my_calculator module:
def addition(a, b):
return a + b
def subtraction(a, b):
return a - b
import unittest
import my_calculator
class CalculatorTestCase(unittest.TestCase):
def setUp(self):
print("setUp...")
self.num1 = 10
self.num2 = 5
def test_addition(self):
print("testing addition...")
expected_result = 15
result = my_calculator.addition(self.num1, self.num2)
self.assertEqual(result, expected_result)
def test_subtraction(self):
print("testing subtraction...")
expected_result = 5
result = my_calculator.subtraction(self.num1, self.num2)
self.assertEqual(result, expected_result)
def tearDown(self):
print("cleaning up...")
self.num1 = 0
self.num2 = 0
if __name__ == "__main__":
unittest.main()
In this example, we began by creating a testcase class named CalculatorTestCase, by subclassing unittest.TestCase. The two individual test methods, whose names start with the letters "test", inform the test runner about which methods represent tests. The setUp() method is used to initialize the test fixture and is executed before any test method runs. The tearDown() method is used to clean up resources and is executed after each test method. The assertEqual() function checks the two provided values for a true condition.
The output of the above unit test is as follows:
setUp... testing addition... cleaning up... .setUp... testing subtraction... cleaning up... . ---------------------------------------------------------------------- Ran 2 tests in 0.001s OK