Friday, April 22, 2011

Test Code that Doesn't

Consider the following code taken from an automated testing suite:


def group(self, group, options="", authUser=None, cmdOptions=""):
if options != "":
options = "-" + options

output, rCode = self._run(cmdOptions, "list {} group {}".format(options, group), authUser, returnOutput=True)
if rCode != 0 and output != None:
lines = output.splitlines()
if len(lines) > 1:
for line in lines:
if line.find("[debug]") == -1:
group = self._parseGroup(line)
else:
group = self._parseGroup(output)

return group, rCode

What's especially interesting about this piece of code is that if the program being called succeeds and returns multiple lines, only the very last one is considered the actual output. If the program being called by _run fails, the return value is the text of what you asked for!

Though this is part of an automated testing suite, no real checks take place. The only thing it bothers to do is ignore debugging output. But that's handy!