幾何変換用のクラスを作成します。単体テストを実行すると、メソッド内の操作に起因する丸め誤差が原因で失敗します。
私のテストでは、ポイント (2,2,0) を返すメソッドの 1 つからの結果を比較しましたが、丸め誤差のために (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)
計算の観点からは許容できますが、単純な単体テストでコードが失敗することは望ましくありません。
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")
そのような問題を解決する正しい方法は何ですか? 明らかに、通常はそうではないため、出力数値を切り上げたり、整数に変換したりすることはできません。丸め誤差を無視するように単体テストをコーディングする方法はありますか? 問題を解決する他の方法はありますか?
編集:質問は、別の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")
しかし、後でベクトルフィールドのサンプルポイントの座標としてタプルのリストをテストする必要があるため、何らかの方法で自動化する必要があります。
リストに100個のタプルがある場合、300個以上の比較を書くのは少し面倒なので、重複として提案されたソリューションは半分の測定値にすぎません。