Skip to content

Commit 184559b

Browse files
committed
change distance tests to use unittest
The exact wording and text in the "Fix the error" section need updating, but the provided code will now work in CodeLens with the current PythonTutor version. Fixes #279.
1 parent d3c2945 commit 184559b

1 file changed

Lines changed: 29 additions & 7 deletions

File tree

pretext/Functions/ProgramDevelopment.ptx

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,17 @@ def distance(x1, y1, x2, y2):
3131
<p>We import the test module to enable us to write a unit test for the function.</p>
3232
<program xml:id="ch06_distance1" interactive="activecode" language="python">
3333
<code>
34-
import test
34+
import unittest
35+
3536
def distance(x1, y1, x2, y2):
3637
return 0.0
3738

38-
test.testEqual(distance(1, 2, 1, 2), 0)
39+
class TestDistance(unittest.TestCase):
40+
def test_distance(self):
41+
self.assertEqual(distance(1, 2, 1, 2), 0)
42+
43+
x = TestDistance('test_distance'))
44+
x.run()
3945
</code>
4046
</program>
4147
<p>The <c>testEqual</c> function from the test module calls the distance function with sample inputs: (1,2, 1,2).
@@ -85,17 +91,33 @@ def distance(x1, y1, x2, y2):
8591
we compute and return the result.</p>
8692
<program xml:id="ch06_distancefinal" interactive="activecode" language="python">
8793
<code>
88-
import test
94+
import unittest
95+
8996
def distance(x1, y1, x2, y2):
9097
dx = x2 - x1
9198
dy = y2 - y1
9299
dsquared = dx**2 + dy**2
93100
result = dsquared**0.5
94101
return result
95102

96-
test.testEqual(distance(1,2, 1,2), 0)
97-
test.testEqual(distance(1,2, 4,6), 5)
98-
test.testEqual(distance(0,0, 1,1), 1.41)
103+
class TestDistance(unittest.TestCase):
104+
def test_distance_zero(self):
105+
self.assertEqual(distance(1,2, 1,2), 0)
106+
107+
def test_distance_five(self):
108+
self.assertEqual(distance(1,2, 4,6), 5)
109+
110+
def test_distance_square(self):
111+
self.assertEqual(distance(0,0, 1,1), 1.41)
112+
113+
x = TestDistance('test_distance_zero')
114+
x.run()
115+
116+
x = TestDistance('test_distance_five')
117+
x.run()
118+
119+
x = TestDistance('test_distance_square')
120+
x.run()
99121
</code>
100122
</program>
101123
<note>
@@ -117,7 +139,7 @@ test.testEqual(distance(0,0, 1,1), 1.41)
117139
<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>
118140
</li>
119141
<li>
120-
<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>
142+
<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>
121143
</li>
122144
</ul>
123145
</p>

0 commit comments

Comments
 (0)