I'm trying to write some unit tests with certain methods that parse different elements of my XML. But I'm having a few issues parsing a "test" xml file in my unit test.
My question isn't so much about anything to do with the XML/XSD files, but it's just around how to parse them correctly in my unit test.
This is my code so far:
import unittest from lxml import etree from Directory.method_in_class import ClassName #changed the names for security class TestXmlData(unittest.TestCase): def setUp(self): self.method_in_class = ClassName() XSDDoc = etree.parse("dir/testxsd.xsd") rootXSD = XSDDoc.getroot() def test_whatever(self): # Test whatever if __name__ == '__main__': unittest.main() Even though I'm parsing the same way in the implementation method, I'm getting the below error:
OSError: Error reading file 'dir/testxsd.xsd': failed to load external entity "dir/testxsd.xsd"
I've tried a couple of other alternatives, such as loading the file from this answer, but doing this gives me an error:
import unittest from lxml import etree import os THIS_DIR = os.path.dirname(os.path.abspath(__file__)) class TestSpecData(unittest.TestCase): def setUp(self): my_data_path = os.path.join(THIS_DIR, os.pardir, 'dir/testxsd.xsd') rootXSD = my_data_path.getroot() def test_whatever(self): data = sum(1, 2) self.assertEqual(data, 3) if __name__ == '__main__': unittest.main() AttributeError: 'str' object has no attribute 'getroot'
I've also tried this answer, but I'm not familiar with Django so was getting a bunch of errors.
/is an absolute path.)