Martin Pool's blog

Python rocks for testing

I'm testing a custom XML-object mapping library. Writing test cases in Python is incredibly pleasant:

class BaseParseTest(comfychair.TestCase):
    def runtest(self):
        tt = pywbem.tupletree.xml_to_tupletree(self.input_xml)
        result = pywbem.tupleparse.parse_any(tt)
        self.assert_equal(result, self.expected_obj)
class KeyBinding_Numeric_Test(BaseParseTest):
    input_xml = """<KEYBINDING NAME="answer">
        <KEYVALUE VALUETYPE="numeric">42</KEYVALUE>
    </KEYBINDING>"""
    expected_obj = {"answer": 42}

and that's it! Rinse, repeat. Add more as problems are discovered.

I'm using my comfychair test library here, which is yet another rewrite of the JUnit idea, specialized for the kind of tests I often write.

In particular:

It's only a matter of degree, but being 50% easier to create test cases has a compounding effect on the amount of test coverage.

Python has much weaker compile-time tests than say Java or C. The ease of writing tests tends to make up for this though, and to get you to a higher state than Java that merely compiles but is not yet tested.

Archives 2008: Apr Feb 2007: Jul May Feb Jan 2006: Dec Nov Oct Sep Aug Jul Jun Jan 2005: Sep Aug Jul Jun May Apr Mar Feb Jan 2004: Dec Nov Oct Sep Aug Jul Jun May Apr Mar Feb Jan 2003: Dec Nov Oct Sep Aug Jul Jun May