5

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?

Richard B
  • 895
  • 13
  • 39

2 Answers2

1

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).

Richard B
  • 895
  • 13
  • 39
0

I would add the root folder and test/resource_dir to the sys.path. This will allow you to always include resources in the exact same way no matter what the directory structure of your test folder currently looks like.

Adding the root folder to sys.path also helps create consistency between the test runner and normal execution, and will allow your tests to directly reference resources from root/package/resource_directory.

Adding test/resource_dir to sys.path allows tests to directly reference resources from test/resource_dir.

For some sample code on how to set this up, see Option B on this answer, but with replacing the lib folder for resource_dir.

hostingutilities.com
  • 8,894
  • 3
  • 41
  • 51