Search code examples
python-unittesttestcase

TypeError: assertEqual() missing 1 required positional argument: 'second'


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.


Solution

  • 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')