from xml.etree.ElementTree import Element, SubElement, parse, tostring import unittest import os class MyXmlResultsFile: def __init__(self, fileName): """create a results template based on the current resource""" self.fileName = fileName self.topNode = 'test_delete' def getRoot(self): """return the top level root create file if it doesn't exist""" if not os.path.exists(self.fileName): self.create() return parse(self.fileName).getroot() def write(self, root): """write the file to disk""" path = os.path.split(self.fileName)[0] if not os.path.exists(path): os.makedirs(path) try: f = open(self.fileName, 'w') f.write(tostring(root)) finally: f.close() del f def create(self): """initialise a blank results file""" root = Element(self.topNode) SubElement(root, 'platform', {'host': 'mypc', 'build' : 'unittest', 'jre' : 'myjvm'}) self.write(root) def getConfiguration(self): """fetch the link configuration from the xml and return a dict""" configNode = self.getRoot().find('test_configuration') # uses underscore here return configNode.attrib def setConfiguration(self, configDict): """add the link configuration as a node to the xml""" root = self.getRoot() SubElement(root, 'test_configuration', configDict) self.write(root) class TestDelete(unittest.TestCase): def setUp(self): self.dirName = 'C:\\testdir' self.fileName = os.path.join(self.dirName, 'test.xml') if os.path.exists(self.fileName): os.remove(self.fileName) self.assertEquals(os.path.exists(self.fileName), False) def runTest(self): configDict = {'config':'dict'} resultsFile = MyXmlResultsFile(self.fileName) resultsFile.setConfiguration(configDict) config = resultsFile.getConfiguration() self.assertEquals(configDict, config) del resultsFile def tearDown(self): # here is the Fail: os.remove(self.fileName) if __name__ == '__main__': unittest.main()