intro to mocking - djangocon 2015

67
Intro To Mocking: Why Unit Testing Doesn’t Have to Be So Hard! Daniel Davis @Ooblioob

Upload: excella

Post on 22-Jan-2018

522 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Intro to Mocking - DjangoCon 2015

Intro To Mocking:

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

Daniel Davis

@Ooblioob

Page 2: Intro to Mocking - DjangoCon 2015

• 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…

Page 3: Intro to Mocking - DjangoCon 2015

3

Page 4: Intro to Mocking - DjangoCon 2015

Survey Time!

4

Page 5: Intro to Mocking - DjangoCon 2015

• Struggled with unit testing until I learned

about Mocking

Learning To Test

5

Page 6: Intro to Mocking - DjangoCon 2015

Me Learning about Mocking…

6

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

Page 7: Intro to Mocking - DjangoCon 2015

Many of us are in the same boat…

7

Page 8: Intro to Mocking - DjangoCon 2015

• “I wish someone would just give a talk on

this…”

• So if that’s you…

We all want to write better tests…

Page 9: Intro to Mocking - DjangoCon 2015

Let’s Talk About Unit Tests…

Page 10: Intro to Mocking - DjangoCon 2015

Unit – 70%

Integration

– 20%

UI

10%

Testing Types

Page 11: Intro to Mocking - DjangoCon 2015

• “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

Page 12: Intro to Mocking - DjangoCon 2015

Mocking makes unit testing easier!

12

Page 13: Intro to Mocking - DjangoCon 2015

What Are Mocks?

13

Test Double

Dummy

ObjectTest Stub Test Spy

Mock

Object

Fake

Object

Commonly referred to as “Mocks”

Page 14: Intro to Mocking - DjangoCon 2015

• 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

Page 15: Intro to Mocking - DjangoCon 2015

Blah Blah Blah professor Dan…

15

Page 16: Intro to Mocking - DjangoCon 2015

• Eliminates dependencies

– Isolated Unit Tests

Problems Mocks Solve

16

foo(x) bar(x)

Page 17: Intro to Mocking - DjangoCon 2015

• Tests methods that have no return value

Problems Mocks Solve

17

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

Page 18: Intro to Mocking - DjangoCon 2015

• Tests error handling

Problems Mocks Solve

18

Page 19: Intro to Mocking - DjangoCon 2015

• 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

Page 20: Intro to Mocking - DjangoCon 2015

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

20

Page 21: Intro to Mocking - DjangoCon 2015

• 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

Page 22: Intro to Mocking - DjangoCon 2015

Sample Problem

22

Page 23: Intro to Mocking - DjangoCon 2015

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

Page 24: Intro to Mocking - DjangoCon 2015

• 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

Page 25: Intro to Mocking - DjangoCon 2015

Easy enough…

25

“Surely no one could

have seen EVERYONE

in the database!!!”

- The Internget_next_person() get_random_person()

Page 26: Intro to Mocking - DjangoCon 2015

Write a Unit Test…

26

Page 27: Intro to Mocking - DjangoCon 2015

It works!!!

27

Page 28: Intro to Mocking - DjangoCon 2015

Noooooooooo!!!!

Page 29: Intro to Mocking - DjangoCon 2015

29

Page 30: Intro to Mocking - DjangoCon 2015

Easy enough…

30

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

Page 31: Intro to Mocking - DjangoCon 2015

31

Page 32: Intro to Mocking - DjangoCon 2015

PatchingModule.attribute

Mock method

Page 33: Intro to Mocking - DjangoCon 2015

It works EVERY SINGLE TIME!!!

33

Page 34: Intro to Mocking - DjangoCon 2015

Variations on a theme

34

Page 35: Intro to Mocking - DjangoCon 2015

Variations on a theme

35

Page 36: Intro to Mocking - DjangoCon 2015

Variations on a theme

36

Page 37: Intro to Mocking - DjangoCon 2015

Variations on a theme

37

OMG, ContextManagers! WHAT???

Page 38: Intro to Mocking - DjangoCon 2015

But what if we call it multiple times???

38

Page 39: Intro to Mocking - DjangoCon 2015

• What if I want to test the while loop?

Different results on multiple calls

Page 40: Intro to Mocking - DjangoCon 2015

Uh…umm…

40

UMM…

Page 41: Intro to Mocking - DjangoCon 2015

Use side_effect

41

Page 42: Intro to Mocking - DjangoCon 2015

• 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

Page 43: Intro to Mocking - DjangoCon 2015

Mocking to Verify Behavior

43

Page 44: Intro to Mocking - DjangoCon 2015

Problem: Matching in “Docker”

Page 45: Intro to Mocking - DjangoCon 2015

• 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

Page 46: Intro to Mocking - DjangoCon 2015

Implementation

46

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

Page 47: Intro to Mocking - DjangoCon 2015

Behavior Verification

47

Page 48: Intro to Mocking - DjangoCon 2015

What about checking parameters???

48

Page 49: Intro to Mocking - DjangoCon 2015

Verifying Parameters

49

Page 50: Intro to Mocking - DjangoCon 2015

Shouldn’t we check the other

methods too?

50

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

Page 51: Intro to Mocking - DjangoCon 2015

Multiple Mocks

51

Page 52: Intro to Mocking - DjangoCon 2015

Patch Multiple

52

Page 53: Intro to Mocking - DjangoCon 2015

Testing Multiple Calls…

53

Page 54: Intro to Mocking - DjangoCon 2015

Multiple calls

54

Page 55: Intro to Mocking - DjangoCon 2015

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/

Page 56: Intro to Mocking - DjangoCon 2015

Mocking Exceptions

56

Page 57: Intro to Mocking - DjangoCon 2015

Sample Problem: Docker Payments with Stripe

Page 58: Intro to Mocking - DjangoCon 2015

Stripe Payment Code

Page 59: Intro to Mocking - DjangoCon 2015

Stripe Testing…ugh!

59

Page 60: Intro to Mocking - DjangoCon 2015

• 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…

Page 61: Intro to Mocking - DjangoCon 2015

…have you tried Mocking???

61

Page 62: Intro to Mocking - DjangoCon 2015

Mocking an Exception

62

Page 63: Intro to Mocking - DjangoCon 2015

Wrap Up:Key Take-Aways

63

Page 64: Intro to Mocking - DjangoCon 2015

• Mocking makes writing unit tests simpler

– Eliminates dependencies

– Verifies behavior

– Tests error handling

• You just need some practice!

Remember This!

Page 65: Intro to Mocking - DjangoCon 2015

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

• Pip Install Mock (Python 2)

• Create a simple class, then write tests!

Try It On Your Own

65

Page 66: Intro to Mocking - DjangoCon 2015

Let’s Go Write Some Tests!!!

66

Page 67: Intro to Mocking - DjangoCon 2015

Questions?

67

GitHub: https://github.com/Ooblioob

Twitter: @Ooblioob