At Ticketmaster, we’re committed to testing. Automated tests improve our stability, helping us sell tickets to more fans and add new features. As a developer, I have an additional motivation to write tests well: tests illustrate the intended behavior of my code. Besides achieving good code coverage, an articulate test quickly teaches another person what my code does. Here are a few cues that I’ve settled on, inspired by comic strips.
Testing speeds things up.
DILBERT © 2015 Scott Adams. Used By permission of UNIVERSAL UCLICK. All rights reserved.
Keep it short
Three frames, four lines of dialogue: that’s limited space for pictures and words. Readers will skip over a fifty-line test block with glazed, lizard eyes. Do you want your readers to pay attention? Keep it tight, then.
DILBERT © 2015 Scott Adams. Used By permission of UNIVERSAL UCLICK. All rights reserved.
Stay focused
Test one feature at a time.
DILBERT © 2014 Scott Adams. Used By permission of UNIVERSAL UCLICK. All rights reserved.
Do you love Game of Thrones? Complex subplots, amirite? Intertwining narratives are fun, but testing many things at once is just confusing. Instead of writing one script that covers all possible cases, I separate each behavior into its own test. One strip, one joke; one test, one case. Isn’t that a Marley song?
Be self-contained
The Boss wants data-driven product releases.
DILBERT © 1994 Scott Adams. Used By permission of UNIVERSAL UCLICK. All rights reserved.
Do you recognize Dilbert and the Boss? Of course! Did you forget what happened before this strip from 1994? Maybe a fanboy remembers, but everything you need to know to get the joke is in these frames. I describe this as episodic style. The tests form a series of short narratives with familiar actors, but the events of one test do not impact the outcome of another. When tests rely on the same data, I generate fresh objects before each test. Tweaking the data for a particular test won’t pollute the tests that come after it, and my readers won’t need to track state as they scroll down the page.
Clear narrative
Have you noticed the similarity between Given-When-Then stanzas and the Arrange-Act-Assert pattern? These templates both resemble story structure because people remember narratives. While writing a test, I map the logic into exposition, rising action, and resolution. If any phase is longer than a line or two, I use comments or wrapper functions to delineate the boundaries. But I can’t hide too much detail—the punch line loses its sting if you don’t know the Boss practices feng shui martial arts.
Don’t leave your app defenseless.
DILBERT © 2015 Scott Adams. Used By permission of UNIVERSAL UCLICK. All rights reserved.
Tie it all together
There are many elements to a comic strip: the dialogue, the characters, the scenery. These layers all converge on a single message. Do you see Rodney’s wrinkled tie? This subtle detail emphasizes just how unsafe the Boss’ new product is. Without pictures, variable names must evoke imagery that reinforces the behavior being tested.
Poor Rodney—no wonder his tie is wrinkled.
DILBERT © 2010 Scott Adams. Used By permission of UNIVERSAL UCLICK. All rights reserved.
That’s all. I write short tests that focus on a single behavior. I name variables and functions carefully, and isolate state within each test. I ensure that the sequence of actions conveys a narrative structure. These elements of style focus the reader’s attention on my code’s behavior and foster clear understanding in a memorable way. I hope you find these cues helpful.
Love it? Hate it? Know any good jokes? Tell me what you think by dropping a note to matt@ticketmaster.com.