Reputation: 381
I am trying to make my setUp-method create a mock configuration file, write a bunch of mock variables to it, and then use this file to instantiate the class (called Producer) i am running my tests on.
path_to_file =("/path/to/unit.Config")
unitTest = open (path_to_file, 'w')
unitTest.write("a string containing mock variables")
prod = Producer("unit.Config")
The tests work if i manually create a file and fill it with data prior to running the tests, but doing it in setUp causes my program to crash ("Producer instance has no attribute 'LOGGER'). If i remove the 3 first lines of code the tests will run fine - so writing to the config file works.
Upvotes: 0
Views: 1052
Reputation: 58788
To guarantee that the content that you write to a file is actually available to any process reading the file you need to close
the file handle after writing to it. The easiest way to remember to do this is to use a context manager:
with open(path_to_file, 'w') as file_pointer:
file_pointer.write("content")
# Outside the `with` the file content is available
Upvotes: 1
Reputation: 37023
Perhaps if you closed the file before trying to read the configuration from it you might get better results.
Upvotes: 0