How to use Python unittests – with examples

  • Reading time:21 mins read

What is a Python unittest?

Python unittesting is a type of software testing where individual units of a program are tested to ensure that they meet a given set of standards and that it is performing as intended. in this article, we’ll show you how to use Python unittesting methods.


What is Python unittesting used for?

Python unittests can be used to test single functions, entire modules or a complete interface. In this article, we will write tests on a simple function using a common testing framework called unittest. The function below takes in a number as an argument and adds 10 to it.




And if we change the argument that we are passing to the function from 5 to 7 then we get a different output.




Now we want to write some tests for this simple function to ensure that it performs as we would expect. Tests are normally written in a different file that is conventionally named in two ways.

The file name should be preceded or followed by the word ‘test’ separated from the filename by an underscore.


Python unittest  

We are going to import the Python unittest framework as well as the function that we are running tests on which in this case is named add_10. All tests need to be under a descriptive class in this case we will name it ‘AdditionTest’.

We also need to inherit from unittest.TestCases. Now we can start to write the basic tests for our code.  We want to write a test method named ‘testadd’ and we are going to give it some numbers to test and assert that the actual outcome that we will get is the one we were expecting.  




Now to run the above code we can run the command below in our Terminal. However, please note that this will only work if you’re running Python3 on your machine.  

py -m unittest


Basically, the above test checks if the number that we have given is the output that comes out of the function. Now there is a more precise way of running the above code instead of having to type that command every time we need to run a test.
At the bottom of our file containing our test case we are going to do an if statement that says that if the name of the file is equal to the same thing that we are running, we will run unittests.main().
Now we are going to modify the function  ‘test_add’ and run a few more tests on integer, float and fractional numbers. As shown below.
Despite the impression created by the code, we have modified that we are running several tests; this is actually one test as manifested in the output. This is because we only have one test method.
Now suppose we pass in a wrong output as the expected output in the test method above, it is likely that the test will fail. As we would see in the code below we have changed the expected output in the first statement to 26 while the correct one should be 25.
From the output above it is clear that we have an AssertionError which means that the differences between what came out of the function and what we were expecting through this test are different.
So now we want to test that the function raises the correct errors when given the wrong data. Suppose that we try to add the string ‘cabbage’ to 10 we are going to get a Type Error. Now, this is good because the function is already throwing an error as we expected.
On the other hand, if we try to add a different string such as ‘True’ to the number 10 we do not get an error.
Once we pass in the word ‘true’ as an argument to the function one would expect that we should get some kind of error or perhaps not get any output at all.
On the contrary, we are getting eleven as though we had passed one as the argument, now this is not good because it implies that we can have the wrong input being fed into the function and still have an output.  
python unittest
Now to avert this, we can write another test that checks for the TypeError. Furthermore, we want to use the context manager to make sure that it is throwing an error.
Now we can say that our data is equal to the test,  and we’ll do self.assertRaises(TypeError) to make sure that it raises the Type Error.
From the output, it is evident that we have run two tests that have all passed successfully. Now suppose we revert back to the string True that was also not throwing any error. In this case, we are going to have a failure.
As we can see in the output the TypeError was not raised as we had earlier intended. On the other hand, if we run the function add_10 individually it gives us an actual answer of 11.
This means that we need to add a little more validation into our function first. Therefore, we are going to say that if the type of x is neither an integer nor a floating-point number then raise a TypeError.  



If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

Programming tutorials

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!





Not sure if programming is for you? With CodeBerry you’ll like it.