Skip to content

Instantly share code, notes, and snippets.

@tomelm
Created February 4, 2015 17:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomelm/b814022b0af0a3967347 to your computer and use it in GitHub Desktop.
Save tomelm/b814022b0af0a3967347 to your computer and use it in GitHub Desktop.
with mock.patch("%s.restart_server" % module_under_test, autospec=True) as mock_restart:
restart_servers_in_datacenter(servers, "sfo")
mock_restart.assert_called_once()
$ py.test beast.py
============================================================================
test session starts
============================================================================
platform linux2 -- Python 2.6.7 -- py-1.4.20 -- pytest-2.5.2
collected 1 items
beast.py F
============================================================================
FAILURES
============================================================================
_________________________________________________________________
test_restart_servers_in_datacenter_with_match
_________________________________________________________________
E AttributeError: 'function' object has no attribute 'assert_called_once'
beast.py:22: AttributeError
============================================================================
1 failed in 0.01 seconds
============================================================================
def restart_servers_in_datacenter(servers, datacenter):
pass
def restart_server(server):
"""Safely restart a server by removing it from the load balancer,
temporarily silencing its monitoring, telling its mom it will be home
before dark, etc.
(The implementation doesn't matter so we'll leave it out, but assume it
makes remote API calls and invokes system commands and generally
performs expensive operations.)
"""
pass
def restart_servers_in_datacenter(servers, datacenter):
for server in servers:
if server.endswith("-%s" % datacenter):
restart_server(server)
import mock
module_under_test = __name__
def test_restart_servers_in_datacenter_with_match():
servers = set(["web1-sfo", "web2-phx", "web3-jfk"])
with mock.patch("%s.restart_server" % module_under_test) as mock_restart:
restart_servers_in_datacenter(servers, "sfo")
mock_restart.assert_called_once()
$ py.test beast.py
============================================================================
test session starts
============================================================================
platform linux2 -- Python 2.6.7 -- py-1.4.20 -- pytest-2.5.2
collected 1 items
beast.py .
============================================================================
1 passed in 0.01 seconds
============================================================================
@bvarghese1
Copy link

Instead of assert_called_once if the developer had mocked restart_server function and asserted if that function was called thrice with the arguments "web1-sfo", "web2-phx", "web3-jfk". Would that have worked?
With reference to this blog post: http://engineeringblog.yelp.com/2015/02/assert_called_once-threat-or-menace.html
/cc @tomelm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment