Tested on:Raspberry Pi 2; Raspberry Pi B+; Python 2.7; Raspbian pre Jessie
Prerequisites:Python project setup for test-driven development

Using Coverage on a Raspberry Pi

In short, software testing is an integral part of modern software development and gives a robust, yet flexible approach to development. Good software tests are especially important for a Raspberry Pi: since testing actuators, sensors and time-dependent behaviour is difficult to impossible, at least the algorithmic parts of the code should be tested with great care.
Coverage was developed by Ned Batchelder. It gives an graphical representation of which parts of the code are covered by the testing framework.

This tutorial shows the usage of coverage based on the sample project setup from the prerequisites.

Omit files from coverage
Open .coveragerc with

In here, exclude any file from the coverage report by adding the files or file patterns below omit

To exclude single lines of python code, mark them with # pragma:no cover, e.g.

Run coverage on the Raspberry Pi

Collect results
Copy the directory ~/MyProject_tdd/htmlcov with scp to the host with

Examine results
The report is in file:///home/YOUR_USERNAME/htmlcov/index.html; just enter this in the web browsers address line. It will look like this, with the percentages of with tests covered lines calculated:

Clickable browser representation of the code coverage

Clickable browser representation of the code coverage

When clicking on a module, the line-wise results of the tests with covered lines marked green and uncovered lines marked red are displayed:
Detailed output of a coverage test run with line wise result

Detailed output of a coverage test run with line wise result

Ned Batchelder - Website: http://nedbatchelder.com
File transfer with SCP: http://www.knight-of-pi.org/scp-copy-linux-raspberry/

Knight of Pi

Johannes Bergs aka Knight of Pi. Diploma in Bioinformatics, some Webdesign and Python coding then. Living in the beautiful city of Vienna.


Time limit is exhausted. Please reload CAPTCHA.