Replace mock.assert_not_called() with self.assertFalse(mock.called) in unit tests

Bug #1272428 reported by Max Lobur
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Ironic
Fix Released
Medium
Max Lobur

Bug Description

There is no assert_not_called method in mock object.
Here are some debug outs:
(Pdb) perform_mock
<MagicMock name='_perform_node_power_action' id='53761488'>
(Pdb) dir(perform_mock)
['assert_any_call', 'assert_called_once_with', 'assert_called_with', 'assert_has_calls', 'attach_mock', 'call_args', 'call_args_list', 'call_count', 'called', 'configure_mock', 'method_calls', 'mock_add_spec', 'mock_calls', 'reset_mock', 'return_value', 'side_effect']
(Pdb) perform_mock.assert_not_called()
<MagicMock name='_perform_node_power_action.assert_not_called()' id='53796624'>

Since this is mock, it mocks all it's interface (using __getattr__) and returns the new mock as a result of any non-existent method call or non-existent property read. Need to be careful with this.

Currently I found the only usage of non-existent "assert_not_called" here:
https://github.com/openstack/ironic/blob/703309e5be784ff82a35cbfe9d1ad51e79987f30/ironic/tests/drivers/test_deploy_utils.py#L227

But there may be others, like a "assert_not_called" with some typo, which will work the same way...

This should be replaced with
self.assertFalse(perform_mock.called)

Related topic http://stackoverflow.com/questions/12187122/assert-a-function-method-was-not-called-using-mock

Max Lobur (max-lobur)
description: updated
summary: - Replace mock.assert_not_called() to
- self.assertFalse(perform_mock.called) in unit tests
+ Replace mock.assert_not_called() to self.assertFalse(mock.called) in
+ unit tests
summary: - Replace mock.assert_not_called() to self.assertFalse(mock.called) in
+ Replace mock.assert_not_called() with self.assertFalse(mock.called) in
unit tests
Revision history for this message
Max Lobur (max-lobur) wrote :

I also grep'd for assert_was_not_called, assert_has_no_calls - no occurrences found. Seems the one mentioned above is the only

Changed in ironic:
assignee: nobody → Max Lobur (max-lobur)
Revision history for this message
Max Lobur (max-lobur) wrote :

Found one more - assert_called_once https://github.com/openstack/ironic/blob/7e4e859363bd87846323c17a40ea0718340cef71/ironic/tests/conductor/test_manager.py#L427

(Pdb) deploy
<MagicMock name='tear_down' id='59835280'>

(Pdb) dir(deploy)
['assert_any_call', 'assert_called_once_with', 'assert_called_with', 'assert_has_calls', 'attach_mock', 'call_args', 'call_args_list', 'call_count', 'called', 'configure_mock', 'method_calls', 'mock_add_spec', 'mock_calls', 'reset_mock', 'return_value', 'side_effect']

(Pdb) 'assert_called_once' in dir(deploy)
False

(Pdb) deploy.assert_called_once() # This does nothing
<MagicMock name='tear_down.assert_called_once()' id='59810832'>

# It appears in interface if called at least once - be careful in your tests!
(Pdb) 'assert_called_once' in dir(deploy)
True

(Pdb) deploy.assert_called_once() # Still does nothing
<MagicMock name='tear_down.assert_called_once()' id='59810832'>

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

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

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

Reviewed: https://review.openstack.org/70349
Committed: https://git.openstack.org/cgit/openstack/ironic/commit/?id=86fa723d8bad8a121d75699f8a219f01be8a44b5
Submitter: Jenkins
Branch: master

commit 86fa723d8bad8a121d75699f8a219f01be8a44b5
Author: Max Lobur <email address hidden>
Date: Fri Jan 31 08:54:28 2014 -0500

    Replace nonexistent mock assert methods with real ones

    Methods like assert_not_called, assert_called_once, assert_called do
    not exist, they generated on the fly (dummy) and don't assert anything.
    This patch replaces each of nonexistent method calls with real.

    Change-Id: I12ac093b81a7a90b9b3771678ea2cc3c2a40c876
    Closes-Bug: #1272428

Changed in ironic:
status: In Progress → Fix Committed
Thierry Carrez (ttx)
Changed in ironic:
milestone: none → icehouse-3
status: Fix Committed → Fix Released
Thierry Carrez (ttx)
Changed in ironic:
milestone: icehouse-3 → 2014.1
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.