Table of Contents

Test-driven development, unit testing & quality assurance

Lecturer: Pietro Berkes, <pietro.berkes_AT_ googlemail _DOT_ com>

Download the cheat sheets with a summary of the tools and methods discussed in the class!

Course material

Slides: class slides (final version)

Exercises:

Outline of the lecture (2h):

  1. Unit testing
    • unittest
    • testing with numpy arrays
    • what to test and how for scientific programming
    • code coverage (coverage.py)
  2. Debugging
    • pdb
  3. Profiling
    • timeit
    • cProfile
  4. Other useful tools
    • documenting: pydoc, doctest
    • static checking: pylint

Many live demos, music and dances!

Good programming practices

Software carpentry in general

Two comprehensive software carpentry courses:

University of Alberta

Tiziano's course at the Bernstein center in Berlin

The classic book about software carpentry: The pragmatic programmer

Coding standards

PEP8, the official Python style guide

pylint , tool to check coding standards

pyflakes, passive code checker (only errors, not style)

pep8, script to check PEP8 coding standard

Test suites

unittest, the standard Python test framework

nose, an alternative framework that simplifies writing tests and allows for extension and customization of test experience. There is a useful option for running a test coverage analysis: nosetests –with-coverage

py.test, another popular alternative

doctest, write tests inside of docstrings

mock, the most popular tool for creating mock objects for testing.

Debugging

pdb, the standard python debugger

winpdb, a graphical interface for pdb. It's platform-independent despite its name.

DDD (DataDisplayDebugger), graphical general-purpose debugger.

Python IDEs often have a built-in debugger.

Profiling

cProfile, the batteries included Python profiler

timeit, built-in Python module to measure the execution time of small code parts

Tools to visualize profiling results:

RunSnakeRun, a wxPython-based visualization of profiling results – very intuitive!

PyCallGraph

GProf2Dot

KCacheGrind