Package: pytest-datadir-ng
Thedatadir-ngplugin forpytestprovides thedatadiranddatadir_copyfixtures which allow test functions to easily access resources
in data directories. It was inspired by thepytest-datadir pluginand 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:Thedatadirfixture allows test functions and methods to access resources in
so-called “data directories”.Thedatadir_copyfixture is similar to thedatadirfixture, 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.InstallationJust do:pip install pytest-datadir-ngThe datadir fixtureThe “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 thed[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.propThe filetest_one.pycontains the following function:deftest_func(datadir):data_path=datadir["data.txt"]# ...The filetest_two.pycontains the following class:classTestClass(object):deftest_method(self,datadir):strings_path=datadir["strings.prop"]# ...When thetest_func()function asks for thedata.txtresource, the
following directories are searched for a file or directory nameddata.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 apy.path.localobject. In this case, the returned path would betests/test_one/test_func/data.txt.When thetest_method()method asks for thestrings.propresource,
the following directories are searched for a file or directory with the namestrings.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 pathtests/test_two/TestClass/test_method/strings.prop.As you can see, the searched directory hierarchy is slighly different if amethodinstead of afunctionasks 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 namedglobal.dat, then the resulting path would betests/data/global.datsince no other directory in the searched directory hierarchy contains
a file namedglobal.dat. In other words, thetests/data/directory
is the place for global (or fallback) resources.If a resource cannot be found inanyof the searched directories, aKeyErroris raised.The datadir_copy fixtureThe “datadir_copy” fixture is similar to thedatadirfixture, 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 themodifiedversion 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:deftest_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 historyVersion 1.1.1Add aLICENSEfile (fixes #3).Version 1.1.0Allow per-test directories underdata/(thanks, Alexander Lukanin).Version 1.0.1Added thisVersion historysection. :-)Fixed bad usage ofpy.path.localobjects in code examples.Version 1.0.0Initial release