Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions pretext/Functions/ProgramDevelopment.ptx
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ def distance(x1, y1, x2, y2):
<p>We import the test module to enable us to write a unit test for the function.</p>
<program xml:id="ch06_distance1" interactive="activecode" language="python">
<code>
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()
</code>
</program>
<p>The <c>testEqual</c> function from the test module calls the distance function with sample inputs: (1,2, 1,2).
Expand Down Expand Up @@ -85,17 +91,33 @@ def distance(x1, y1, x2, y2):
we compute and return the result.</p>
<program xml:id="ch06_distancefinal" interactive="activecode" language="python">
<code>
import test
import unittest

def distance(x1, y1, x2, y2):
dx = x2 - x1
dy = y2 - y1
dsquared = dx**2 + dy**2
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()
</code>
</program>
<note>
Expand All @@ -117,7 +139,7 @@ test.testEqual(distance(0,0, 1,1), 1.41)
<p>Copy line 11 on to line 12. On line 12, change <c>1.41421</c> to <c>1.41</c>. Run. The test fails.</p>
</li>
<li>
<p>Type <c>, 2</c> after 1.41. (The 2 represents the precision of the test &#x2013; how many digits to the right of the decimal that must be correct.) Run.</p>
<p>Change the line to <c>self.assertAlmostEqual(distance(0,0, 1,1), 1.41, places=2)</c>. (The <c>assertAlmostequal</c> and <c>places=2</c> represent the precision of the test &#x2013; how many digits to the right of the decimal that must be correct.) Run.</p>
</li>
</ul>
</p>
Expand Down