I am currently working on pytests, and for the expected results, which are in form of tuples, or lists are making the pytest classes look clunky.
This is not a duplicate question. In no other solutions they portray for txt file.
For example, the answer to this question for a json file is:
path = "C:\my_file.json"
my_var = json.load(open(path))
Likewise, how to do it for tuples, or lists?
Is storing in a .txt file a good option? If so how to assign the entire context correctly to a variable?
2
Answers
For a .txt file you can do something like:
As juanpa comments below, you should just define your expected results in another module and refer to them through the module to avoid cluttering up your actual test classes.
If this is not an acceptable solution, read on.
You can store your expected results to a
txt
file, and then useast.literal_eval
to parse it to a python object. Note that your text must be made up entirely of python literals for this to work (you can’t include a variable, e.g.)Suppose you have an
expected_result.txt
that looks like so:gives the list
e = ['1', 2, (3, 4, 5), {'6': 7}, {8, 9, 10}]
Inspecting that object:
prints:
Further reading: