Unit test framework - unittest in Python

Pranav Bakare - Oct 14 - - Dev Community

In Python, one of the most commonly used unit test frameworks is unittest, which is included in the standard library. It provides a rich set of tools for creating and running tests, as well as reporting results.

Key Features of the unittest Framework

  1. Test Case: A test case is created by subclassing unittest.TestCase. Each method in the class that starts with test_ is considered a test.

  2. Assertions: The framework provides a variety of assertion methods to check for expected outcomes, such as assertEqual, assertTrue, assertFalse, etc.

  3. Test Runner: The framework includes a test runner that runs the tests and reports results.

Basic Structure of a Unit Test

  1. Import the unittest module: Start by importing the unittest library.

  2. Create a Test Case: Define a class that inherits from unittest.TestCase.

  3. Define Test Methods: Each method should begin with test_ and contain assertions to check the expected behavior.

  4. Run the Tests: Use unittest.main() to run the tests if the script is executed directly.

Simple Example

Here’s a straightforward example that demonstrates how to use the unittest framework to test a simple function.

Function to be Tested

First, let’s create a simple function that we will test:

math_functions.py

def add(a, b):
return a + b

def subtract(a, b):
return a - b

Unit Test for the Function

Now, we’ll create a unit test for the add and subtract functions:

test_math_functions.py

import unittest
from math_functions import add, subtract # Import the functions to be tested

class TestMathFunctions(unittest.TestCase):

def test_add(self):
    self.assertEqual(add(2, 3), 5)          # Test case: 2 + 3 = 5
    self.assertEqual(add(-1, 1), 0)         # Test case: -1 + 1 = 0
    self.assertEqual(add(0, 0), 0)          # Test case: 0 + 0 = 0

def test_subtract(self):
    self.assertEqual(subtract(5, 3), 2)     # Test case: 5 - 3 = 2
    self.assertEqual(subtract(-1, -1), 0)   # Test case: -1 - (-1) = 0
    self.assertEqual(subtract(0, 5), -5)    # Test case: 0 - 5 = -5
Enter fullscreen mode Exit fullscreen mode

if name == 'main':
unittest.main()

Explanation of the Unit Test

  1. Import Statements:

import unittest: Imports the unittest module.

from math_functions import add, subtract: Imports the functions we want to test.

  1. Creating a Test Case:

class TestMathFunctions(unittest.TestCase): Defines a test case class that inherits from unittest.TestCase.

  1. Defining Test Methods:

Each method starting with test_ represents a separate test.

Inside each test method, self.assertEqual() checks if the output of the function matches the expected value.

  1. Running the Tests:

if name == 'main': unittest.main(): This line allows the script to run the tests when executed directly.

Running the Tests

To run the tests, simply execute the test_math_functions.py script. You can do this from the command line:

python test_math_functions.py

Output

If all tests pass, you will see output similar to this:

..

Ran 2 tests in 0.001s

OK

This indicates that both the test_add and test_subtract methods passed successfully. If any assertion fails, unittest will report the failure, showing which test failed and why.

Conclusion

The unittest framework in Python provides a simple and effective way to create and run unit tests. By writing tests for your functions, you can ensure that they behave as expected and maintain high code quality throughout your development process.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .