Testing

Presented by Adina Howe.

Some notes on testing

Adapted from Titus Brown and Anthony Scopatz.

When we say “testing” we really mean automated testing. The central problems addressed by testing are correctness and reproducibility.

Unit tests address small units of code, like functions. They are used to isolate and nail down and prove the functionality of potentially complicated little functions.

Unless you write flawless, bug-free, perfectly accurate, fully precise, and predictable code every time, you must test your code in order to trust it enough to answer in the affirmative to at least a few of the following questions:

Does your code work?

Always?

Does it do what you think it does?

Does it continue to work after changes are made?

Does it continue to work after system configurations or libraries are upgraded?

Does it respond properly for a full range of input parameters?

What about edge or corner cases?

What’s the limit on that input parameter?

How will it affect your publications?

Assert statements:

Code looks like this:

def combine_values(func, values):
   assert len(values) > 0, 'Cannot combine values from empty list'
   current = values[0]
   for i in range(1, len(values)):
      current = func(current, values[i])
   return current

def add(a,b):
   return a + b

numbers = [1,3,6,7,9]
print combine_values(add, numbers)
print combine_values(add, [])

Nosetests:

We’re going to be using the nose testing framework, which is just a framework that makes it easy to find and execute tests.

Basically, ‘nose’ creates a command ‘nosetests’ that finds and runs tests. The idea is that you won’t need to register new tests.

A test function looks like this:

def test_something():
   # run some code
   # fail loudly or succeed silently

Here’s a real example:

def test_1():
    result = combine_values(add, [1,1,1]):
    assert result == 3

def test_2():
    result = combine_values(add, [0]):
    assert result == 0
comments powered by Disqus

Table Of Contents

Previous topic

Introducing Python functions

Next topic

Introducing Python

This Page




Edit this document!

This file can be edited directly through the Web. Anyone can update and fix errors in this document with few clicks -- no downloads needed.

  1. Go to Testing on GitHub.
  2. Edit files using GitHub's text editor in your web browser (see the 'Edit' tab on the top right of the file)
  3. Fill in the Commit message text box at the bottom of the page describing why you made the changes. Press the Propose file change button next to it when done.
  4. Then click Send a pull request.
  5. Your changes are now queued for review under the project's Pull requests tab on GitHub!

For an introduction to the documentation format please see the reST primer.