From f6341c71dd31389a047c5a846be7b6df07437261 Mon Sep 17 00:00:00 2001 From: Ben Waugh Date: Tue, 9 Apr 2013 21:23:53 +0100 Subject: [PATCH] Turn testing cheat sheet into first draft of a real cheat sheet, instead of an overview of testing in Python. --- testing/cheat-sheet.md | 83 ++++++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 31 deletions(-) diff --git a/testing/cheat-sheet.md b/testing/cheat-sheet.md index dbc03e0..d8b9fdb 100644 --- a/testing/cheat-sheet.md +++ b/testing/cheat-sheet.md @@ -1,38 +1,26 @@ -Testing Cheat Sheet -=================== +Python Testing Cheat Sheet +========================== -Terminology ------------ +Defensive programming +--------------------- -* A *unit test* acts on an isolated component within an application. -* A *test fixture* is the input data that a test acts on. -* The *interface* of a function is its public face, defined by its - input and output. -* The *implementation* of a function is how it gets from the input to - the output. -* A *stub* is a very simple implementation of one function that is - used in a test of a different function. +Using an assertion to ensure input is acceptable: -Unit Testing ------------- + def some_function(x): + assert x >= 0 + # ... continue safe in knowledge that x > 0 -* A *normal case* is a test case that reflects what is expected to be - typical usage of a function. -* A *boundary case* is a test case that reflects a less typical but - potentially troublesome type of usage. +Adding an explanatory message to the assertion: -Exceptions ----------- + assert x >= 0, "Function not defined for negative x." -* raise -* catch -* define +Alternatively, raise an exception to indicate what the problem is: -Assertions ----------- + def some_function(x): + if x < 0: + raise TypeError, "Function not defined for negative x." + return 0 -* syntax -* stops execution Unittest -------- @@ -40,11 +28,44 @@ Unittest * extending TestCase * assertions, e.g. self.assertEquals + Nose ---- -* invocation: nosetests -* naming conventions: test_* -* fixtures: setup +To run tests, at the shell prompt, type + + nosetests + +TODO: finish this + +By default, Nose will find tests in files named `test_*`. + + +### A simple test + + from nose.tools import assert_equal + + from mystatscode import mean + + def test_single_value(): + observed = mean([1.0]) + expected = 1.0 + assert_equal(observed, expected) + +### Other assertions + +TODO: finish this + +### Floating point tests + +* assert_almost_equal... + +### Fixtures + +TODO: + +* setup... * per-test fixtures with @with_setup decorator -* assertions, e.g. assert_equal, assert_almost_equal... + + + -- 2.26.2