Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added individual test case docstring support and tests #51

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions ddt.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
else:
_have_yaml = True

__version__ = '1.1.1'
__version__ = '1.1.2'

# These attributes will not conflict with any real python attribute
# They are added to the decorated test method and processed later
Expand Down Expand Up @@ -129,7 +129,7 @@ def mk_test_name(name, value, index=0):
return re.sub(r'\W|^(?=\d)', '_', test_name)


def feed_data(func, new_name, *args, **kwargs):
def feed_data(func, new_name, test_docstring, *args, **kwargs):
"""
This internal method decorator feeds the test data item to the test.

Expand All @@ -139,27 +139,33 @@ def wrapper(self):
return func(self, *args, **kwargs)
wrapper.__name__ = new_name
wrapper.__wrapped__ = func
# Try to call format on the docstring
if func.__doc__:
try:
wrapper.__doc__ = func.__doc__.format(*args, **kwargs)
except (IndexError, KeyError):
# Maybe the user has added some of the formating strings
# unintentionally in the docstring. Do not raise an exception as it
# could be that he is not aware of the formating feature.
pass
# set docstring if exists
if test_docstring is not None:
wrapper.__doc__ = test_docstring
else:
# Try to call format on the docstring
if func.__doc__:
try:
wrapper.__doc__ = func.__doc__.format(*args, **kwargs)
except (IndexError, KeyError):
# Maybe the user has added some of the formating strings
# unintentionally in the docstring. Do not raise an exception
# as it could be that user is not aware of the
# formating feature.
pass
return wrapper


def add_test(cls, test_name, func, *args, **kwargs):
def add_test(cls, test_name, test_docstring, func, *args, **kwargs):
"""
Add a test case to this class.

The test will be based on an existing function but will give it a new
name.

"""
setattr(cls, test_name, feed_data(func, test_name, *args, **kwargs))
setattr(cls, test_name, feed_data(func, test_name, test_docstring,
*args, **kwargs))


def process_file_data(cls, name, func, file_attr):
Expand All @@ -178,17 +184,21 @@ def func(*args):
# If file does not exist, provide an error function instead
if not os.path.exists(data_file_path):
test_name = mk_test_name(name, "error")
add_test(cls, test_name, create_error_func("%s does not exist"), None)
test_docstring = """Error!"""
add_test(cls, test_name, test_docstring,
create_error_func("%s does not exist"), None)
return

_is_yaml_file = data_file_path.endswith((".yml", ".yaml"))

# Don't have YAML but want to use YAML file.
if _is_yaml_file and not _have_yaml:
test_name = mk_test_name(name, "error")
test_docstring = """Error!"""
add_test(
cls,
test_name,
test_docstring,
create_error_func("%s is a YAML file, please install PyYAML"),
None
)
Expand Down Expand Up @@ -217,9 +227,9 @@ def _add_tests_from_data(cls, name, func, data):
value = elem
test_name = mk_test_name(name, value, i)
if isinstance(value, dict):
add_test(cls, test_name, func, **value)
add_test(cls, test_name, test_name, func, **value)
else:
add_test(cls, test_name, func, value)
add_test(cls, test_name, test_name, func, value)


def ddt(cls):
Expand Down Expand Up @@ -250,14 +260,15 @@ def ddt(cls):
if hasattr(func, DATA_ATTR):
for i, v in enumerate(getattr(func, DATA_ATTR)):
test_name = mk_test_name(name, getattr(v, "__name__", v), i)
test_docstring = getattr(v, "__doc__", None)
if hasattr(func, UNPACK_ATTR):
if isinstance(v, tuple) or isinstance(v, list):
add_test(cls, test_name, func, *v)
add_test(cls, test_name, test_docstring, func, *v)
else:
# unpack dictionary
add_test(cls, test_name, func, **v)
add_test(cls, test_name, test_docstring, func, **v)
else:
add_test(cls, test_name, func, v)
add_test(cls, test_name, test_docstring, func, v)
delattr(cls, name)
elif hasattr(func, FILE_ATTR):
file_attr = getattr(func, FILE_ATTR)
Expand Down
15 changes: 15 additions & 0 deletions test/test_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ def annotated(a, b):
return r


def annotated2(listIn, name, docstring):
r = Mylist(listIn)
setattr(r, "__name__", name)
setattr(r, "__doc__", docstring)
return r


@ddt
class FooTestCase(unittest.TestCase):
def test_undecorated(self):
Expand All @@ -44,6 +51,14 @@ def test_greater(self, value):
a, b = value
self.assertGreater(a, b)

@data(annotated2([2, 1], 'Test_case_1', """Test docstring 1"""),
annotated2([10, 5], 'Test_case_2', """Test docstring 2"""))
def test_greater_with_name_docstring(self, value):
a, b = value
self.assertGreater(a, b)
self.assertIsNotNone(getattr(value, "__name__"))
self.assertIsNotNone(getattr(value, "__doc__"))

@file_data("test_data_dict_dict.json")
def test_file_data_json_dict_dict(self, start, end, value):
self.assertLess(start, end)
Expand Down
35 changes: 35 additions & 0 deletions test/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,41 @@ class Mytest(object):
assert_is_not_none(getattr(ddt_mytest, 'test_hello_2_2'))


def test_ddt_data_doc_attribute():
"""
Test the ``__doc__`` attribute handling of ``data`` items with ``ddt``
"""

def hello():
"""testFunctionDocstring {6}

:param: None
:return: None
"""
pass

class Myint(int):
pass

class Mytest(object):
pass

d1 = Myint(1)
d1.__name__ = 'case1'
d1.__doc__ = """docstring1"""

d2 = Myint(2)

data_hello = data(d1, d2)(hello)
setattr(Mytest, 'test_hello', data_hello)

ddt_mytest = ddt(Mytest)
assert_is_not_none(getattr(getattr(ddt_mytest, 'test_hello_1_case1'),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not assert_equals or something more precise? You know the value you should expect...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

'__doc__'))
assert_is_not_none(getattr(getattr(ddt_mytest, 'test_hello_2_2'),
'__doc__'))


def test_ddt_data_unicode():
"""
Test that unicode strings are converted to function names correctly
Expand Down
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ deps =
coverage
flake8
six>=1.4.0
PyYAML
mock
commands =
nosetests -s --with-coverage --cover-package=ddt --cover-html
flake8 ddt.py test
Expand Down