Skip to main content

Fixtures for pytest allowing test functions/methods to easily retrieve test resources from the local filesystem.

Project description

The datadir-ng plugin for pytest provides the datadir and datadir_copy fixtures which allow test functions to easily access resources in data directories. It was inspired by the pytest-datadir plugin and is intended to be a more flexible version of that plugin (hence the “ng” part in its name – as in “next generation”).

This plugin provides two fixtures:

  • The datadir fixture allows test functions and methods to access resources in so-called “data directories”.

  • The datadir_copy fixture is similar to the datadir fixture, but it copies the requested resources to a temporary directory first so that test functions or methods can modify their resources on-disk without affecting other test functions and methods.

Installation

Just do:

pip install pytest-datadir-ng

The datadir fixture

The “datadir” fixture allows test functions and methods to access resources in so-called “data directories”.

The fixture behaves like a dictionary. Currently, only retrieving items using the d[key] syntax is supported. Things like iterators, len(d) etc. are not.

How the fixture looks for resources is best described by an example. Let us assume the following directory structure for your tests:

tests/
+-- test_one.py
+-- test_two.py
+-- data/
|   +-- global.dat
+-- test_one/
|   +-- test_func/
|       +-- data.txt
+-- test_two/
    +-- TestClass/
        +-- test_method/
            +-- strings.prop

The file test_one.py contains the following function:

def test_func(datadir):
    data_path = datadir["data.txt"]

    # ...

The file test_two.py contains the following class:

class TestClass(object):
    def test_method(self, datadir):
        strings_path = datadir["strings.prop"]

        # ...

When the test_func() function asks for the data.txt resource, the following directories are searched for a file or directory named data.txt, in this order:

  • tests/test_one/test_func/

  • tests/test_one/

  • tests/data/test_one/test_func/

  • tests/data/test_one/

  • tests/data/

The path to the first existing file (or directory) is returned as a py.path.local object. In this case, the returned path would be tests/test_one/test_func/data.txt.

When the test_method() method asks for the strings.prop resource, the following directories are searched for a file or directory with the name strings.prop, in this order:

  • tests/test_two/TestClass/test_method/

  • tests/test_two/TestClass/

  • tests/test_two/

  • tests/data/test_two/TestClass/test_method/

  • tests/data/test_two/TestClass/

  • tests/data/test_two/

  • tests/data/

Here, this would return the path tests/test_two/TestClass/test_method/strings.prop.

As you can see, the searched directory hierarchy is slighly different if a method instead of a function asks for a resource. This allows you to load different resources based on the name of the test class, if you wish.

Finally, if a test function or test method would ask for a resource named global.dat, then the resulting path would be tests/data/global.dat since no other directory in the searched directory hierarchy contains a file named global.dat. In other words, the tests/data/ directory is the place for global (or fallback) resources.

If a resource cannot be found in any of the searched directories, a KeyError is raised.

The datadir_copy fixture

The “datadir_copy” fixture is similar to the datadir fixture, but copies the requested resources to a temporary directory first so that test functions or methods can modify their resources on-disk without affecting other test functions and methods.

Each test function or method gets its own temporary directory and thus its own fresh copies of the resources it requests.

Caveat: Each time a resource is requested using the dictionary notation, a fresh copy of the resource is made. This also applies if a test function or method requests the same resource multiple times. Thus, if you modify a resource and need to access the modified version of the resource later, save its path in a variable and use that variable to access the resource later instead of using the dictionary notation multiple times:

def test_foo(datadir_copy):
    # This creates the initial fresh copy of data.txt and saves
    # its path in the variable "resource1".
    resource1 = datadir_copy["data.txt"]

    # ...modify resource1 on-disk...

    # You now want to access the modified version of data.txt.

    # WRONG way: This will overwrite your modified version of the
    #            resource with a fresh copy!
    fh = datadir_copy["data.txt"].open("rb")

    # CORRECT way: This will let you access the modified version
    #              of the resource.
    fh = resource1.open("rb")

Version history

Version 1.1.1

  • Add a LICENSE file (fixes #3).

Version 1.1.0

  • Allow per-test directories under data/ (thanks, Alexander Lukanin).

Version 1.0.1

Version 1.0.0

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-datadir-ng-1.1.1.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

pytest_datadir_ng-1.1.1-py2.py3-none-any.whl (7.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pytest-datadir-ng-1.1.1.tar.gz.

File metadata

  • Download URL: pytest-datadir-ng-1.1.1.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.8.0

File hashes

Hashes for pytest-datadir-ng-1.1.1.tar.gz
Algorithm Hash digest
SHA256 0d9e0212eaa4d0440a4b7c3d2df4b4b7eeebde1854ab383c5aff590764ad8a52
MD5 fe2af071333902c9c81b072334611049
BLAKE2b-256 1ad6812562c99bfb8870e897f6d9a0af4d116118144b205257f9778c51481f99

See more details on using hashes here.

File details

Details for the file pytest_datadir_ng-1.1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: pytest_datadir_ng-1.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.8.0

File hashes

Hashes for pytest_datadir_ng-1.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7fec7a4996a12529a935512c128624fa7289495b520fd31b4645c3a71daa394e
MD5 b9c84b75da30aaba288cb1a93fe77d96
BLAKE2b-256 c598d4b1344e2f307d7707e5f113409442c2879657fb76880621697074176dce

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page