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.
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 test_add_num.py









Summary
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
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
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!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING
Not sure if programming is for you? With CodeBerry you’ll like it.
