In this article we will learn about testing in Python. This covers what is unit testing, how to write and run unit tests in Python programming language.
Introduction
Testing is the practice of writing code that helps to find if there are any errors in the actual logic of the program. It does not prove that our logic is correct. It only reports if the conditions given by the tester are handled correctly or not. Testing is generally used to find out logical errors, as syntax errors will be reported by the Python runtime itself.
Unit testing is about specifically testing a unit. A unit in a Python program or script can be an entire module, a single class, a single class, or almost anything in between. Following are some of the reasons why testing is needed:
- Testing makes sure that our codes work properly under a given set of conditions.
- Testing allows us to make sure that changes to the code did not break existing functionality.
- Testing forces us to think about the code under unusual conditions, possibly revealing errors in the process.
Unit Testing in Python
Python’s standard library provides unittest module for performing unit testing on our Python code. A unit test consists of one or more assertions. Assertion is statement which is supposed to be always true. The unittest module contains many assert functions. These functions are available in unittest. TestCase class. One such function is assertTrue( ) which takes an argument and asserts it to be True.
Writing Test Cases
Consider the following program which contains two functions for printing the next prime number:
def is_prime(number):
"""Return True if *number* is prime."""
for element in range(number):
if number % element == 0:
return False
return True
def print_next_prime(number):
"""Print the closest prime number larger than *number*."""
index = number
while True:
index += 1
if is_prime(index):
print(index)
We have two functions, is_prime and print_next_prime. If we wanted to test print_next_prime, we would need to be sure that is_prime is correct, as print_next_prime makes use of it. In this case, the function print_next_prime is one unit, and is_prime is another.
Let’s assume that the above Python code is saved in a file named primes.py. Now, let’s write our test code inside another file named test_primes.py. The test case for checking the function is_prime is as follows:
import unittest
from primes import is_prime
class PrimesTestCase(unittest.TestCase):
"""Tests for `primes.py`."""
def test_is_five_prime(self):
"""Is five successfully determined to be prime?"""
self.assertTrue(is_prime(5))
if __name__ == '__main__':
unittest.main()
The file creates a unit test with a single test case: test_is_five_prime. Using Python’s built-in unittest framework, any member function whose name begins with test in a class deriving from unittest.TestCase will be run, and its assertions checked, when unittest.main() is called.
Running Test Cases
We can run the test cases by running the Python code which contains the test cases. In out example we will use the command python test_primes.py to run the test cases. We’ll see the output of the unittest framework printed on the console:
$ python test_primes.py
E
======================================================================
ERROR: test_is_five_prime (__main__.PrimesTestCase)
----------------------------------------------------------------------
...
The single “E” represents the results of our single test (if it was successful, a “.” would have been printed). We can see that our test failed, the line that caused the failure, and any exceptions raised.
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply