From 6481dd95e52e01944218864f8838fc5c8eaeab4b Mon Sep 17 00:00:00 2001 From: guyrt Date: Wed, 22 Feb 2012 05:17:37 -0800 Subject: [PATCH] Ready to present. --- Python-10-Testing-your-program.rest | 37 ++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/Python-10-Testing-your-program.rest b/Python-10-Testing-your-program.rest index 5ac4ee0..6f1f303 100644 --- a/Python-10-Testing-your-program.rest +++ b/Python-10-Testing-your-program.rest @@ -246,6 +246,8 @@ Some people develop code by writing the tests first. If you write your tests comprehensively enough, the expected behaviors that you define in your tests will be the necessary and sufficient set of behaviors your code must perform. Thus, if you write the tests first and program until the tests pass, you will have written exactly enough code to perform the behavior your want and no more. Furthermore, you will have been forced to write your code in a modular enough way to make testing easy now. This will translate into easier testing well into the future. +-------------------------------------------------------------------- +An example -------------------------------------------------------------------- The overlap method takes two rectangles (red and blue) and computes the degree of overlap between them. Save it in overlap.py. A rectangle is defined as a tuple of tuples: ((x_lo,y_lo),(x_hi),(y_hi)) @@ -257,7 +259,6 @@ The overlap method takes two rectangles (red and blue) and computes the degree o ((red_lo_x, red_lo_y), (red_hi_x, red_hi_y)) = red ((blue_lo_x, blue_lo_y), (blue_hi_x, blue_hi_y)) = blue - # bug in lo_y vs. hi_x if (red_lo_x >= blue_hi_x) or \ (red_hi_x <= blue_lo_x) or \ (red_lo_y >= blue_hi_x) or \ @@ -333,3 +334,37 @@ to. blue = ((1, 0), (2, 4)) assert overlap(red, blue) == ((1, 3), (2, 4)) + +Run your tests. + +:: + + [rguy@infolab-33 ~/TestExample]$ nosetests + ...F + ====================================================================== + FAIL: test_overlap.test_partial_overlap + ---------------------------------------------------------------------- + Traceback (most recent call last): + File "/usr/lib/python2.6/site-packages/nose/case.py", line 183, in runTest + self.test(*self.arg) + File "/afs/ictp.it/home/r/rguy/TestExample/test_overlap.py", line 19, in test_partial_overlap + assert overlap(red, blue) == ((1, 3), (2, 4)) + AssertionError + + ---------------------------------------------------------------------- + Ran 4 tests in 0.005s + + FAILED (failures=1) + + +Oh no! Something failed. The failure was on line in this test: + +:: + + def test_partial_overlap(): + red = ((0, 3), (2, 5)) + blue = ((1, 0), (2, 4)) + assert overlap(red, blue) == ((1, 3), (2, 4)) + + +Can you spot why it failed? Try to fix the method so all tests pass. \ No newline at end of file -- 2.26.2