1

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
2
  • What do you think is passing arguments to those methods? Commented Mar 12, 2019 at 22:28
  • Correct, you don't even need those arguments there. Commented Mar 12, 2019 at 22:34

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

2 Comments

This worked perfectly, thank you. My test failed :) because the Cosine function returns this value "0.986586764527925" and I only check for seven digit after the comma.
@H35am I'm glad it helped.
1

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.