Loading files for tests #8154
Unanswered
mattgwwalker
asked this question in
General
Replies: 3 comments 5 replies
-
I tend to use
from . import resources
def test_audio():
with importlib.resources.path(resources, "audio.ogg") as path:
assert sut(path) = "expected" |
Beta Was this translation helpful? Give feedback.
2 replies
-
I recommend pytest-datadir, I use it all the time. 👍 |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thanks @nicoddemus, I have just seen your suggestion. I was going to report that I had success with adding the following into a
I was then able to specify a filename in a test using:
This approach seems to work well for me. Do you see any negatives? Thanks again, Matthew |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
How do I load a data file for a test? Specifically, how do I load the file given that I do not know the current working directory?
I would like to test that my code correctly opens an audio file. Imagine the following layout:
"pytest" can be run from within
rootdir
or, often, from with thetests
directory itself.Currently we're resolving this issue with the following line inserted at the top of every test file:
Then we know the current working directory, and we can load files relative to it:
Is there a better way to do this? Forgetting the
chdir
line leads to hard-to-track down errors as it seems the lines are run when the tests are found rather than just before they are executed. If we were to add a sub-directory of tests then I would suspect our current technique would fail.I was expecting to find a general solution to be something like:
But I can't find any relevant documentation or discussions. Does anyone have any suggestions for how to resolve this or recommendations for articles I should read?
Thanks,
Matthew
Beta Was this translation helpful? Give feedback.
All reactions