====== Test-driven development, unit testing & quality assurance ======

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

{{:software_carpentry_cheatsheets_v1_2_1.pdf|Download the cheat sheets}} with a summary of the tools and methods discussed in the class!

===== Course material =====

Slides: {{:software_carpentry_final.pdf|class slides (final version)}}

Exercises: 

  * {{:day1_exercises_corrected.pdf|day1_exercises.pdf (exercise sheet)}} !! corrected version !!
  * {{:day1_exercises.zip|day1_exercises.zip (source code)}}
  * {{:day1_solutions.zip|Exercise solutions}}

Outline of the lecture (2h):

  - Unit testing
    * unittest
    * testing with numpy arrays
    * what to test and how for scientific programming
    * code coverage (coverage.py)
  - Debugging
    * pdb
  - Profiling
    * timeit
    * cProfile
  - 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:

[[http://software-carpentry.org/|University of Alberta]]

[[http://itb.biologie.hu-berlin.de/~zito/teaching/SC/|Tiziano's course]] at the Bernstein center in Berlin

The classic book about software carpentry: [[http://pragprog.com/titles/tpp/the-pragmatic-programmer | The pragmatic programmer]]

==== Coding standards ====

[[http://www.python.org/dev/peps/pep-0008/|PEP8]], the official Python style guide

[[http://www.logilab.org/857 | pylint ]], tool to check coding standards

[[http://pypi.python.org/pypi/pyflakes | pyflakes]], passive code checker (only errors, not style)

[[http://pypi.python.org/pypi/pep8/0.6.1 | pep8]], script to check PEP8 coding standard
===== Test suites =====

[[http://docs.python.org/library/unittest.html|unittest]], the standard Python test framework

[[http://somethingaboutorange.com/mrl/projects/nose/0.11.1/|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''

[[http://codespeak.net/py/dist/test/test.html|py.test]], another popular alternative

[[http://docs.python.org/library/doctest.html | doctest]], write tests inside of docstrings

[[http://www.voidspace.org.uk/python/mock | mock]], the most popular tool for creating mock objects for testing.
===== Debugging =====

[[http://docs.python.org/library/pdb.html|pdb]], the standard python debugger

[[http://winpdb.org/download/|winpdb]], a graphical interface for ''pdb''. It's
platform-independent despite its name.

[[http://www.gnu.org/software/ddd/|DDD]] (DataDisplayDebugger), graphical general-purpose debugger.

Python IDEs often have a built-in debugger.

===== Profiling =====

[[http://docs.python.org/library/profile.html|cProfile]], the batteries included
 Python profiler

[[http://docs.python.org/library/timeit.html|timeit]], built-in Python module to
 measure the execution time of small code parts

Tools to visualize profiling results:

[[http://www.vrplumber.com/programming/runsnakerun/|RunSnakeRun]], a wxPython-based visualization of profiling results -- very intuitive!

[[http://pycallgraph.slowchop.com/|PyCallGraph]]

[[http://code.google.com/p/jrfonseca/wiki/Gprof2Dot|GProf2Dot]]

[[http://kcachegrind.sourceforge.net/html/Home.html|KCacheGrind]]
