2

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?

fewistle
  • 23
  • 5
  • "This is not a duplicate question. In no other solutions they portray for txt file." No, generally you don't want just plain text, unless it's in a text-based serialization format (JSON, yaml). But the solutions do show you how to do that. Or you could just use `pickle`, which would be probably easier. Just look at the duplicate targets – juanpa.arrivillaga Jan 18 '23 at 20:47
  • Or better yet, why dont' you just seperate these out into *another module*? – juanpa.arrivillaga Jan 18 '23 at 20:48
  • Okay @juanpa.arrivillaga, close it, no worries. But, what does another module mean, sorry, I am completely new to everything. – fewistle Jan 18 '23 at 21:08
  • 2
    another `.py` file that you `import`. Something like `testing_constants.py` then you can just `import testing_constants as tc` or something, then `tc.EXPECTED_VALUE` or whatever. And I didn't close your question. – juanpa.arrivillaga Jan 18 '23 at 21:09
  • This is a legit good way. Thanks @juanpa.arrivillaga – fewistle Jan 18 '23 at 21:30

2 Answers2

2

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 use ast.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:

['1', 2, (3, 4, 5), {'6': 7}, {8, 9, 10}]
import ast

with open("expected_result.txt") as f:
    e = ast.literal_eval(f.read())

gives the list e = ['1', 2, (3, 4, 5), {'6': 7}, {8, 9, 10}]

Inspecting that object:

print(type(e))
for index, item in enumerate(e):
    print(index, type(item), item)

prints:

<class 'list'>
0 <class 'str'> 1
1 <class 'int'> 2
2 <class 'tuple'> (3, 4, 5)
3 <class 'dict'> {'6': 7}
4 <class 'set'> {8, 9, 10}

Further reading:

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • you really should just use pickle. But honestly, the OP should just use another module. – juanpa.arrivillaga Jan 18 '23 at 20:48
  • @juanpa.arrivillaga right, but IIRC pickle requires you to write the object through code, but this approach will allow you to write the expected result directly to a file. I agree that they should just use another module. Will edit that into my answer. – Pranav Hosangadi Jan 18 '23 at 20:50
  • 1
    Also, you might as well just use `eval` here, if you are going that route. – juanpa.arrivillaga Jan 18 '23 at 21:03
0

For a .txt file you can do something like:

path = 'C:\\my_file.txt'
open_file = open(path, 'r')
var = open_file.read() 
layman
  • 79
  • 10