Category Archives: Python unittest

Python testing with unittest module | Testing mathematical operations

The unittest module is part of Python’s standard library and it is very popular when it comes to testing Python code. You need two files (preferably placed in the same directory), one file that contains the code that is tested and another file with the code that tests the code of the first file.

My first file is named math_operators.py and it contains basic functions I am going to test. The second file is named test_math_operators.py and it is going to contain the testing code.

Note:
I am referring to this documentation when writing the tests on this page: https://docs.python.org/3/library/unittest.html#unittest.TestCase.debug.

Testing Python’s mathematical operators with unittest module

In order to start testing I have created the following basic functions in my math_operators.py file.

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

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

def multiplication(a, b):
	return a * b

def division(a, b):
	if b == 0:
		raise ValueError("Can't divide by zero!")
	return a / b

Continue reading