I am working on a project in Runestone using test.testEqual(). I work with a Anaconda/Spyder console and translate code back into Runestone. Python doesn't seem to support test.testEqual so I have attempted to use TestCase.assertEqual(first,second, msg) method under the unittest framework. My code throws the error message: TypeError: assertEqual() missing 1 required positional argument: 'second'
but as I show in the code below I include both arguments in the call. I am new to unit testing so not sure where to go in order to solve this issue?
switched from test.testEqual() to TestCase.assertEqual(first,second,msg)
from unittest import TestCase
def distance(x1, y1, x2, y2):
dx = x2 - x1
dy = y2 - y1
dsquared = dx**2 + dy**2
result = dsquared**0.5
return result
TestCase.assertEqual(distance(1,2, 1,2),0,msg='Equal')
TestCase.assertEqual(distance(1,2, 4,6), 5, msg='Equal')
TestCase.assertEqual(distance(0,0, 1,1), 2**0.5, msg='Equal')
We would expect the three test cases to Pass based on their execution in Runestone consoles.
I would suggest using TestCase in a different way. Instead make a test class and inherit unittest.TestCase. Add an individual test then you are good to go
class TestDistance(TestCase):
def test_distance(self):
self.assertEqual(distance(1, 2, 1, 2), 0, msg='Equal')
self.assertEqual(distance(1, 2, 4, 6), 5, msg='Equal')
self.assertEqual(distance(0, 0, 1, 1), 2 ** 0.5, msg='Equal')
The approach from w33b works just fine but, for the sake of getting your approach working (since you are very close), you actually just need to instantiate the TestCase class.
The way you've written it, you are only referencing the class; but, in order to use its methods, the class needs to be instantiated with the ().
TestCase().assertEqual(distance(1,2, 1,2),0,msg='Equal')
TestCase().assertEqual(distance(1,2, 4,6), 5, msg='Equal')
TestCase().assertEqual(distance(0,0, 1,1), 2**0.5, msg='Equal')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With