diff --git a/pretext/Functions/ProgramDevelopment.ptx b/pretext/Functions/ProgramDevelopment.ptx index 2ba59fb9..c1621e61 100644 --- a/pretext/Functions/ProgramDevelopment.ptx +++ b/pretext/Functions/ProgramDevelopment.ptx @@ -31,11 +31,17 @@ def distance(x1, y1, x2, y2):

We import the test module to enable us to write a unit test for the function.

-import test +import unittest + def distance(x1, y1, x2, y2): return 0.0 -test.testEqual(distance(1, 2, 1, 2), 0) +class TestDistance(unittest.TestCase): + def test_distance(self): + self.assertEqual(distance(1, 2, 1, 2), 0) + +x = TestDistance('test_distance') +x.run()

The testEqual function from the test module calls the distance function with sample inputs: (1,2, 1,2). @@ -85,7 +91,8 @@ def distance(x1, y1, x2, y2): we compute and return the result.

-import test +import unittest + def distance(x1, y1, x2, y2): dx = x2 - x1 dy = y2 - y1 @@ -93,9 +100,24 @@ def distance(x1, y1, x2, y2): result = dsquared**0.5 return result -test.testEqual(distance(1,2, 1,2), 0) -test.testEqual(distance(1,2, 4,6), 5) -test.testEqual(distance(0,0, 1,1), 1.41) +class TestDistance(unittest.TestCase): + def test_distance_zero(self): + self.assertEqual(distance(1,2, 1,2), 0) + + def test_distance_five(self): + self.assertEqual(distance(1,2, 4,6), 5) + + def test_distance_square(self): + self.assertEqual(distance(0,0, 1,1), 1.41) + +x = TestDistance('test_distance_zero') +x.run() + +x = TestDistance('test_distance_five') +x.run() + +x = TestDistance('test_distance_square') +x.run() @@ -117,7 +139,7 @@ test.testEqual(distance(0,0, 1,1), 1.41)

Copy line 11 on to line 12. On line 12, change 1.41421 to 1.41. Run. The test fails.

  • -

    Type , 2 after 1.41. (The 2 represents the precision of the test – how many digits to the right of the decimal that must be correct.) Run.

    +

    Change the line to self.assertAlmostEqual(distance(0,0, 1,1), 1.41, places=2). (The assertAlmostequal and places=2 represent the precision of the test – how many digits to the right of the decimal that must be correct.) Run.