Created
February 4, 2015 17:35
-
-
Save tomelm/b814022b0af0a3967347 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 | |
============================================================================ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def restart_servers_in_datacenter(servers, datacenter): | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 | |
============================================================================ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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