H35am
H35am

Reputation: 818

Python unittest TypeError

Trying to build an unittest in Python, but I get this TypeError where it says that it misses required potential argument. I've tried everything and end up with this code here:

import unittest
from MyCosine import CosineSim, CosineDis

class TestMyCosine(unittest.TestCase):

    x = [3.5 , 3 , 3.5 , 2.5 , 3]
    y = [3.5 , 3 , 4 , 2.5 , 4.5]
    result = 0.9865867

    def testCosineSim(self, result, x, y):
        self.x = x
        self.y = y
        self.result = result
        self.assertEqual(CosineSim(x,y), result, "0.9865867" )

    def testCosineDis(self, result, x, y):
        self.x = x
        self.y = y
        self.result = result
        self.assertEqual(CosineDis(x,y) , result, "0.9865867")


if __name__ == '__main__':
    unittest.main(exit=False)

and this is the error message:

======================================================================
ERROR: testCosineDis (__main__.TestMyCosine)
----------------------------------------------------------------------
TypeError: testCosineDis() missing 3 required positional arguments:     'result', 'x', and 'y'

======================================================================
ERROR: testCosineSim (__main__.TestMyCosine)
----------------------------------------------------------------------
TypeError: testCosineSim() missing 3 required positional arguments: 'result', 'x', and 'y'

----------------------------------------------------------------------
Ran 2 tests in 0.000s

FAILED (errors=2)

This is one of the actual functions:

def CosineDis(x,y):
    result = 1 - distance.cosine(x, y)
    return result

Upvotes: 1

Views: 2492

Answers (2)

niCk cAMel
niCk cAMel

Reputation: 909

The scope of where you define x and where you have used x as an input argument is not the same. Since you seem to want to use static values for x, y and result, we can just put them in setUp() which unit test will detect and run before any test method is called.

Quick editing on the phone (haven't tested) :

import unittest
from MyCosine import CosineSim, CosineDis

class TestMyCosine(unittest.TestCase):
    def setUp(self) :
        self.x = [3.5 , 3 , 3.5 , 2.5 , 3]
        self.y = [3.5 , 3 , 4 , 2.5 , 4.5]
        self.result = 0.9865867

    def testCosineSim(self):
        self.assertEqual(CosineSim(self.x,self.y), self.result, "0.9865867" )

    def testCosineDis(self):
        self.assertEqual(CosineDis(self.x,self.y) , self.result, "0.9865867")


if __name__ == '__main__':
    unittest.main(exit=False)

Upvotes: 3

Alan Borsato
Alan Borsato

Reputation: 256

That is because the variable you created will be be passed automatically to the TestCase. One solution is to pass the parameters manually. Or you could use a solution like parametrized for that:

from parameterized import parameterized

class TestSequence(unittest.TestCase):
    @parameterized.expand([
        [0.9865867, 3.5, 3.5],
        [0.9865867, 3, 3],
    ])
    def testCosineSim(self, result, x, y):
        self.assertEqual(CosineSim(x,y), result)

Upvotes: 1

Related Questions