我想测试两个对象是否相等。该对象的类型是由ROS(机器人操作系统)定义的类Point。我有以下测试:尝试assertAlmostEqual/assertEqual时不支持的操作数类型(s)
def test_when_getting_position_after_2s_then_position_at_2s_is_returned(self):
self.expected_position.x = -self.radius
self.expected_position.y = 0
self.assertAlmostEqual(
self.expected_position,
self.trajectory.get_position_at(2))
我使用unittest
,当我试图断言,如果他们几乎是相等的,我得到它说的错误:
TypeError: unsupported operand type(s) for -: 'Point' and 'Point'
我得到同样的错误当我使用assertEqual
,我知道我能做到这一点:
self.assertAlmostEqual(self.expected_position.x, self.trajectory.get_position_at(1).x)
self.assertAlmostEqual(self.expected_position.y, self.trajectory.get_position_at(1).y)
不过,我想可以断言的位置,而不是特定的领域。我怎样才能做到这一点?
编辑:异常的完整回溯是:
Error
Traceback (most recent call last):
File "/usr/lib/python2.7/unittest/case.py", line 329, in run
testMethod()
File "/home/m/turtlebot_ws/src/trajectory_tracking/src/test/trajectory/test_astroid_trajectory.py", line 26, in test_when_getting_position_after_1s_then_position_at_1s_is_returned
self.assertAlmostEqual(self.expected_position, self.trajectory.get_position_at(1))
File "/usr/lib/python2.7/unittest/case.py", line 554, in assertAlmostEqual
if round(abs(second-first), places) == 0:
TypeError: unsupported operand type(s) for -: 'Point' and 'Point'
什么是异常的*完整回溯* –
@MartijnPieters,我刚刚编辑我的问题提供完整的追溯。 – lmiguelvargasf