My Blog
Articles related to programming, computer science, technology and research.

19/04/2018 Categories: Python Programming. No Comments on Testing in Python

Contents1 Introduction2 Unit Testing in Python2.1 Writing Test Cases2.2 Running Test Cases 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 […]

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:

 

 

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:

 

 

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:

 

 

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.

Related Links:

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up