Python unittest - unit test example

Today we will learn about python unittest and look through python unit test example programs. In previous tutorial we learned about python zip function.

Python unittest

Python unittest module is used to test a unit of source code. Suppose, you need to test your project. You know what kind of data the function will return. After writing huge code, you need to check it whether the output is correct or not.

Normally, what we do is printing the output and match it with the reference output file or check the output manually.

To reduce this pain, Python has introduced unittest module. Using this module you can check the output of the function by some simple code. In this tutorial we will discuss about basic usage of Python unittest module and write some python unit test cases to test a class functions.

Python Unit Test Example Source

First of all we have to write some code to unit test them. We will have a Python class.

The main purpose of the class is to store and retrieve person’s name. So, we write set_name() function to store the data and get_name() function to retrieve name from the class.

We named the class file as Person.py. And the output of the above code will be like below.

Python unittest structure

Now, let’s learn how to code for unit testing. An individual testcase is created by subclassing unittest.TestCase. By overriding or adding appropriate functions, we can add logic to test. The following code will be succeeded if a is equals to b.

How to run python unittest module

If you’re using PyCharm IDE, you can simply press ctrl+shift+F10 to run unittest module. Otherwise you can use command prompt to run this module. For example, we named the file for unit-testing as Basic_Test.py. So the command to run python unittest will be:


$python3.6 -m unittest Basic_Test.Testing

If you want to see the verbose, then the command will be;


$python3.6 -m unittest -v Basic_Test.Testing

By using the PyCharm, we get the below output.

python-unittest-example

Python Unit Test Outcome & Basic Functions

This unittest has 3 possible outcomes. They are mentioned below:

  1. OK: If all test cases are passed, the output shows OK.
  2. Failure: If any of test cases failed and raised an AssertionError exception
  3. Error: If any exception other than AssertionError exception is raised.

There are several function under unittest module. They are listed below.

Method Checks that
assertEqual(a,b) a==b
assertNotEqual(a,b) a != b
assertTrue(x) bool(x) is True
assertFalse(x) bool(x) is False
assertIs(a,b) a is b
assertIs(a,b) a is b
assertIsNot(a, b) a is not b
assertIsNone(x) x is None
assertIsNotNone(x) x is not None
assertIn(a, b) a in b
assertNotIn(a, b) a not in b
assertIsInstance(a, b) isinstance(a, b)
assertNotIsInstance(a, b) not isinstance(a, b)

Python unit test example

Now it’s time to write unit tests for our source class Person. In this class we have implemented two function – get_name() and set_name().

Now, we will test those function using unittest. So we have designed two test cases for those two function. Have a look at the following code, you will understand it easily.

Note that the unittest module executes the test functions in the order of their name, not in the order they are defined. And since we want our set_name test to execute first, we have named our test case functions as test_0_set_name and test_1_get_name.

Python Unit Test Example Output

Below images show the output produced by our unit test program – both in normal mode and in verbose mode.

python-unit-test-example-verbose

That’s all about Python unittest tutorial. To learn more, read the Official Documentation. For any further query please use the comment box. 🙂

By admin

Leave a Reply

%d bloggers like this: