How many times have you gone through a test script from an outside project, or an older project and read through the source and found that you couldn’t discern the meaning of the test? For me it’s been plenty of times and especially so with test automation using things like selenium. This is part of the reason that Test::A8N exists, to make test automation understandable, even over time and with changing implementations.
One of the simplest things to do to help improve the readability (and therefore maintainability) of your test code is this little tip that I picked up while working with my friend Luke Closs (thanks Luke!).
When writing your test code, simply wrap your related test code in a named block which describes what this part of your test script is testing. Here’s an example from the Net::API::RPX distribution I have on CPAN:
As you can see it’s a pretty simple thing to do and might not indicate much value. However, when your test script gets into the hundreds to over a thousand lines of code, it becomes quite easy to lose the meaning of the test in all that source code, especially when you start re-using variables and the like. As well as being more readable, the named block gives you a lexical scope so that you can be more strict about which variables are re-used between test cases.
I certainly found that writing my tests in this fashion have helped improve my tests and ability to maintain my tests many many times over. Hopefully you’ll find it useful too.
[...] This post was mentioned on Twitter by Kip Hampton. Kip Hampton said: RT @konobi: New blog post: Making perl tests more readable http://bit.ly/12yJOZ [...]