Could someone tell me what the correct way of dealing with resource files in pytest?
I have a habit of creating a resources directory just under the package (src) directory and another under the test branch
root---package---resource_dir
| |-file1.py
| |-file2.py
|
|-test-- resource_dir-
| |-config1.csv
| |-config2.csv
|-test1.py
|-test2.py
If I want to access these files from within my tests what is the pythonic way of doing so? Up to now I have been using relative file paths but that solution isn't great. Is there a solution similar to the Java resources solution?
One way I have found to do this is using pkg_resources. This has the ability to search through your package for files. So, for example, if you want to print the contents of a file that is in the test resources directory you could do the following:
import pkg_resources
config_file = open(pkg_resources.resource_filename('test.resources','config.csv'),'r')
for line in config_file:
print(line)
print(">>>End of File")
please note that all directories and subdirectories need to be pacakges (i.e the init.py file must be present, even if its empty).