From: Ben Waugh Date: Thu, 11 Apr 2013 10:19:32 +0000 (+0100) Subject: Add fixture examples to testing cheat sheet. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=f14706e0b8b8106dab245eadb40bea4fd3daee94;p=swc-testing-nose.git Add fixture examples to testing cheat sheet. --- diff --git a/testing/cheat-sheet.md b/testing/cheat-sheet.md index d7f48a0..74e7106 100644 --- a/testing/cheat-sheet.md +++ b/testing/cheat-sheet.md @@ -98,7 +98,37 @@ Testing that a method raises the appropriate exception when the input is invalid ### Fixtures -TODO: +A *fixture* is what the test function uses as input, e.g. values, objects and arrays. -* setup... -* per-test fixtures with @with_setup decorator +To set up a fixture once before any tests are run, define a method called `setup` in the same files +as the test functions. This can assign values to global variables for use in the test functions. + + long_list = None + + def setup(): + long_list = [0] + # append more values to long_list... + +If the global variables assigned in `setup` might be modified by some of the test functions, the set-up +step must be executed once before each test function is called: + + from nose.tools import with_setup + + from mycode import mean, clear + + long_list = None + + def setup_each(): + long_list = [0] + # append more values to long_list... + + @with_setup(setup_each) + def test_mean_long_list(): + observed = mean(long_list) + expected = 0.0 + assert_equal(observed, expected) + + @with_setup(setup_each) + def test_clear_long_list(): + clear(long_list) + assert_equal(len(long_list), 0)