Python单元测试TypeError

时间:2019-03-12 22:19:43

标签: python python-3.x python-unittest

尝试在Python中构建单元测试,但出现此TypeError,提示它错过了必需的潜在参数。 我已经尝试了所有方法,并在此处获得以下代码:

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)

这是错误消息:

======================================================================
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)

这是实际功能之一:

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

0 个答案:

没有答案