Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 282
chaza68 Avatar asked Sep 04 '26 02:09

chaza68


2 Answers

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')
like image 106
w33b Avatar answered Sep 07 '26 18:09

w33b


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')
like image 30
Crimp City Avatar answered Sep 07 '26 18:09

Crimp City



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!