omarkohl / pytest-datafiles Goto Github PK
View Code? Open in Web Editor NEWpytest plugin to create a tmpdir containing a preconfigured set of files and/or directories.
License: MIT License
pytest plugin to create a tmpdir containing a preconfigured set of files and/or directories.
License: MIT License
It seems that if relative paths are given in the decorator, they're assumed to be relative to the directory where you ran pytest
from. To me, it makes more sense to have the paths relative to the file that defines the tests that are running. Breaking backwards compatibility is problematic, but what would you think of having a flag which can be used to make the paths relative to the test itself?
I see a similar issue with an earlier version of pytest (4.3.0) which appears to have been fixed with pytest v4.5.0. But I'm using pytest v6.2.4 - is there something I'm missing?
The sdist (tarball) at PyPI doesnt contain the test files. i.e. https://files.pythonhosted.org/packages/26/60/4e49c18caf0ba7b7222da47ea6143f4f1ea3993f043be7bdf4ae409e544d/pytest-datafiles-2.0.1.tar.gz
Not including the tests makes it harder to package this for distros.
I notice that #10 is currently in flight, so it may be better to do that first, as poetry has its own way of recording which .py and data files need to be included in the sdist.
for reference, poetry needs something like the following in pyproject.toml:
packages = [
{ include = "pytest_datafiles.py"}, # not sure about this line
{ include = "tests", format = "sdist" }, # this is the voodoo, but maybe something extra is needed to ensure the _fixture_files is also included
]
It seems that the datafiles
fixture is a py.path
(well, a LocalPath
) - does this have any functionality that pathlib.Path
does not? If not, I think using pathlib
's Path
would be nice. The only function I've missed so far is is_file
, but I haven't tested this much. Additionally, the py
website says
Note: The ‘py’ library is in “maintenance mode” and so is not recommended for new projects. Please check out pathlib or pathlib2 for path operations.
I want to test two files like so:
@pytest.mark.datafiles(os.path.join(FIXTURE_DIR, '2019_DPD.txt'),
os.path.join(FIXTURE_DIR, 'expected_DPD.csv'))
def test_dpd_to_dataframe(datafiles):
path_to_data_file = str(datafiles.listdir()[0])
# Do stuff
expected_dpd_df = pd.read_csv(str(datafiles.listdir()[1]))
# Do other stuff
assert dpd_df.equals(expected_dpd_df)
and this will work only if file1 and file2 are stored in that order (The functions I am testing with are different and the files, hence, are different)
Is there any way to make sure that datafiles.listdir() returns the files in the exact same order and not switch it. (Like it is happening now)
Hi,
I've just seen this warning when upgrading to pytest-3.8.0:
pytest_datafiles.py:71: RemovedInPytest4Warning: MarkInfo objects are deprecated as they contain merged marks which are hard to deal with correctly.
Please use node.get_closest_marker(name) or node.iter_markers(name).
Docs: https://docs.pytest.org/en/latest/mark.html#updating-code
content = request.keywords.get('datafiles').args
pytest_datafiles.py:76: RemovedInPytest4Warning: MarkInfo objects are deprecated as they contain merged marks which are hard to deal with correctly.
Please use node.get_closest_marker(name) or node.iter_markers(name).
Docs: https://docs.pytest.org/en/latest/mark.html#updating-code
options.update(request.keywords.get('datafiles').kwargs)
I may try to fix this in the next few days (it should be easy enough), but just wanted to let you know now.
Thanks for the useful module!
Tom
I'm trying to write a fixture that returns safely a data file for testing.
Here's an example:
import os
import pytest
import json
from pathlib import Path
# from real_estate_tools.utils import get_property_detail, fetch_properties
FIXTURE_DIR = Path(__file__).parent.resolve() / "test_files"
# Works as expected
@pytest.mark.datafiles(FIXTURE_DIR / "complete_result.json")
def test_load_file(datafiles) -> dict:
with open(datafiles / os.listdir(datafiles)[0], "r") as f:
assert json.load(f)
# Turn to fixture
@pytest.mark.datafiles(FIXTURE_DIR / "complete_result.json")
@pytest.fixture
def load_file(datafiles) -> dict:
with open(datafiles / os.listdir(datafiles)[0], "r") as f:
return json.load(f)
# Doesn't work
# In the fixture, the part os.listdir(datafiles)
# has zero length, i.e. nothing is copied to the
# target temp dir.
def test_using_fixture(load_file):
print(load_file)
assert True
My motivation is that I want to execute multiple different tests using complete_result.json
as an input. Therefore, I'm trying to write the "loader" once as a fixture but it doesn't work.
What am I doing wrong?
I'd like to use datafiles in a parameterized fixture. In other words, I want my test function to be automatically invoked with different data files, as with a normal fixture with the params
argument. How do I achieve this? I tried the code below, but my datafiles are not copied to the tmpdir, and the test collection fails, because the test_files()
fixture does not yield any output. I'm quite new to pytest, so possibly I don't fully understand how it works.
@pytest.fixture(params = [1,2])
@pytest.mark.datafiles('file1.txt','file1.txt')
def test_files(request,datafiles):
for testfile in datafiles.listdir():
yield testfile
@pytest.fixture(params = ['expected_output1','expected_output2'])
def expected_output(request):
return request.param
def my_test_function(test_files,expected_output):
assert myFcn(test_files) == expected_output
Mode bits are not copied by default (https://py.readthedocs.io/en/latest/path.html#py._path.local.LocalPath.copy). This results in unexpected modes in testing. It would be useful if there were some way to use the datafiles fixture and copy modes.
With pytest 4.3.0 I am constantly seeing the following warning when executing tests using datafiles:
.tox/test-py37/lib/python3.7/site-packages/_pytest/mark/structures.py:324
xxxx/.tox/test-py37/lib/python3.7/site-packages/_pytest/mark/structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.datafiles - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
PytestUnknownMarkWarning,
The plugin works as expected though
I am running into an issue where I get a "ScopeMismatch" error if I try to use datafiles to create another fixture with a different scope than "function".
I want the scope to be "module" based because I would like to keep the database open for all tests since loading the database is time consuming.
Here is a simplified example of what I am trying to do:
@pytest.fixture(scope="module")
@pytest.mark.datafiles(TEST_DATA_DIR / "image.png")
def image_db(datafiles):
"""Loads image.png into database and returns database entry"""
with database():
database.add(datafiles / "image.png")
for db_entry in database.query_images():
yield db_entry
break
test setup failed
ScopeMismatch: You tried to access the 'function' scoped fixture 'datafiles' with a 'module' scoped request object, involved factories
Any help is a appreciated.
@pytest.fixture
@pytest.mark.datafiles('/tmp/big_files/film1.mp4')
def lots_of_files(datafiles):
pass
currently not providing the files I'm looking for
I'm using pytest-datafiles to test BuildStream, which is a build tool and as such does a lot of filesystem manipulation (naturally, we also have a lot of test cases which need to manipulate files mirrored outside of the git directory).
For us, the difference between a symlink to a file, and a copy of the said file, is an important difference; we need to test build scenarios where there are symlinks.
Currently our problem is that, if I commit symlinks to my test data dirs in git, and then use @pytest.mark.datafiles()
to test with a directory that contains symlinks, the symlinks just dont appear in the test environment. Instead copies of the symlink targets are created.
Currently I have some ugly workarounds in place which create the required symlinks in the tmpdir manually in advance of testing.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.