Skip to content
Swizec Teller - a geek with a hatswizec.com

Unit testing is for lazy people

The other day I was talking to a guy about a possible freelancing gig and he said how wonderful it was that I should bring up the topic of unit/automated testingwithout being asked. Said that most (many?) developers don't have the level of rigor to use automated testing.

Oops building

My reaction was one of disbelief "Rigor!? But automated testing is one of the laziest things a developer can do! It speeds stuff up _so much!"_

As luck would have it, last night I was hit over the head with my own words and nearly died debugging a single function.

I was working on Stripe webhooks and for security reasons decided not to use the event data sent in request body. Makes sense right? Take the event id from request body, then fetch the actual event from Stripe.

It's the only way to be certain you aren't responding to bogus events sent by an evil person trying to make you look bad (nothing actually bad can happen, at worst a customer would get extra paid invoice emails).

Due to poor decoupling - I didn't really want to decouple a 6 line function into two functions - everything was now difficult to test. I can't create events on Stripe's servers with unit tests and without actual events existing I can't test the function works as it's supposed to.

How many bugs can you put in a 6 line function anyway?

A lot of bugs!

When the client tested on staging ... it didn't work. Invoice email wasn't sent and Stripe complained of a 500 error.

English: British Army Signals Trials Unit test...

It took me almost two hours to fix all the bugs because my testing cycle looked like this:

  1. change code
  2. commit to develop branch
  3. switch to staging branch
  4. merge develop branch into staging
  5. push to github
  6. change to other terminal window
  7. pull from staging branch
  8. restart python processes
  9. go to Stripe dashboard
  10. pick customer
  11. create invoice item
  12. create actual invoice
  13. choose invoice
  14. pay invoice
  15. go to Stripe logs
  16. find invoice.payment_succeeded webhook
  17. scroll down to response
  18. look through raw html of django's error page
  19. find symptom
  20. GOTO 1.

That's right, a whopping 20 step debug cycle all because I'm an idiot and couldn't find a way to automate this. Or maybe I was too tired to do the unobvious thing ... although I still don't want to split a 6 liner into two functions.

With proper unit testing the debug cycle would look like this:

  1. change code
  2. run tests
  3. symptom thrown in face
  4. GOTO 1.

Much lazier right?

For the record, those six lines of code contained 4 bugs ->

  • forgot to import a module
  • different event data structure than I understood from the docs
  • twice^
  • misnamed variables from one line to another

Yes, all of those could easily have been caught if my test coverage was actually any good! And then not only would I not look like an idiot in front of the client, I'd probably spend no more than ten minutes fixing this.

Let this be a lesson both to you and to Future Swizec!

Enhanced by Zemanta

Did you enjoy this article?

Published on February 27th, 2012 in Debugging, Integration testing, Programming, Test, Test automation, TestUnit, Uncategorized

Learned something new?
Want to become a high value JavaScript expert?

Here's how it works 👇

Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet 📖right away. After that you'll get thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over my 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

Start with an interactive cheatsheet 📖

Then get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career.

"Man, love your simple writing! Yours is the only email I open from marketers and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"

~ Ashish Kumar

Join over 10,000 engineers just like you already improving their careers with my letters, workshops, courses, and talks. ✌️

Have a burning question that you think I can answer? I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

Ready to Stop copy pasting D3 examples and create data visualizations of your own?  Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev

Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com

By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️