python unittest由舍入错误引起的失败

tom*_*z74 5 python unit-testing rounding

我为几何变换构建了类.当我运行单元测试时,由于我的方法中的操作产生的舍入错误,它会失败.

在我的测试中,我比较了应该返回点(2,2,0)的方法之一的结果,但由于舍入错误它返回(1.9999999999999996,1.9999999999999996,0.0)

Finding files... done.
Importing test modules ... done.

** DEBUG_45 from the method point=(1.9999999999999996, 1.9999999999999996, 0.0)
======================================================================
FAIL: testPointCoord (vectOper.test.TestNearestPoint.TestNearestPoint)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\src\vectOper\test\TestNearestPoint.py", line 14, in testPointCoord
self.assertEqual(pointCoord, (2,2,0), "nearest point failed")
AssertionError: nearest point failed

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (failures=1)
Run Code Online (Sandbox Code Playgroud)

从计算的角度来看,这是可以接受的,但我不希望我的代码在简单的单元测试中失败.

import unittest
from vectOper.nearestPoint import NearestPoint

class TestNearestPoint(unittest.TestCase):

    def testPointCoord(self):
        nearestPoint = NearestPoint()
        pointCoord = nearestPoint.pointCoord(samplePoint=(2,2,2),lineStart=(0,0,0), lineVect=(1,1,0))
        self.assertEqual(pointCoord, (2,2,0), "nearest point failed")
Run Code Online (Sandbox Code Playgroud)

什么是解决这类问题的正确方法?显然我不能将输出数字向上舍入或将它们转换为整数,因为通常情况并非如此.有没有办法代码单元测试忽略舍入错误?有没有其他方法可以解决问题?

编辑:问题可以通过使用另一个答案self.assertAlmostEqual正确建议解决但问题是我需要测试一个元组的入口.在我尝试做所有建议之后:

def testPointCoord(self):
    nearestPoint = NearestPoint()
    pointCoord = nearestPoint.pointCoord(samplePoint=(2,2,2),lineStart=(0,0,0), lineVect=(1,1,0))
    self.assertAlmostEqual(pointCoord[0], 2, places=7, msg="nearest point x-coodr failed")
    self.assertAlmostEqual(pointCoord[1], 2, places=7, msg="nearest point y-coodr failed")
    self.assertAlmostEqual(pointCoord[2], 0, places=7, msg="nearest point z-coodr failed")
Run Code Online (Sandbox Code Playgroud)

但是我需要以某种方式自动化它,因为稍后我需要测试一个元组列表作为矢量场的样本点坐标.

建议作为重复的解决方案只是一半的措施,因为如果列表中有100个元组,则会有点单调乏味地写300个比较.

Luc*_*iro 4

为什么不在使用映射的每个维度中使用assertAlmostEqual?我无权访问你的课程,所以我在这里写了一个类似的例子:

from unittest import TestCase

class Test_Tuple_Equality(TestCase):
    def test_tuple_equality_True(self):
        p1 = (0.00000001, 0.00000000001, 0)
        p2 = (0,0,0)
        map(lambda x, y: self.assertAlmostEqual(x,y), p1, p2)

    def test_tuple_equality_False(self):
        p1 = (0.00000001, 0.00000000001, 0)
        p2 = (1,0,0)
        map(lambda x, y: self.assertAlmostEqual(x,y), p1, p2)
Run Code Online (Sandbox Code Playgroud)

Map 会将您的 n 维元组比较转换为 n 浮点数比较。

您甚至可以创建一个 Compare_points 函数,例如:

def compare_points(self, p1, p2):
    map(lambda x,y: self.assertAlmostEqual(x,y), p1,p2)
Run Code Online (Sandbox Code Playgroud)

然后在你的测试中使用它

另一个解决方案是使用 numpy 的方法:

import numpy

>>>numpy.testing.assert_almost_equal((2,2,0), (1.9999999999,2,0), decimal=7, err_msg='', verbose=True)
Run Code Online (Sandbox Code Playgroud)

Numpy 安装起来很麻烦,但是,如果您已经使用它,那么它将是最合适的。