Tested on:Raspberry Pi 2; Raspberry Pi B+; Python 2.7; Raspbian pre Jessie
Prerequisites:Raspberry Pi with Raspbian Jessie and internet connection (see steps 1-7 in the Introduction)
0

Python project setup for test-driven development

Real test-driven development (writing the tests before the code) is often impossible when programming a Raspberry Pi. For example, how should the test suite verify that triggering an rc switch with a 433Mhz Sender really happened? A very sophisticated hardware setup would be required.
It is still a good idea to practise TDD whenever possible, test everything that could be tested and to write manually executable hardware tests with comments what should happen (e.g. ‘# Running this test activates the rc switch for 10 seconds.’).

Install the test-driven setup
Boot the Raspberry Pi, then type in terminal

The installed modules

  • Pytest for running the tests
  • Coverage for code coverage measurement
  • Sphinx for documenting the project
  • Ipython debugger ipdb for Bugfixing
  • Use mock to simulate hardware events

Start the dummy project

Run the tests

If the installation passed, this yields one successfull and one failed test:

TDD setup failed test

TDD setup failed test

See the README.md with sudo nano ~/MyProject_tdd/README.md for more usage information.

Virtual environment
If you want to keep the dependencies bound to the application instead of a system wide installation, setup and activate a virtual environment before installing the requirements.txt with pip.

Test-driven project file structure
In addition to the files from the basic python project setup, there are some more files:

Complex file structure

Test-driven project setup

This project setup has a sample documentation build with Sphinx and is setup for test-driven development with the pytest-coverage test framework.
Test framework file structure

Test framework file structure

The additional files are

  • htmlcov: directory for storing code coverage measurement results
  • _build: stores the Sphinx html documentation
  • index.rst: main file for building the documentation in ReStructured Text format
  • MyProject/tests/test_myproject.py: sample test file
Links
Test-driven development wiki: https://en.wikipedia.org/wiki/Test-driven_development
Home automation with rc switches: http://www.knight-of-pi.org/cheap-home-automation-with-a-raspberry-pi-controlling-rc-switches-with-pi-switch/
Coverage tutorial: http://www.knight-of-pi.org/using-coverage-on-a-raspberry-pi/
Sphinx Tutorial: http://www.knight-of-pi.org/generating-the-documentation-of-raspberry-pi-python-projects-with-sphinx/
Mock Tutorial: http://www.knight-of-pi.org/software-testing-mock-edge-values-or-physical-interactions/
Virtual environment tutorial: http://www.knight-of-pi.org/virtual-environments-simplify-project-management/
Basic python project setup: http://www.knight-of-pi.org/standard-python-project-file-setup/

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.

Comment

Time limit is exhausted. Please reload CAPTCHA.