You are on page 1of 8

Abusing pyunit To Run

Regression Tests
Miki Tebeka
mtebeka@qualcomm.com

pyunit

unittest module in Python standard library

One of the Xnit frame!orks

"esigned for nit Testing

#$d like to use it for Regression Testing

%hecks for &good' kno!n output from a program



Main #dea

(a)e one method to run a regression

"ynamically add test function to test class

*+pected output is in gold directory

%an ha)e input in input directory

gold,input names are test names



One &real' test function
def runtest(self, name, arguments):
outfile = join("out", name)
goldfile = join("gold", name)
# Run program
if system("echo %s > %s 2>!" % "
(" "#join(arguments), outfile)) $= %:
self#fail("non&'ero (alue return")
# )hec* output
if differ(outfile, goldfile):
self#fail("output for %s differs" % name)

Adding a Test
def add+test(name, arguments):
,,,-dd a test "name" .ith "arguments"
/ote: "name" must 0e 1ualified 2ython
(aria0le name
,,,
def t(self):
self#runtest(name, arguments)
t#func+doc = "3esting %s" % name
setattr(3est4cho, "test+%s" % name, t)

Adding All Tests
for test in glo0(join("gold", "5")):
# 6nly files are tests
if not isfile(test):
continue
test = 0asename(test)
add+test(test, 7test8)

Running

unittest has a main function that check


current source file for all class deri)ed from
3est)ase and runs all methods starting !ith
test in it
# 9ain
if ++name++ == "++main++":
main() # :mported from unittest

-uestions.

You might also like