-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a "global_datadir" fixture too? #28
Comments
This is very nice. I've found myself wanting this in multiple projects |
You can simplify a bit by using the request context.
note that your package_path only looks up one directory above the current directory so if you either have a test that is already in the root dir or a test that is nested several levels down, this won't work. I'm looking through the request object for something that could get the data dir in the root tests directory, but it looks like we might need to walk the dirs. hmm... surely pytest can tell us where the root conftest.py is?
|
I hope there's a way to make this more generic so it can be included in this plugin, but here's what I just put together in conftest.py in the root tests directory of my project. import os
import pathlib
import pytest
import shutil
@pytest.fixture(scope="session")
def original_global_datadir():
return pathlib.Path(os.path.realpath(__file__)).parent / "data"
def prep_global_datadir(tmp_path_factory, original_global_datadir):
temp_dir = tmp_path_factory.mktemp("data") / "datadir"
shutil.copytree(original_global_datadir, temp_dir)
return temp_dir
@pytest.fixture(scope="session")
def session_global_datadir(tmp_path_factory, original_global_datadir):
return prep_global_datadir(tmp_path_factory, original_global_datadir)
@pytest.fixture(scope="module")
def module_global_datadir(tmp_path_factory, original_global_datadir):
return prep_global_datadir(tmp_path_factory, original_global_datadir)
@pytest.fixture(scope="function")
def global_datadir(tmp_path_factory, original_global_datadir):
return prep_global_datadir(tmp_path_factory, original_global_datadir) |
I usually put such "global" fixtures in |
Any plans on integrating this functionality? |
I just bump into this problem myself. One addition would be to take the approach suggested by @nicoddemus here, since a shared data folder for an entire project could become very large. Hence, it would be def test_foo(global_datadir):
file = global_datadir.get("file.txt") # -- only here the file is copied to the tmp folder.
# -- would tmp folder be the same returned by "datadir" fixture? About going forward with this: authors are very sensible on adding new features since this is a very mature and slim package (and I mostly agree with this approach). On the other hand, seems a very common request, and having a separate plugin to deal with such similar problem would be a bit annoying. @nicoddemus @gabrielcnr - could you comment on that? |
maybe something similar of what @nicoddemus pointed but could be parametrized, in the lines of def pytest_generate_tests(metafunc):
for mark in metafunc.definition.iter_markers("data_file"):
retrieve_data_file(metafunc, mark) and in the @pytest.mark.data_file("file1", "file.txt")
def test_with_mark(file1):
assert file1.exists() and it can return a |
@igortg - I think the idea of a global datadir might be indeed useful. Would you like to contribute with a PR ? That would be very welcome. Otherwise I'm happy to take a look at this at some point in the next few weeks after my holidays :-) |
When the testsuite has nested directories,
shared_datadir
tries to find adata
directory inside each level. This is useful but in some cases you might also want to have a truly "global" datadir.I think it would make sense if a
global_datadir
fixture was added that would always reference thedata
directory at the top level of the tests directory. If there is a single nest level, thenshared_datadir
andglobal_datadir
should point to the same dir.The implementation could be something like this (not thoroughly tested, e.g. I haven't tried it for namespace packages etc):
Should I make a PR?
The text was updated successfully, but these errors were encountered: