) return resp.json() class TestAdd(unittest.TestCase): def test_add_service(self): result = add_service(40, 2) self.assertEqual(result, 42) What if... Sunday, July 28, 13
) return resp.json() class TestAdd(unittest.TestCase): def test_add_service(self): result = add_service(40, 2) self.assertEqual(result, 42) What if... Sunday, July 28, 13
) return resp.json() class TestAdd(unittest.TestCase): def test_add_service(self): result = add_service(40, 2) self.assertEqual(result, 42) What if... Dependency! Sunday, July 28, 13
) return resp.json() class TestAdd(unittest.TestCase): def test_add_service(self): result = add_service(40, 2) self.assertEqual(result, 42) What if... Fragile! Sunday, July 28, 13
your code. • Callable. • Creates Attributes when accessed (new Mock objects) • Record how they’re used (& you can make assertions about that!) Sunday, July 28, 13
>>> from unittest.mock import Mock # Python 3 >>> # Create a Mock object >>> m = Mock() >>> m <Mock id='4311593936'> # Access arbitrary attributes >>> m.some_value <Mock name='mock.some_value' id='4311595472'> # Call arbitrary methods with arbitrary parameters >>> m.get_result(value=42) <Mock name='mock.get_result()' id='4311568144'> # Make assertions about how it was called >>> m.get_result.assert_called_once_with(value=42) Sunday, July 28, 13
>>> from unittest.mock import Mock # Python 3 >>> # Create a Mock object >>> m = Mock() >>> m <Mock id='4311593936'> # Access arbitrary attributes >>> m.some_value <Mock name='mock.some_value' id='4311595472'> # Call arbitrary methods with arbitrary parameters >>> m.get_result(value=42) <Mock name='mock.get_result()' id='4311568144'> # Make assertions about how it was called >>> m.get_result.assert_called_once_with(value=42) Sunday, July 28, 13
>>> from unittest.mock import Mock # Python 3 >>> # Create a Mock object >>> m = Mock() >>> m <Mock id='4311593936'> # Access arbitrary attributes >>> m.some_value <Mock name='mock.some_value' id='4311595472'> # Call arbitrary methods with arbitrary parameters >>> m.get_result(value=42) <Mock name='mock.get_result()' id='4311568144'> # Make assertions about how it was called >>> m.get_result.assert_called_once_with(value=42) Different Mock objects Sunday, July 28, 13
>>> from unittest.mock import Mock # Python 3 >>> # Create a Mock object >>> m = Mock() >>> m <Mock id='4311593936'> # Access arbitrary attributes >>> m.some_value <Mock name='mock.some_value' id='4311595472'> # Call arbitrary methods with arbitrary parameters >>> m.get_result(value=42) <Mock name='mock.get_result()' id='4311568144'> # Make assertions about how it was called >>> m.get_result.assert_called_once_with(value=42) Sunday, July 28, 13
>>> from unittest.mock import Mock # Python 3 >>> # Create a Mock object >>> m = Mock() >>> m <Mock id='4311593936'> # Access arbitrary attributes >>> m.some_value <Mock name='mock.some_value' id='4311595472'> # Call arbitrary methods with arbitrary parameters >>> m.get_result(value=42) <Mock name='mock.get_result()' id='4311568144'> # Make assertions about how it was called >>> m.get_result.assert_called_once_with(value=42) *Still* Different Mock objects! Sunday, July 28, 13
>>> from unittest.mock import Mock # Python 3 >>> # Create a Mock object >>> m = Mock() >>> m <Mock id='4311593936'> # Access arbitrary attributes >>> m.some_value <Mock name='mock.some_value' id='4311595472'> # Call arbitrary methods with arbitrary parameters >>> m.get_result(value=42) <Mock name='mock.get_result()' id='4311568144'> # Make assertions about how it was called >>> m.get_result.assert_called_once_with(value=42) Sunday, July 28, 13
>>> from unittest.mock import Mock # Python 3 >>> # Create a Mock object >>> m = Mock() >>> m <Mock id='4311593936'> # Access arbitrary attributes >>> m.some_value <Mock name='mock.some_value' id='4311595472'> # Call arbitrary methods with arbitrary parameters >>> m.get_result(value=42) <Mock name='mock.get_result()' id='4311568144'> # Make assertions about how it was called >>> m.get_result.assert_called_once_with(value=42) AssertionError if this is wrong. Sunday, July 28, 13
import add_service class TestAdd(unittest.TestCase): def test_add_service(self): with patch("main.requests") as mock_requests: add_service(40, 2) # Call our function # Verify .post() was called as expected mock_requests.post.assert_called_once_with( "http://math.biz/sum", {'operands': [40, 2]} ) Let’s Test! Sunday, July 28, 13
import add_service class TestAdd(unittest.TestCase): def test_add_service(self): with patch("main.requests") as mock_requests: add_service(40, 2) # Call our function # Verify .post() was called as expected mock_requests.post.assert_called_once_with( "http://math.biz/sum", {'operands': [40, 2]} ) Let’s Test! Sunday, July 28, 13
import add_service class TestAdd(unittest.TestCase): def test_add_service(self): with patch("main.requests") as mock_requests: add_service(40, 2) # Call our function # Verify .post() was called as expected mock_requests.post.assert_called_once_with( "http://math.biz/sum", {'operands': [40, 2]} ) Let’s Test! requests is referenced from the main module Sunday, July 28, 13
from main import add_service class TestAdd(unittest.TestCase): def test_add_service(self): with patch("main.requests") as mock_requests: add_service(40, 2) # Call our function # Verify .post() was called as expected mock_requests.post.assert_called_once_with( "http://math.biz/sum", {'operands': [40, 2]} ) Sunday, July 28, 13
from main import add_service class TestAdd(unittest.TestCase): def test_add_service(self): with patch("main.requests") as mock_requests: add_service(40, 2) # Call our function # Verify .post() was called as expected mock_requests.post.assert_called_once_with( "http://math.biz/sum", {'operands': [40, 2]} ) Sunday, July 28, 13
from main import add_service class TestAdd(unittest.TestCase): def test_add_service(self): with patch("main.requests") as mock_requests: add_service(40, 2) # Call our function # Verify .post() was called as expected mock_requests.post.assert_called_once_with( "http://math.biz/sum", {'operands': [40, 2]} ) Sunday, July 28, 13
from main import add_service class TestAdd(unittest.TestCase): def test_add_service(self): with patch("main.requests") as mock_requests: add_service(40, 2) # Call our function # Verify .post() was called as expected mock_requests.post.assert_called_once_with( "http://math.biz/sum", {'operands': [40, 2]} ) Sunday, July 28, 13
from main import add_service class TestAdd(unittest.TestCase): def test_add_service(self): with patch("main.requests") as mock_requests: add_service(40, 2) # Call our function # Verify .post() was called as expected mock_requests.post.assert_called_once_with( "http://math.biz/sum", {'operands': [40, 2]} ) Sunday, July 28, 13
requests from requests import ConnectionError, HTTPError def get_content(url): try: response = requests.get(url) result = json.loads(response.json()) except (ConnectionError, HTTPError): logging.warn("No Result") result = {} return result How do you test this? Sunday, July 28, 13
json.loads(response.json()) except (ConnectionError, HTTPError): logging.warn("No Result") result = {} return result $ coverage run tests.py && coverage report coverage helps you know what code is covered by tests! Sunday, July 28, 13
import time time.sleep(1000) # Expensive! return value + 1 def calc_alpha(self): return self._calc(100) def calc_beta(self): return self._calc(999) How do I test this? Sunday, July 28, 13
import time time.sleep(1000) # Expensive! return value + 1 def calc_alpha(self): return self._calc(100) def calc_beta(self): return self._calc(999) How do I test this? without calling this? Sunday, July 28, 13
Narrowly define your Mock object’s behavior / Expect Assertions if they’re used incorrectly. • Rely on Spec/Autospec’d objects as test objects Sunday, July 28, 13