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))
((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 \
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