tldr; a combination of the @idata and @file_data decorators
Use Case
Add a decorator @ifile_data to pass multiple files as data items into a test function.
-
Files would be specified by a glob pattern relative to the test files directory.
-
The name of the test case would be the file.name of each file.
-
Test generated test names should include the file name.
MyTest_0_some_1234_json
MyTest_1_some_xyz_json
an example of how this operator would be used.
@ddt
class MyTestCase(unittest.TestCase):
@ifile_data('./data/some_*.json')
def MyTest(self, some_data):
self.assertIsNotNone(some_data)
workaround
I am using this as a workaround for now:
from unittest import TestCase, mock
import json
from ddt import ddt, idata
from pathlib import Path
class NamedList(list):
pass
def get_file_data(glob_pattern):
result = []
for file in Path(__file__).parent.glob(glob_pattern):
with open(file) as reader:
dataitem = NamedList(json.load(reader))
setattr(dataitem, '__name__', file.name)
result.append(dataitem)
return result
@ddt
class FileDataTests(TestCase):
@idata(get_file_data('./data/some_*.json'))
def test_file_data(self, some_data):
self.assertIsNotNone(some_data)
tldr; a combination of the @idata and @file_data decorators
Use Case
Add a decorator @ifile_data to pass multiple files as data items into a test function.
Files would be specified by a glob pattern relative to the test files directory.
The name of the test case would be the file.name of each file.
Test generated test names should include the file name.
an example of how this operator would be used.
workaround
I am using this as a workaround for now: