Introduction to Unit Testing in Python using unittest framework Part 1
The unittest is one of the python’s unit testing frameworks. It was originally inspired by JUnit and has a similar flavor as major unit testing frameworks in other programming languages. Generally these frameworks are referred to as xUnit testing frameworks.
Some of the xUnit testing frameworks are:
- JUnit — Unit testing framework for Java programming language
- PHPUnit — Unit testing framework for PHP programming language
- NUnit — Unit testing framework for C#, VB.NET, and other .NET languages
- CppUnit — Unit testing framework for C++ programming language
- PyUnit,pytest ,nose2— Other unit testing frameworks for Python programming language
- SUnit — Unit testing framework for Smalltalk programming language
What’s unit testing?
Unit testing is a software testing method by which individual units of a program are individually and independently scrutinized to determine if they are working as expected. For example a class or method to calculate interest on loan in a banking system.
Installation
unittest has been built into the Python standard library since version 2.1. Thus no installation is required. It contains both a testing framework and a test runner.
Test Runner is a tool that is used to run or execute tests and export results. It is a library that selects the source code directory and picks the test files to run them for verifying bugs and errors.
Writing Tests
Following the golden rule of Test Driven Development, we’ll be required to write tests before the code under test. To begin create a directory for your project. In this tutorial we can call it intro-unittest for conformity. We’re going to write tests to verify that we can add and remove items from a python list. Without further ado, let’s start writing the tests.
Open the folder we created above in a code editor or IDE of your choice. Inside the folder create a file named test_list.py. Open the file and define a class named TestList as shown on the code snippet below.
class TestList(unittest.TestCase):
TestList is a subclass of unittest.TestCase i.e. it inherits from the unittest.TestCase base class which will be used to create new test cases using available assertion methods.
This tutorial assumes you’re familiar with OOP and Inheritance in python.
In test_list.py import the unittest module and and update TestList class with our first test as shown below:
A few things to note:
- First we’re initializing a python list of car makes. See Line 4.
- The test method test_item_can_be_added_to_list is calling another method add_item that accepts a parameter of a car make. This is the function responsible with adding new items to the list. See Line 8.
- We’re creating another list of car makes with the items we expect after a new item has been added. See line 7.
- Finally we’re using assertEquals method to verify if the list has been updated.
Open Command Prompt or terminal and navigate to the project directory (intro-unittest). Execute the following command to run the test:
python -m unittest test_list.py
The test should fail since the add_item method doesn’t exist yet. See figure below.
Create a new file or module named car_make.py. Add the code as shown on the snippet below:
Update the test file by importing the module and calling of the add_item method with the module name. See line 2 and 9. The code should look like below:
Re-run the test. The test should pass this time. See figure below:
Let’s add our final test to verify whether an item can be successfully removed from the list. Add the following code to the TestList class.
def test_item_can_be_removed_from_list(self):
expected=['Toyota',"BMW"]
self.assertEquals(list.remove_item('Nissan'),expected)
Re-run the test. This time the second test must fail since the remove_item method doesn’t exist in car_make module.
Update the car_make module with remove_item method. The complete code should look as follows:
The complete test class should look like below:
Re-run the tests. You should have two passing tests. See figure below:
In our next tutorial we’re going to improve our tests with fixtures.