Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Mock - Mocking and Testing Library, An Introduction

Mock - Mocking and Testing Library, An Introduction

This deck goes a little deeper into the mocking framework, "mock".

Burton Williams

March 22, 2014
Tweet

More Decks by Burton Williams

Other Decks in Programming

Transcript

  1. About me { “name”: “Burton Williams”, “title”: “Software Engineer Contractor”,

    “email” : “[email protected]”, “github” : “ikiini”, “origin_country”: “St Vincent and the Grenadines” }
  2. Agenda • What is Mock? • Why use Mock? •

    Basics. • Useful features. – patch. – side_effect. • Other features.
  3. Agenda • What is Mock? • Why use Mock? •

    Basics. • Useful features. – patch. – side_effect. • Other features.
  4. What is Mock? “Mock is a library for testing in

    Python. It allows you to replace parts of your system under test with mock objects and make assertions about how they have been used”
  5. Agenda • What is Mock? • Why use Mock? •

    Basics. • Useful features. – patch. – side_effect. • Other features.
  6. Why use Mock? • No more writing stubs. • The

    ability to change behavior of an object in the context of a test without affecting other tests that use the same object. • Test flexibility • Its easy!!!
  7. Agenda • What is Mock? • Why use Mock? •

    Basics. • Useful features. – patch. – side_effect. • Other features.
  8. Basics - creation • As simple as import and instantiate.

    – >>> from mock import Mock – >>> my_mock_object = Mock()
  9. Basics - usage • Add attributes with values. – >>>

    my_mock_object.attr_x = 3 • Treat it like a method and add a return value. – >>> my_mock_object.return_value = 42 – >>> my_mock_object() – ... 42 • Monkey patch a method on a class we're testing. (extending on example above) – >>> class RealDeal(object): – ... def a_func(): – ... return “a_func was called” – >>> real = RealDeal() – >>> real.a_func = my_mock_object – >>> real.a_func() – ... 42
  10. Agenda • What is Mock? • Why use Mock? •

    Basics. • Useful features. – patch. – side_effect. • Other features.
  11. Useful features - patch • Good for mocking classes instantiated

    in your code under test. • Access the mock that replaces the class by using the mock's return value. You can configure the mocked class however you need to from here. • Can be used in with statements or as a decorator on a test case or test class. As a decorator it comes in 3 flavors: – @patch – @patch.object – @patch.dict
  12. Useful features – patch examples • with statement: • >>>

    with patch('package.module.Class') as mock: • ... class_instance = mock.return_value • ... class_instance.y = 2 • ... class_instance.method.return_value = 42 • ... real = package.module.Class() • ... real.method() # will return 42
  13. Useful features – patch examples • @patch: – >>> from

    package.module import Class – >>> class ClassUnderTest(object): – ... def some_method(): – ... my_class = Class() – ... return my_class.method() – >>> – >>> class MyTestCase(unittest.TestCase): – ... @patch('package.module.Class') – ... def myTest(self, mock_class): – ... class_instance = mock_class.return_value – ... class_instance.method.return_value = 42 – ... real = ClassUnderTest() – ... self.assertEqual(42, real.method())
  14. Useful features – side_effect • Allows you to vary the

    return values of a mock object. • Example: – >>> mock = Mock(side_effect=[4, 5, 6]) – >>> mock() – 4 – >>> mock() – 5 – >>> mock() – 6
  15. Useful features – side_effect • Example cont'd: >>> vals =

    {(1, 2): 1, (2, 3): 2} – >>> def side_effect(*args): – ... return vals[args] – ... – >>> mock = Mock(side_effect=side_effect) – >>> mock(1, 2) – 1 – >>> mock(2, 3) – 2
  16. Agenda • What is Mock? • Why use Mock? •

    Basics. • Useful features. – patch. – side_effect. • Other features.
  17. Other features • Method call tracking • Exception raising •

    MagicMock vs Mock. – Pretty much the same thing, except that MagicMock has magic functions already built onto the mock. • Create a mock from an existing class.