0

I am currently trying to unit test a module that parses and validates an XML file against a .XSD schema that I created. I want to be able to test a bunch of different XML files, but don't want to have to make a bunch of different XMLs. I was wondering if there is a way to simply use a string to create a File object that can be passed into the function I am testing.

An example might be:

File('<test><text>This is a test xml file used for testing</text></test>')

I just don't know the exact syntax to do it and can't find it. Thanks!

1
  • 1
    I think you want io.StringIO Commented May 20, 2021 at 21:05

1 Answer 1

1

You can use StringIO for this!

import io
f = io.StringIO("<test><text>Thats a test!</text></test>")

Is just the same as

f = open("test.xml", "r", encoding="utf-8")

But without having to have an actual file!

So it should fulfil your needs

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

2 Comments

This worked great! Thanks so much for the help!
Your welcome! Please press the check mark beside the answer to accept it.

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.