0

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.

6
  • "I've tried a couple of other alternatives, such as loading the file from this answer, but doing this gives me an error:" - please show the code for this alternative. I think the reason for your original error is that unit tests can be executed from different directories, and paths should therefore be absolute. The way you are parsing this XSD file is fine. Commented Mar 6, 2020 at 20:33
  • I thought I was giving the absolute path of the xsd from within the unit test though? The file I'm trying to parse is from the "dir" directory. Commented Mar 9, 2020 at 9:35
  • Please show the code that is using an absolute path to the XSD file. Thanks. (Only a path starting with / is an absolute path.) Commented Mar 9, 2020 at 10:43
  • Oh sorry. I thought you meant absolute path from the current directory. Would that not work? Commented Mar 9, 2020 at 10:49
  • I can only repeat myself. Please show the code that is using an absolute path to the XSD file. (You said you tried the approach in this answer that produces a different error.) Commented Mar 9, 2020 at 10:53

1 Answer 1

1

In the second version after your edit, you are calling getroot on a string instead of a parsed XML tree. Still, if you properly parse the file with an absolute path, this could solve your problem.

Schema example (test.xsd)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified">

    <xs:element name="config" type="xs:string" />

</xs:schema>

Python code (test.py)

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, 'data/test.xsd')

        tree = etree.parse(my_data_path)
        root = tree.getroot()

    def test_whatever(self):
        data = sum([1, 2])
        self.assertEqual(data, 3)

if __name__ == '__main__':
    unittest.main()

Output

Assuming a folder test that contains test.py, and that test.xsd is in a subfolder called data, and after fixing an error in your test (adding list brackets) the output is

$ python test.py
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. I'm still getting an error using your above code: OSError: Error reading file '../dir/testxsd.xsd': failed to load external entity "../dir/testxsd.xsd". I have my test.py file which I'm running from a 'Test' directory. Within that Test directory, I have a "dir" directory which contains testxsd.xsd
@Adam Details about your project structure are important. Also, in your case there is no need to include os.pardir it seems.
Thank you again. Could you let me know how you're running the test? I'm running it using python -m unittest test.py but still getting an error for some reason. It's obviously something I'm doing wrong so I'll try to figure that out.
At the end of my answer, you'll see I am using python test.py. But python -m unittest test.py should do the same. Perhaps you are leaving out an important detail, like more complex structure, or more code.
Oh right sorry, didn't see that. Yes perhaps you are right. I'll give it more thought but your answer should be fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.