It happens every time. You fire up your editor. The Twilio app spits off your fingers, deftly wielding the TwiML primitives you know to build your voice or SMS functionality. You save. You commit. You deploy. You set the request URLs for your Twilio number. You gingerly unlock your phone and confidently tap in the digits. Then you hear it:

“We’re sorry. An application error has occurred. Goodbye.”

Rage wells inside you familiar to the impulse hearing the dog laugh at you in Duck Hunt. You open the Twilio App Monitor and find the culprit – a fat fingered typo on your Dial verb.

Cue the sad trombone.

It happens to all of us. And when you’re iterating on your Twilio code, the wash-rinse-repeat cycle of changing code, deploying to a web server and manually testing on your phone can become tiresome quickly.

One technique I use to reduce this frustration is writing unit tests against my Twilio webhook endpoints as I work on my app. Utilizing a web framework’s test client to write these tests quickly helps keep my focus on the app I’m trying to write and the knucklehead errors that produce the Duck Hunt experience to a minimum.

What You’ll Learn

In this post, we’ll show that technique in action with Flask, my go-to framework for writing Twilio apps in Python.

How to write a simple Twilio Conference endpoint with Flask

Write a unit test with Nose for that endpoint

Expand that unit test into a test case we can reuse for all our Twilio apps

Then show how that reusable test case can be applied to more complex flows.

What You’ll Need

Let’s Cut Some Code

To start, we’ll open a text editor in our Python environment with the Twilio and Flask modules installed and clack out a simple app that will create a Twilio Conference room using the verb and the noun.

Here’s a quick cut in a file we’ll name app.py: