1

So I recently started to get into the topic of unit testing in the python world, and I got stuck on the following issue:

def test_correct_object_on_queue(self):
    self.logger.log('Starting test for correct object state on queue')

    called_with_args = False

    icc = Icc(self.mock_io, '0', self.logger, Queue())

    icc.center_module()

    calls = icc.dxl_io.set_goal_position.mock_calls

    for c in calls:
        # print c
        if c == call({'0': 0}):
            called_with_args = True
            break

    assert called_with_args == True

Here is the definition of the center_module:

def center_module(self):
    self.add_to_queue(self.dxl_io.set_goal_position, [{self.servo_ids[0]: 0}])

If I uncomment the # print c line, this test will pass. Here is what my calls contain: [call.__str__(), call.__str__(), call.__nonzero__(), call({'0': 0}), call.__str__()]

You might ask why don't I use something built in method related to my purpose, such as assert_called_with of the Mock (and thus, MagicMock) class. Well, during debugging I noticed how they can't work with complex data structures such as a dictionary (the expected variable in the assert method will be a tuple: <type 'tuple'>: (({'0': 0},), {}) and it returns this as actual: call({'0': 0}), obviously their comparison will fail), and following the suggestion here I wanted to go with the approach described above.

My question (aside how to fix?) is why is the test only finding the call with the args if I ask it to print all the calls?

Community
  • 1
  • 1
pszent
  • 55
  • 1
  • 1
  • 7

0 Answers0