用于DOM的Python正确单元测试

时间:2013-01-08 21:40:46

标签: python unit-testing dom

我正在编写基于各种输入构建DOM的东西,以及一些单元测试。我开始编写单元测试,看起来像这样:

class TestXMLRenderer(unittest.TestCase)
    def setUp(self):
        self.handler = handlers.XMLRenderer()
        self.top     = self.handler.create_xml()
    def testProcedure(self):
        '''Test that the Procedure node is created'''
        node = self.top.childNodes[1]
        self.assertTrue(isinstance(node, DOM.Element))
        self.assertEqual(node.nodeName, 'proc:Procedure')
        self.assertEqual(len(node.attributes), 3)
        self.assertTrue('xmlns:xsi' in node._attrs.keys())
        self.assertTrue('xmlns:proc' in node._attrs.keys())
        self.assertTrue('xsi:schemaLocation' in node._attrs.keys())
        self.assertEqual(node.getAttribute('xmlns:xsi'), 'http://www.w3.org/2001/XMLSchema-instance')
        self.assertEqual(node.getAttribute('xmlns:proc'), 'http://www.omg.org/space/procspec')
        self.assertEqual(node.getAttribute('xsi:schemaLocation'), 'http://www.omg.org/space/procspec ../../tools/xtp/Procedure.xsd')

但过了一会儿,这有点单调乏味。所以我想我可以定义另外一两个类来帮助测试:

import xml.dom.minidom as DOM

class XMLCompareFail(Exception):
    '''
    Exception raised when a node and the XMLNode do not match
    '''
    def __init__(self, message, code, Errors):
        self.message = message
        self.code    = code
        self.Errors  = Errors

    def __repr__(self): return "XMLCompareFail"

    def __str__(self):
        strs   = [self.message]
        names  = 'Names:  %s / %s' % (self.Errors[0].nodeName, self.Errors[1].name)
        if self.Errors[1].value:
            values = 'Values: %s / %s' % (self.Errors[0].nodeValue, self.Errors[1].value)
        else:
            values = 'Values: None'
        strs.append(names)
        strs.append(values)
        return repr(' -- '.join(strs))

class XMLNode(object):
    '''
    Object containing desired info for a particular node
    '''
    def __init__(self, name, value, ntype, numChildren):
        self.name  = name
        self.value = value
        self.ntype = ntype
        self.numChildren  = numChildren

class XMLTester(object):
    '''
    Comparison tests for DOM nodes
    '''
    def compareNode(self, node, info):
        if not isinstance(node, info.ntype):
            raise XMLCompareFail('Nodes are different instances', 0, [node, info])
        if not len(node.childNodes) == info.numChildren:
            raise XMLCompareFail('Nodes have different number of children', 1, [node, info])
        if not node.nodeName == info.name:
            raise XMLCompareFail('Nodes have differet names', 2, [node, info])
        if info.value:
            child = node.firstChild
            if not isinstance(child, DOM.Text):
                raise XMLCompareFail('Child node is not a text node', 3, [node, info])
            if not child.nodeValue == info.value:
                raise XMLCompareFail('Nodes have different text', 4, [node, info])

        return True

    def compareNodeList(self, node, nodeList):
        queue = set()
        queue.add(node)
        nodeList.reverse()
        while queue:
            curNode = queue.pop()
            curList = nodeList.pop()
            if not self.compareNode(curNode, curList):
                raise XMLCompareFail('Nodes do not match', [curNode, curList])
            if curNode.childNodes:
                for item in curNode.childNodes:
                    if isinstance(item, DOM.Text):
                        continue
                    else: queue.add(item)

        return True

将简化的未来测试看起来更像:

def testFixedValueFour(self):
    '''Test fixed value with no top and a string'''
    node = self.handler.create_fixedValue('another string')
    FixedValue         = XMLNode('proc:FixedValue', None, DOM.Element, 1)
    StringValue        = XMLNode('proc:StringValue', 'another string', DOM.Element, 1)
    nodeList = [FixedValue, StringValue]
    self.assertTrue(self.compareNodeList(node, nodeList))

所以我的问题是,就单元测试而言,这种方法还可以吗?我正在尝试自学单元测试,我有点迷失。

0 个答案:

没有答案