Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么Python中会出现断言错误?_Python_Python 3.x_Unit Testing_Assertion - Fatal编程技术网

为什么Python中会出现断言错误?

为什么Python中会出现断言错误?,python,python-3.x,unit-testing,assertion,Python,Python 3.x,Unit Testing,Assertion,我正在用Python测试一个函数。这是我写的函数 def hypotenuse(a, b): math.sqrt(a**2 + b**2) 我使用了这个测试用例 def test_hypotenuse_1(self): self.assertEqual(funcs.hypotenuse, 3, 4) 出现了这个断言错误 ====================================================================== FAIL: test

我正在用Python测试一个函数。这是我写的函数

def hypotenuse(a, b):
   math.sqrt(a**2 + b**2)
我使用了这个测试用例

def test_hypotenuse_1(self):
   self.assertEqual(funcs.hypotenuse, 3, 4)
出现了这个断言错误

======================================================================
FAIL: test_hypotenuse_1 (__main__.TestCases)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "funcs_tests.py", line 27, in test_hypotenuse_1
    self.assertEqual(funcs.hypotenuse, 3, 4)
AssertionError: <function hypotenuse at 0x7f397f2d79d8> != 3 : 4

我做错了什么?抱歉,如果这是一个基本问题,我是第一次编写代码。

您需要调用该函数,然后指定该调用的结果应该等于什么

def test_hypotenuse_1(self):
    self.assertEqual(funcs.hypotenuse(3, 4), 5)
这表明边为3和4的三角形的斜边等于5

您的测试仍然会失败,因为斜边不会返回结果。它需要:

def hypotenuse(a, b):
    return math.sqrt(a**2 + b**2)
请注意,对于这样的数学函数,通常不应使用等式测试。它使用浮点算法,可能会有舍入误差。您可以使用该函数进行此操作。

您可能需要调用funcs.斜边。你还必须从抵押中归还一些东西。