Friday, 24 May 2019

How can I get files within the tests in Python?

I have the following package structure:

.
├── my_app
│   ├── app.py
│   ├── cli.py
│   ├── db.py
│   └── __init__.py
├── setup.py
├── tests
│   ├── data
│   │   └── foobar.gz
│   ├── test_app.py
│   └── test_database.py
└── tox.ini

Within test_app.py, I do this:

import pkg_resources
path = '../tests/data/foobar.gz'
full_path = pkg_resources.resource_filename(__name__, path)

Now I've seen the message

/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py:1145:
  DeprecationWarning: Use of .. or absolute path in a resource path
  is not allowed and will raise exceptions in a future release.
self, resource_name

What should I do instead?

(I could also change the structure of the package, but I would like to keep the test data within the tests - it is not necessary for running the application)



from How can I get files within the tests in Python?

No comments:

Post a Comment