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

2

6

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.

Youlandayoulton answered 29/5, 2019 at 13:59 Comment(0)
S
8

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')
Stannary answered 19/6, 2019 at 22:48 Comment(1)
Thanks for the clarification and guidance! I will go back and implement this.Youlandayoulton
O
4

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')
Ophthalmoscopy answered 8/7, 2022 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.