Intro to Mocking - DjangoCon 2015

Preview:

Citation preview

Intro To Mocking:

Why Unit Testing Doesn’t Have to Be So Hard!

Daniel Davis

@Ooblioob

• 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

Survey Time!

4

• Struggled with unit testing until I learned

about Mocking

Learning To Test

5

Me Learning about Mocking…

6

Image credit: http://stylecaster.com/excited-gif/

Many of us are in the same boat…

7

• “I wish someone would just give a talk on

this…”

• So if that’s you…

We all want to write better tests…

Let’s Talk About Unit Tests…

Unit – 70%

Integration

– 20%

UI

10%

Testing Types

• “They’re 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”

• “There’s just some stuff you can’t unit test”

Great Unit Testing Myths

Mocking makes unit testing easier!

12

What Are Mocks?

13

Test Double

Dummy

ObjectTest Stub Test Spy

Mock

Object

Fake

Object

Commonly referred to as “Mocks”

• 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

Blah Blah Blah professor Dan…

15

• Eliminates dependencies

– Isolated Unit Tests

Problems Mocks Solve

16

foo(x) bar(x)

• Tests methods that have no return value

Problems Mocks Solve

17

How do we know that bar(x) has been called?

• Tests error handling

Problems Mocks Solve

18

• Eliminate dependency on database calls

– Speed up testing!

• Reduce test complexity

– Don’t have to write complex logic to handle

behavior of methods not under test

• Don’t have to wait to implement other

methods

Other Reasons Mocks Are Important

19

Ok, I’m sold…Show me how to actually do this…

20

• Mock (MagicMock)– Most robust, popular

– Built-in as of Python 3.3!

• flexmock– Based on Ruby’s flexmock

• mox– Similar to Java’s EasyMock

• Mocker

• dingus– “record then assert” mocking library

• fudge– Similar to Mockito

• MiniMock– Simple mocking with DocTest

What Are The Python Options?

21

Sample Problem

22

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

• 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

Easy enough…

25

“Surely no one could

have seen EVERYONE

in the database!!!”

- The Internget_next_person() get_random_person()

Write a Unit Test…

26

It works!!!

27

Noooooooooo!!!!

29

Easy enough…

30

What if knew the result of get_random_person()???

31

PatchingModule.attribute

Mock method

It works EVERY SINGLE TIME!!!

33

Variations on a theme

34

Variations on a theme

35

Variations on a theme

36

Variations on a theme

37

OMG, ContextManagers! WHAT???

But what if we call it multiple times???

38

• What if I want to test the while loop?

Different results on multiple calls

Uh…umm…

40

UMM…

Use side_effect

41

• 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

Mocking to Verify Behavior

43

Problem: Matching in “Docker”

• 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 hasn’t evaluated yet:

– Display the “give it time” message

Problem: “Docker” matches

Implementation

46

How do we test this??? No return values!!!

Behavior Verification

47

What about checking parameters???

48

Verifying Parameters

49

Shouldn’t we check the other

methods too?

50

We’d need to have multiple mocks to do that!!!

Multiple Mocks

51

Patch Multiple

52

Testing Multiple Calls…

53

Multiple calls

54

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/

Mocking Exceptions

56

Sample Problem: Docker Payments with Stripe

Stripe Payment Code

Stripe Testing…ugh!

59

• Requires us to call the API

• Unclear what those numbers mean

• Must create the stripe_token

– Not a dictionary of CC fields

– Encrypted

That’s ridiculous…

60

There’s got to be a better way…

…have you tried Mocking???

61

Mocking an Exception

62

Wrap Up:Key Take-Aways

63

• Mocking makes writing unit tests simpler

– Eliminates dependencies

– Verifies behavior

– Tests error handling

• You just need some practice!

Remember This!

• http://mock.readthedocs.org/en/latest/

• Pip Install Mock (Python 2)

• Create a simple class, then write tests!

Try It On Your Own

65

Let’s Go Write Some Tests!!!

66

Questions?

67

GitHub: https://github.com/Ooblioob

Twitter: @Ooblioob