Saturday, June 18, 2016

Unit Testing and Code Coverage in Python

Unit Test is one of the most important part of development artifacts, which helps to automatically test whatever code we write. Coverage report measures quality of Unit Tests and points out unused/untested code. This tutorial will help to get started with Unit Testing and Coverage report in Python.
For this tutorial we will use Eclipse PyDEV as Python IDE and its integrated Coverage plugin to generate Code Coverage report.

Sample Code:
Download sample code from GitHub. This includes a function (under /src) to get ODD and EVEN numbers for a supplied Count. If supplied count is ODD, it returns all ODD numbers and vice versa. For this implementation, we have written Tests (under /test) which validates count of numbers returned. Count of numbers returned will be half of Supplied Count e.g. Supplied Count 100, it'll return 50 EVEN numbers.

Project Structure:

Create Python Project ("UnitTest") and then create 2 Source Folders under this Project- "src" and "test". Source codes are kept under "src" and respective Unit Test scripts under "tests" Folder. Unit Test scripts are created using same Package structure as of the actual script but Module name prefixed with "test_".

Unit Tests:

Python (since 2.1) includes Unit Test package (unittest). It helps to test scenarios of functions/classes and assert conditions. We'll cover more about unittest functionality in upcoming tutorials. In this session, I'll cover very simple assertion (assertEqual), which validates equality.

import unittest

import me.pras.data as data

classTest(unittest.TestCase):

deftestData(self):

count = 9

expectedCount = count/2

nums = data.getNumbers( count )

self.assertEqual( len( nums ),
expectedCount, 'Count mismatch...')

if __name__ == "__main__":

unittest.main()

Above code validates functionality of me.pras.data.getNumbers() function. If we run this Test (Right click on test_data.py - Run As - Python unit-test)

Drag and Drop our Project "UnitTest" at the bottom area of "Code Coverage" view.

If you have already run the Unit Tests, it'll show report on right-hand side of "Code Coverage" view. As you can see, we are passing only EVEN count as a part of Unit Test, so it has marked else sectionand_odd() functions as untested.

Isn't it Cool ? It points out any untested code and measures accuracy of your Unit Tests.