serhii.net

In the middle of the desert you can say anything you want

14 Jan 2022

Using pytest-datafiles for assets in pytest

pytest-datafiles · PyPI allows copying files to a temporary directory, then they can be modified etc. Really neat!

Sample:

ASSETS_DIR = Path(__file__).parent / "assets"
PROJ_DIR = ASSETS_DIR / "project_dir"

konfdir =  pytest.mark.datafiles(PROJ_DIR)

@konfdir
def test_basedir_validity(datafiles):
	assert directory_is_valid(datafiles)

Also love this bit:

Note about maintenance: This project is maintained and bug reports or pull requests will be addressed. There is little activity because it simply works and no changes are required.

SADLY this means that returned path is py.path, I’m not the only one complaining about that1

Pytest has newer native fixtures that use Pathlib (Temporary directories and files — pytest documentation) but datafiles hasn’t been moved to them.

Nel mezzo del deserto posso dire tutto quello che voglio.