Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
wswld committed Sep 27, 2021
2 parents 96007f3 + 877755b commit b0683d2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
20 changes: 11 additions & 9 deletions ddt.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
FILE_ATTR = '%file_path' # store the path to JSON file
YAML_LOADER_ATTR = '%yaml_loader' # store custom yaml loader for serialization
UNPACK_ATTR = '%unpack' # remember that we have to unpack values
index_len = 5 # default max length of case index
INDEX_LEN = '%index_len' # store the index length of the data


try:
Expand Down Expand Up @@ -90,12 +90,10 @@ def data(*values):
Should be added to methods of instances of ``unittest.TestCase``.
"""
global index_len
index_len = len(str(len(values)))
return idata(values)
return idata(values, len(str(len(values))))


def idata(iterable):
def idata(iterable, index_len):
"""
Method decorator to add to your test methods.
Expand All @@ -104,6 +102,7 @@ def idata(iterable):
"""
def wrapper(func):
setattr(func, DATA_ATTR, iterable)
setattr(func, INDEX_LEN, index_len)
return func
return wrapper

Expand Down Expand Up @@ -138,7 +137,7 @@ def wrapper(func):
return wrapper


def mk_test_name(name, value, index=0, name_fmt=TestNameFormat.DEFAULT):
def mk_test_name(name, value, index=0, index_len=5, name_fmt=TestNameFormat.DEFAULT):
"""
Generate a new name for a test case.
Expand Down Expand Up @@ -264,13 +263,14 @@ def _add_tests_from_data(cls, name, func, data):
"""
Add tests from data loaded from the data file into the class
"""
index_len = len(str(len(data)))
for i, elem in enumerate(data):
if isinstance(data, dict):
key, value = elem, data[elem]
test_name = mk_test_name(name, key, i)
test_name = mk_test_name(name, key, i, index_len)
elif isinstance(data, list):
value = elem
test_name = mk_test_name(name, value, i)
test_name = mk_test_name(name, value, i, index_len)
if isinstance(value, dict):
add_test(cls, test_name, test_name, func, **value)
else:
Expand Down Expand Up @@ -332,11 +332,13 @@ def ddt(arg=None, **kwargs):
def wrapper(cls):
for name, func in list(cls.__dict__.items()):
if hasattr(func, DATA_ATTR):
index_len = getattr(func, INDEX_LEN)
for i, v in enumerate(getattr(func, DATA_ATTR)):
test_name = mk_test_name(
name,
getattr(v, "__name__", v),
i,
index_len,
fmt_test_name
)
test_data_docstring = _get_test_data_docstring(func, v)
Expand Down Expand Up @@ -369,4 +371,4 @@ def wrapper(cls):

# ``arg`` is the unittest's test class when decorating with ``@ddt`` while
# it is ``None`` when decorating a test class with ``@ddt(k=v)``.
return wrapper(arg) if inspect.isclass(arg) else wrapper
return wrapper(arg) if inspect.isclass(arg) else wrapper
25 changes: 13 additions & 12 deletions test/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,12 @@ def hello():
dh_keys = set(data_hello.__dict__.keys())
post_size = len(data_hello.__dict__)

assert post_size == pre_size + 1
extra_attrs = dh_keys - keys
assert len(extra_attrs) == 1
extra_attr = extra_attrs.pop()
assert getattr(data_hello, extra_attr) == (1, 2)
assert post_size == pre_size + 2
extra_attrs = list(dh_keys - keys)
extra_attrs.sort()
assert len(extra_attrs) == 2
assert getattr(data_hello, extra_attrs[0]) == 1
assert getattr(data_hello, extra_attrs[1]) == (1,2)


def test_file_data_decorator_with_dict():
Expand All @@ -135,13 +136,13 @@ def hello():

dh_keys = set(data_hello.__dict__.keys())
post_size = len(data_hello.__dict__)

assert post_size == pre_size + 1
extra_attrs = dh_keys - keys

assert len(extra_attrs) == 1
extra_attr = extra_attrs.pop()
assert getattr(data_hello, extra_attr) == ("test_data_dict.json",)
assert post_size == pre_size + 2

extra_attrs = list(dh_keys - keys)
extra_attrs.sort()
assert len(extra_attrs) == 2
assert getattr(data_hello, extra_attrs[0]) == 1
assert getattr(data_hello, extra_attrs[1]) == ("test_data_dict.json",)


def _is_test(x):
Expand Down

0 comments on commit b0683d2

Please sign in to comment.