+ All Categories
Home > Technology > Mocking in Python

Mocking in Python

Date post: 15-Jul-2015
Category:
Upload: excella-consulting
View: 290 times
Download: 0 times
Share this document with a friend
Popular Tags:
70
Mocking Strategies Please don’t mock me after this presentation… Daniel Davis
Transcript
Page 1: Mocking in Python

Mocking Strategies

Please don’t mock me after this

presentation…

Daniel Davis

Page 2: Mocking in Python

• Daniel Davis

• Software Developer for 8 years

• Fun Fact:

– I ran the “Jingle All The Way” 5k dressed as a

Giant Gingerbread man

Who Am I?

Page 3: Mocking in Python

3

Page 4: Mocking in Python

Really though, who are you?

• Came from Java world

• Python developer for 2 years

• DevOps

– Lots of work with automation and quality

• Passionate about quality!

– Doesn’t happen overnight…

Page 5: Mocking in Python

Testing Journey

Page 6: Mocking in Python

• Became a Certified Scrum Developer

(CSD) about 2 years ago

• Finally learned about craftsmanship and

writing better tests

– Basically black magic

• Learned in Java…

I want to be a better developer

Page 7: Mocking in Python

• Switching to Python was jarring

• Kept wondering about writing unit tests

• Dealing with complicated frameworks like

Django

Finding Python

7

Page 8: Mocking in Python

• Asked colleagues

– Many had the same questions

• Decided to learn…

• Wanted to share with the community

Unit testing? Mocks?

8

Page 9: Mocking in Python

Let’s Talk About Unit Tests…

Page 10: Mocking in Python

Unit – 70%

Integration

– 20%

UI

10%

Testing Types

Page 11: Mocking in Python

• “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: Mocking in Python

Mocking makes unit testing easier!

12

Page 13: Mocking in Python

What Are Mocks?

13

Test Double

Dummy

ObjectTest Stub Test Spy

Mock

Object

Fake

Object

Commonly referred to as “Mocks”

Page 14: Mocking in Python

• 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: Mocking in Python

Blah Blah Blah professor Dan…

15

Page 16: Mocking in Python

• Eliminates dependencies in the CUT

(class under test)

– Isolated Unit Tests

Problems Mocks Solve

16

foo(x) bar(x)

Page 17: Mocking in Python

• Tests methods that have no return value

Problems Mocks Solve

17

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

Page 18: Mocking in Python

• Tests error handling

Problems Mocks Solve

18

How do we generate this exception???

Page 19: Mocking in Python

• 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: Mocking in Python

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

20

Page 21: Mocking in Python

• 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: Mocking in Python

Sample Problem

22

Valentine’s Day Edition

Page 23: Mocking in Python

Problem: Tinder Competitor

23

Get yo’ container on…

Page 24: Mocking in Python

• 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: Mocking in Python

Easy enough…

25

“Surely no one could

have seen EVERYONE

in the database!!!”

- The Internget_next_person() get_random_person()

Page 26: Mocking in Python

Write a Unit Test…

26

Page 27: Mocking in Python

It works!!!

27

Page 28: Mocking in Python
Page 29: Mocking in Python

29

Page 30: Mocking in Python

Easy enough…

30

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

Page 31: Mocking in Python

31

Page 32: Mocking in Python

PatchingModule.attribute

Mock method

Page 33: Mocking in Python

It works EVERY SINGLE TIME!!!

33

Page 34: Mocking in Python

Variations on a theme

34

Page 35: Mocking in Python

Variations on a theme

35

Page 36: Mocking in Python

Variations on a theme

36

Page 37: Mocking in Python

Variations on a theme

37

OMG, ContextManagers! WHAT???

Page 38: Mocking in Python

But what if we call it multiple times???

38

Page 39: Mocking in Python

• What if I want to test the while loop?

Different results on multiple calls

Page 40: Mocking in Python

Uh…umm…

40

UMM…

Page 41: Mocking in Python

Use side_effect

41

Page 42: Mocking in Python

• 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: Mocking in Python

Mocking to Verify Behavior

43

Page 44: Mocking in Python

Problem: Matching in “Docker”

Page 45: Mocking in Python

• 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: Mocking in Python

Implementation

46

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

Page 47: Mocking in Python

Behavior Verification

47

Page 48: Mocking in Python

What about checking parameters???

48

Page 49: Mocking in Python

Verifying Parameters

49

Page 50: Mocking in Python

Shouldn’t we check the other

methods too?

50

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

Page 51: Mocking in Python

Multiple Mocks

51

Page 52: Mocking in Python

Patch Multiple

52

Page 53: Mocking in Python

Testing Multiple Calls…

53

Page 54: Mocking in Python

Multiple calls

54

Page 55: Mocking in Python

Whew!!!Almost Done!There will be beer soon!

Sweet, delicious beer!

55

Page 56: Mocking in Python

Mocking built-ins and Exceptions

56

Page 57: Mocking in Python

Simple JSON reader

How do we test something like this???

Page 58: Mocking in Python

• Test parsing a valid file

• Test an IOError (i.e. file missing)

• Test a ValueError (i.e invalid json)

Testing JSON reader

58

Page 59: Mocking in Python

• Let’s just create a sample file!

How do I test open()

59

Page 60: Mocking in Python

Can you even mock a builtin?

60

Page 61: Mocking in Python

• open() returns a File object

• open(filename).read()

• So we really need to mock File.read()

– But it’s an instance!!! Oh no!

What? Why Not???

61

Have you tried solving it with Mocks???

Page 62: Mocking in Python

Mocks returning Mocks? WAT???

62

Page 63: Mocking in Python

What about error handling?

63

Page 64: Mocking in Python

What about error handling?

64

Page 65: Mocking in Python

What about ValueError?

Page 66: Mocking in Python

Wrap Up:Key Take-Aways

66

Page 67: Mocking in Python

• Mocking makes writing unit tests simpler

– Eliminates dependencies

– Verifies behavior

– Tests error handling

• You just need some practice!

Remember This!

Page 68: Mocking in Python

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

• Pip Install Mock

• Create a simple class, then write tests!

Try It On Your Own

68

Page 69: Mocking in Python

Let’s Go Write Some Tests!!!

69

Page 70: Mocking in Python

Questions?

70


Recommended