No such thing as mock.assert_called_once()

Bug #1336766 reported by Pavlo Shchelokovskyy
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
OpenStack Heat
Fix Released
Undecided
Pavlo Shchelokovskyy

Bug Description

Some unit tests use the following assertion on mock objects returned by mock.Mock / mock.MagicMock classes:

mocked.assert_called_once()

This is wrong, as the mock object has only methods assert_called_with() and assert_called_once_with() [1]. Any other method access not defined as mock interface will simply pass/return another mock.Mock object, so those assertions in unit tests are not actually checking anything as they do never raise any error.

The correct usage (if one cares only about call count and not arguments) is

self.assertEqual(1, mocked.call_count)

[1] http://www.voidspace.org.uk/python/mock/mock.html

Changed in heat:
assignee: nobody → Pavlo Shchelokovskyy (pshchelo)
description: updated
Revision history for this message
Pavlo Shchelokovskyy (pshchelo) wrote :

after fixing I get this unit test failing:
heat.tests.test_engine_service.ThreadGroupManagerTest.test_tgm_start_with_lock

So indeed at this place the assertion relied upon was not triggered. Investigating further.

Revision history for this message
Pavlo Shchelokovskyy (pshchelo) wrote :

NBD - that particular failing assert is indeed obsoleted by changes introduced later.

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to heat (master)

Fix proposed to branch: master
Review: https://review.openstack.org/104187

Changed in heat:
status: New → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to heat (master)

Reviewed: https://review.openstack.org/104187
Committed: https://git.openstack.org/cgit/openstack/heat/commit/?id=d6c7183ae2f4d950201e439782dcd7b29fe830c5
Submitter: Jenkins
Branch: master

commit d6c7183ae2f4d950201e439782dcd7b29fe830c5
Author: Pavlo Shchelokovskyy <email address hidden>
Date: Wed Jul 2 16:10:17 2014 +0300

    UnitTests: fix mock assertions usage

    Some unit tests used mock.Mock().assert_called_once() assertion.

    This is wrong as the mock.Mock object has no such interface method,
    so due to mock nature such wrong assertion do essentially nothing
    and always pass.

    Change-Id: I9ed5ba26aa099c362644a4f09d67a2f9bf36f382
    Closes-Bug: #1336766

Changed in heat:
status: In Progress → Fix Committed
Changed in heat:
milestone: none → juno-2
status: Fix Committed → Fix Released
Thierry Carrez (ttx)
Changed in heat:
milestone: juno-2 → 2014.2
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.