测试函数中的print语句更改测试结果

时间:2017-09-24 23:07:08

标签: python

正在测试的功能:

def first_ball_contacts(self, timestep):
    """Returns the time and all touching ballpairs (ball_1, ball_2) at the time of the first ball-ball collision within the timestep"""

    first_ball_contacts = {'time': timestep, 'collidors': []} # collidors will be Ball

    for ball_1, ball_2 in itertools.combinations(self.balls, 2):

        t = self.collision_time(ball_1, ball_2, timestep)
        if t!=None:
            if t < first_ball_contacts['time']:

                first_ball_contacts['time'] = t
                first_ball_contacts['collidors'] = [(ball_1, ball_2)]

            elif t == first_ball_contacts['time']:

                first_ball_contacts['collidors'].append((ball_1, ball_2))

    return first_ball_contacts

测试功能:

def test_first_ball_contacts_2_balls(self):

    board = Board(width = 100, height = 100, sps = 1) # Same thing with one line changed
    ball_1 = board.add_ball(x = 45, y = 50, r = 5, vx = 2, vy = 0, m = 1)
    ball_2 = board.add_ball(x = 55, y = 50, r = 5, vx = -2, vy = 0, m = 1)

    contacts = board.first_ball_contacts(1)

    print("FROM "+str(contacts))

    self.assertEqual(contacts['time'], 0)
    self.assertEqual(contacts['collidors'], [(ball_1, ball_2)])

当我运行此测试时,它是成功的:

(balls) Sahands-MBP:src sahandzarrinkoub$ python test_boardmodel.py
.......FROM {'time': 0.0, 'collidors': [(<model.boardmodel.Ball object at 0x105786e10>, <model.boardmodel.Ball object at 0x105786e48>)]}
..........
----------------------------------------------------------------------
Ran 17 tests in 0.004s

OK

但是当我删除print语句时(或者如果我将其更改为其他内容,例如我将其更改为print(str(contacts))),则测试失败:

.......F.........
======================================================================
FAIL: test_first_ball_contacts_2_balls (__main__.BoardModelMethods)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_boardmodel.py", line 282, in test_first_ball_contacts_2_balls
    self.assertEqual(contacts['collidors'], [(ball_1, ball_2)])
AssertionError: Lists differ: [(<mo[28 chars]t 0x10c56ae10>, <model.boardmodel.Ball object at 0x10c56add8>)] != [(<mo[28 chars]t 0x10c56add8>, <model.boardmodel.Ball object at 0x10c56ae10>)]

First differing element 0:
(<mod[26 chars]at 0x10c56ae10>, <model.boardmodel.Ball object at 0x10c56add8>)
(<mod[26 chars]at 0x10c56add8>, <model.boardmodel.Ball object at 0x10c56ae10>)

- [(<model.boardmodel.Ball object at 0x10c56ae10>,
?                                            ^^^

+ [(<model.boardmodel.Ball object at 0x10c56add8>,
?                                            ^^^

-   <model.boardmodel.Ball object at 0x10c56add8>)]
?                                            ^^^

+   <model.boardmodel.Ball object at 0x10c56ae10>)]
?                                            ^^^

另外,如果我注释掉print语句,则测试成功。这是我想要了解的非常奇怪的行为。 print语句如何对其打印的字典的竞争产生影响?特别是如果print语句被注释掉了!

编辑:在我评论print声明之后,似乎它仍然没有成功。但是,如果我完全删除它仍然会失败。所以同样的问题仍然存在:为什么这个印刷语句改变了测试的结果?

另外,不要挂断为什么我在打印内写"FROM "。我偶然发现了这种奇怪的行为,并希望对其进行解释。

0 个答案:

没有答案