Unit testing with pytest and nose

Hans Petter Langtangen [1, 2]

[1] Center for Biomedical Computing, Simula Research Laboratory
[2] Department of Informatics, University of Oslo

Mar 23, 2015

Table of contents

Requirements of the test function
Writing the test function; precomputed data
Writing the test function; exact numerical solution
Testing of function robustness
Automatic execution of tests

Unit testing is widely a used technique for verifying software implementation. The idea is to identify small units of code and test each unit, ideally in a way such that one test does not depend on the outcome of other tests. Several tools, often referred to as testing frameworks, exist for automatically running all tests in a software package and report if any test failed. The value of such tools during software development cannot be exaggerated. Below we describe how to write tests that can be used by either the nose or the pytest testing frameworks. Both these have a very low barrier for beginners, so there is no excuse for not using nose or pytest as soon as you have learned about functions in programming.

Much of the material in this document is taken from Appendix H.6 in the book A Primer on Scientific Programming with Python, 4th edition, by the same author, published by Springer, 2014.

Read »