Git repository for the talk: https://git.becs.aalto.fi/complex-networks/tutorial

CategoryTutorial

The presentation

In this talk, I will cover:

The pieces

Test modules

`nosetests` command line program

Test functions and classes

Assertions

>>> assert_set_equal(set([1, 2, 3]), set([1, 2, 4]) )

AssertionError: Items in the first set but not the second:
3
Items in the second set but not the first:
4

Look at how it prints exactly what the difference is. Maybe you don't even need to go debugging it yourself if this is enough to realize what went wrong.

What assertions are available?

See the list of assert* methods https://docs.python.org/2/library/unittest.html#assert-methods

nose.tools.assert_almost_equal
nose.tools.assert_almost_equals
nose.tools.assert_dict_contains_subset
nose.tools.assert_dict_equal
nose.tools.assert_equal
nose.tools.assert_equals
nose.tools.assert_false
nose.tools.assert_greater
nose.tools.assert_greater_equal
nose.tools.assert_in
nose.tools.assert_is
nose.tools.assert_is_instance
nose.tools.assert_is_none
nose.tools.assert_is_not
nose.tools.assert_is_not_none
nose.tools.assert_items_equal
nose.tools.assert_less
nose.tools.assert_less_equal
nose.tools.assert_list_equal
nose.tools.assert_multi_line_equal
nose.tools.assert_not_almost_equal
nose.tools.assert_not_almost_equals
nose.tools.assert_not_equal
nose.tools.assert_not_equals
nose.tools.assert_not_in
nose.tools.assert_not_is_instance
nose.tools.assert_not_regexp_matches
nose.tools.assert_raises
nose.tools.assert_raises_regexp
nose.tools.assert_regexp_matches
nose.tools.assert_sequence_equal
nose.tools.assert_set_equal
nose.tools.assert_true
nose.tools.assert_tuple_equal

Invoking the python debugger

If a test fails, you can automatically invoke the debugger:

Useful pdb commands:

Full list of commands: https://docs.python.org/2/library/pdb.html#debugger-commands

The ipython debugger is functionally equivalent to pdb.

If you want to invoke the debugger at one specific point, just use the raise keyword at that point:

raise

if n == 5:
    raise

Learning by example

Recommendations for making tests

How to debug a failing test

Full example: Permutations

Prime number testing

Fibonacci numbers

Instructions:

Test-driven development

Instructions:

Euler 001: sum of multiples of 3 and 5

Problem: https://projecteuler.net/problem=1

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

Instructions:

Further topics

Statistical tests

SoftwareTesting2 (last edited 2014-06-05 11:30:40 by RichardDarst)