Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using resource files in python (pytest) tests

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?

like image 378
Richard B Avatar asked Jun 27 '26 01:06

Richard B


2 Answers

If all your files belong to the same package, then you can use importlib instead of pkg_resources.

According to the official documentation this provides less overhead and better performance. https://docs.python.org/3/library/importlib.resources.html#importlib.resources.files

If you have trouble importing from a sibling package, please read the excellent answer here: Sibling package imports

Example:

# import test.resource_dir as a namespace package. 
# add a __init__.py to your directory if you want to turn it into a regular package

import test.resource_dir  
from importlib.resources import files
import pytest

@pytest.fixture
def config_path():
    return files(test.resource_dir).joinpath("config1.csv")

def test_read_config(config_path):
    with open(config_path, 'r') as config:
        assert config.read()
like image 177
Ranudar Avatar answered Jun 28 '26 14:06

Ranudar


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

like image 32
Richard B Avatar answered Jun 28 '26 15:06

Richard B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!