Package: pytest-dataset
Thedatasetplugin forpytestprovides thedataset,dataset_case_dataanddataset_copyfixtures which allow test functions to easily access resources
in data directories. It was inspired by thepytest-datadir-ng plugin.
Thedatasetreturn data from files injsonoryamlformats.
The main function is usage parameter--dataset-prefixin options ordataset_prefixin ini file.The files are searched in directories in order:{dataset_prefix}_{name}.yaml{dataset_prefix}_{name}.json{name}.yaml{name}.jsonThis plugin provides three fixtures:Thedatasetfixture allows test functions and methods to access resources in
so-called “data directories”.Thedataset_copyfixture is similar to thedatasetfixture, 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.Thedataset_case_datafixture return dataset named by the test function name.InstallationJust do:pip install pytest-datasetThe dataset fixtureThe “dataset” 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.
The pytest is called with option--dataset-prefixtestingdeftest_func(dataset):data_path=dataset["test_data"]# ...The filetest_two.pycontains the following class:classTestClass(object):deftest_method(self,dataset):strings_path=dataset["test_data_two"]# ...When thetest_func()function asks for thetest_dataresource, the
following directories are searched for a file in this order.Files:testing_test_data.yamltesting_test_data.jsontest_data.yamltest_data.jsonDirectories: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 is returned as data. In this case, the returned data would be from filetests/test_one/test_func/testing_test_data.yaml.When thetest_method()method asks for thetest_data_tworesource,
the following directories are searched for a file with the name in this order:Files:testing_test_data_two.yamltesting_test_data_two.jsontest_data_two.yamltest_data_two.jsonDirectories: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 data fromtests/test_two/TestClass/test_method/testing_test_data_two.yaml.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, then the resulting file would betests/data/{filename}since no other directory in the searched directory hierarchy contains
the file. 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 dataset_session fixtureSimilar to “dataset” only used for scope “session”.
The path, where are searched the dataset files are reduced to only {rootdir}/data/ .The dataset_package fixtureSimilar to “dataset” only used for scope “package”.
The path, where are searched the dataset files are reduced to only {rootdir}/data/ .The dataset_module fixtureSimilar to “dataset” only used for scope “module”.
The path, where are searched the dataset files are reduced to only possible for “module” scope.The dataset_class fixtureSimilar to “dataset” only used for scope “class”.
The path, where are searched the dataset files are reduced to only possible for “class” scope.The dataset_copy fixtureThe “dataset_copy” fixture is similar to thedatasetfixture, 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.The dataset_case_data fixtureThe “dataset_case_data” fixture allows test functions and methods to access resources used the function name as seareched dataset name.classTestClass(object):deftest_method(self,dataset_case_data):# ...When thetest_method()method is called than dataset_case_data directly contain the data searched in order:Files:testing_test_method.yamltesting_test_method.jsontest_method.yamltest_method.jsonDirectories: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 data fromtests/test_two/TestClass/test_method/testing_test_method.yaml.