Write Better Tests

With Mock!

If You're Not Writing Unit Tests Today

Shame On You!

If You're Not Writing Unit Tests Tomorrow

Shame On Me!

About Me

  • Python/DevOps developer by day

  • Django District organizer by night

New (To Me) In 2016:

"Continuous attention to technical excellence
and good design enhances agility."

Martin Fowler's Testing Pyramid

Reasons to Write Unit Tests

  • Martin Fowler said so

  • Prove to myself that the code works

  • Alert me if something breaks in the future

  • Comfortably make future changes

  • A gift to future developers

I'm not writing unit tests because _____

Common Excuses For Not Writing Tests

  • it's hard / intimidating

  • it takes too long

  • it doesn't benefit me

  • my code is always perfect

  • failing tests + CI = embarrassment

The Itinerary

1) Discuss common scenarios that make writing tests difficult

The Itinerary

2) Show how we can use "mocking" to simplify writing the tests

The Itinerary

3) We all walk out the door feeling confident about writing tests

What the heck is mocking?!?

Tease or laugh at in a scornful or contemptuous manner

Another definition of 'mock':

Not authentic or real, but without the intention to deceive

Mocking is the practice of replacing an object in your code with a fake object for the purpose of executing a test

In software development:

For a fun read on the nuances of:

  • Mock

  • Test Double

  • Stub

  • Fake

search google for "The Little Mocker"

Mocking in Python

  • 'mock' library is the most popular

    • 'pip install mock' in python 2

    • built into python 3

  • Well documented... but not straight forward

Let's get some sample code!

Mike, don't forget...

  • Github README

  • requirements.txt

  • Web app

Exercise 1:

Mocking Bird

Exercise 1: Mocking Bird

The tests actually pass... sometimes...

Let's run the tests 10 times:

for i in {1..10}; do
    pytest tests/test_mocking_bird.py
done | grep seconds

What Can Mock Do For You?

Create predictable scenarios for interfaces that have unpredictable results

Exercise 1: Mocking Bird

Mock tools required:

  • patch

  • return_value

Exercise 1: Mocking Bird

Exercise 2:

Mocking Me

Exercise 2: Mocking Me

The code for via_gif hits an external API. 

  • How do we test that the function handles response codes correctly?

  • Do the tests run quickly?

What Can Mock Do For You?

  • Run tests even if the API is not available

  • Test the rare edge cases

  • Tests run quickly even if the real interface is slow

Exercise 2: Mocking Me

Mocking is a mechanism to decouple outside dependencies, enabling the testing of an individual unit of code.

A Better Definition of 'mock'

Mock tools required:

  • patch

  • return_value

Exercise 2: Mocking Me

Exercise 3:

Mocking Jay

Exercise 3: Mocking Jay

How do we test if hug() gets called when we expect it to?

What Can Mock Do For You?

  • Test interactions with interfaces that have no return value

Exercise 3: Mocking Jay

Mock tools required:

  • patch

  • called

Exercise 3: Mocking Jay

You're Now A Mocking Expert!

My One Takeaway:

Don't be intimidated by mocking, it makes writing tests easier!

Practice creates confidence

Questions?

mocking-guide-in-python

By m3brown

mocking-guide-in-python

  • 1,382