intro to mocking - djangocon 2015

Download Intro to Mocking - DjangoCon 2015

Post on 22-Jan-2018

508 views

Category:

Technology

0 download

Embed Size (px)

TRANSCRIPT

  1. 1. Intro To Mocking: Why Unit Testing Doesnt Have to Be So Hard! Daniel Davis @Ooblioob
  2. 2. Software Developer for 8 years Senior Consultant: Excella Consulting in Washington, DC Fun Fact: I ran the Jingle All The Way 5k dressed as a Giant Gingerbread man A Little About Me
  3. 3. 3
  4. 4. Survey Time! 4
  5. 5. Struggled with unit testing until I learned about Mocking Learning To Test 5
  6. 6. Me Learning about Mocking 6 Image credit: http://stylecaster.com/excited-gif/
  7. 7. Many of us are in the same boat 7
  8. 8. I wish someone would just give a talk on this So if thats you We all want to write better tests
  9. 9. Lets Talk About Unit Tests
  10. 10. Unit 70% Integration 20% UI 10% Testing Types
  11. 11. Theyre good when the problem is easy A rabbit hole of testing I spend too much time writing lots of code to test, so I give up Theres just some stuff you cant unit test Great Unit Testing Myths
  12. 12. Mocking makes unit testing easier! 12
  13. 13. What Are Mocks? 13 Test Double Dummy Object Test Stub Test Spy Mock Object Fake Object Commonly referred to as Mocks
  14. 14. Stubs Provide a canned response to method calls Spy Real objects that behave like normal except when a specific condition is met Mocks Verifies behavior (calls) to a method What Are Mocks? 14
  15. 15. Blah Blah Blah professor Dan 15
  16. 16. Eliminates dependencies Isolated Unit Tests Problems Mocks Solve 16 foo(x) bar(x)
  17. 17. Tests methods that have no return value Problems Mocks Solve 17 How do we know that bar(x) has been called?
  18. 18. Tests error handling Problems Mocks Solve 18
  19. 19. Eliminate dependency on database calls Speed up testing! Reduce test complexity Dont have to write complex logic to handle behavior of methods not under test Dont have to wait to implement other methods Other Reasons Mocks Are Important 19
  20. 20. Ok, Im sold Show me how to actually do this 20
  21. 21. Mock (MagicMock) Most robust, popular Built-in as of Python 3.3! flexmock Based on Rubys flexmock mox Similar to Javas EasyMock Mocker dingus record then assert mocking library fudge Similar to Mockito MiniMock Simple mocking with DocTest What Are The Python Options? 21
  22. 22. Sample Problem 22
  23. 23. Problem: Tinder Competitor True love is an isolated container Image Credits: - http://www.forbes.com/sites/jjcolao/2013/04/08/tinder-for-business-dating-app-looks-to-conquer-other-matchmaking-verticals/ - https://blog.docker.com
  24. 24. Create a method to return a new, random victim candidate Must not show the same person Must not show someone the user has already swiped on Problem: Docker dating app
  25. 25. Easy enough 25 Surely no one could have seen EVERYONE in the database!!! - The Intern get_next_person() get_random_person()
  26. 26. Write a Unit Test 26
  27. 27. It works!!! 27
  28. 28. Noooooooooo!!!!
  29. 29. 29
  30. 30. Easy enough 30 What if knew the result of get_random_person()???
  31. 31. 31
  32. 32. Patching Module.attribute Mock method
  33. 33. It works EVERY SINGLE TIME!!! 33
  34. 34. Variations on a theme 34
  35. 35. Variations on a theme 35
  36. 36. Variations on a theme 36
  37. 37. Variations on a theme 37 OMG, ContextManagers! WHAT???
  38. 38. But what if we call it multiple times??? 38
  39. 39. What if I want to test the while loop? Different results on multiple calls
  40. 40. Uhumm 40 UMM
  41. 41. Use side_effect 41
  42. 42. Use patching / mocks to bring certainty to method calls Eliminates dependencies on other code Even unfinished code!!! Lots of ways to do it, pick your favorite Recap: What did we learn? 42
  43. 43. Mocking to Verify Behavior 43
  44. 44. Problem: Matching in Docker
  45. 45. When a user swipes right If the other user likes them: Send them both a message with contact info If the other user dislikes them: Let the user down gently If the other user hasnt evaluated yet: Display the give it time message Problem: Docker matches
  46. 46. Implementation 46 How do we test this??? No return values!!!
  47. 47. Behavior Verification 47
  48. 48. What about checking parameters??? 48
  49. 49. Verifying Parameters 49
  50. 50. Shouldnt we check the other methods too? 50 Wed need to have multiple mocks to do that!!!
  51. 51. Multiple Mocks 51
  52. 52. Patch Multiple 52
  53. 53. Testing Multiple Calls 53
  54. 54. Multiple calls 54
  55. 55. Whew!!! Almost Done! The day is almost over! You can sleep later! Image Credits: - http://www.clickhole.com/article/8-kittens-god-forbid-could-be-used-prop-wobbly-tab-1364 - http://stuffpoint.com/dogs/image/208632/adorable-corgi-puppies-running-ddddd-picture/
  56. 56. Mocking Exceptions 56
  57. 57. Sample Problem: Docker Payments with Stripe
  58. 58. Stripe Payment Code
  59. 59. Stripe Testingugh! 59
  60. 60. Requires us to call the API Unclear what those numbers mean Must create the stripe_token Not a dictionary of CC fields Encrypted Thats ridiculous 60 Theres got to be a better way
  61. 61. have you tried Mocking??? 61
  62. 62. Mocking an Exception 62
  63. 63. Wrap Up: Key Take-Aways 63
  64. 64. Mocking makes writing unit tests simpler Eliminates dependencies Verifies behavior Tests error handling You just need some practice! Remember This!
  65. 65. http://mock.readthedocs.org/en/latest/ Pip Install Mock (Python 2) Create a simple class, then write tests! Try It On Your Own 65
  66. 66. Lets Go Write Some Tests!!! 66
  67. 67. Questions? 67 GitHub: https://github.com/Ooblioob Twitter: @Ooblioob