When you're feeling all TAPped out

By Matt Layman on September 4, 2013

Good software practices say you should do automated testing, but what do you do when you work in an environment that can’t get good test tools? Use a simple testing protocol like TAP.

TAP is the Test Anything Protocol. The protocol is so simple that you could probably write code to make TAP output in minutes. It is a line based protocol which means that any language that has some sort of print method can generate TAP. That’s basically all software languages in common use.

I could provide the rules, but an example would be easier.

# This is a comment because it starts with a hash symbol.
ok 1 Something passed here.
not ok 2 Whoops, there was a failure.
When there is a line that TAP does not understand (like this one), it will skip it.
# TAP files should have a plan in the format of "1..<some number of tests>".

The key to the protocol is ok and not ok. When lines start with one of these two phrases, TAP readers will interpret it as a pass or failure. Besides the test number, everything else on the line is extra. Toss in how many tests were run and you have fully functional TAP output. We can prove it with Perl’s aptly name prove tool.

matt@eden:~$ prove sample.tap
sample.tap .. Failed 1/2 subtests

Test Summary Report
sample.tap (Wstat: 0 Tests: 2 Failed: 1)
  Failed test:  2
Files=1, Tests=2,  0 wallclock secs ( 0.01 usr +  0.00 sys =  0.01 CPU)
Result: FAIL

Once you have some TAP results from your awesome homegrown test suite, you can get them into a TAP reader like the Jenkins plugin or a specific tool like Tapper.

There are some additional features that I did not discuss here (like TODO tracking). See the TAP website for more details. More adventurous individuals can check out the specifics by looking at the de facto parser, TAP::Parser, on Perl’s CPAN.

If you want to chat about this with me, I'm @mblayman on Twitter.

Matt Layman

Matt is the lead software engineer at Storybird.

Always eager to talk about Python and other technology topics, Matt organizes Python Frederick in Frederick, Maryland (NW of Washington D.C.) and seeks to grow software skills for people in his community.