Comparison of pytest plugins for accessing data files

It is often said there are more pytest plugins that aim to provide convenient access to packaged data files than grains of sand on Earth. Here I compare them all.

Did I miss one or do you have suggestions for other criteria? Feel free to open an issue!

Comparison

Name / URL Supports copying to temp dir Supports access without copying Paths provided as Fixture names Folder names
pytest-datadir pathlib.Path
  • datadir
  • shared_datadir
  • data
  • test_TEST_NAME
pytest-datadir-ng py.path
  • datadir
  • datadir_copy
  • data
  • data/test_TEST_NAME
  • test_TEST_NAME
pytest-datafixtures pathlib.Path
  • datafix
  • datafix_dir
  • datafix_read
  • datafix_readbin
  • datafixtures
  • **/datafixtures

Excluded

These plugins have names that might suggest they'd do something similar, but have slightly different aims than just providing convenient access to packaged data files:

  • pytest-datadir-mgr: More about downloading, caching etc. than just providing convenient access to packaged data files.
  • pytest-data-file: More about loading data from files than just providing raw access.
  • pytest-datafiles: Doesn't provide much in the way of simplifying access to packaged files. More focused on the copy-to-temp aspect.
  • pytest-data-from-files: Like pytest-data-file, more about implicitly loading test data from files than giving access to those files.
  • pytest-filedata: Also more about parametrizing tests with data from files.

Do I even need a plugin for this?

No.

If you're only interested in files and not directories, in Python 3.7+ you can use the functions provided by importlib.resources.

The "older" method is to use pkg_resources's ResourceManager API (part of setuptools) which is discouraged but has the advantage that it also allows you to access entire directories.

The only purpose of these plugins, at least as far as this comparison is concerned, is to get rid of the boilerplate for you.

Other things to note

No matter which method or plugin you choose, all of them will have to extract data files if your package is stored in an archived form (e.g. egg or wheel). To avoid this, specify zip_safe=False in your package configuration.