Skip to content

xmlrunner: Expose python docstrings as comments in the XML result file #214

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

Merged
merged 2 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions tests/django_example/app/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,13 @@
# Create your tests here.
class DummyTestCase(TestCase):
def test_pass(self):
"""Test Pass"""
pass

def test_negative_comment1(self):
"""Use a close comment XML tag -->"""
pass

def test_negative_comment2(self):
"""Check XML tag </testsuites>"""
pass
4 changes: 4 additions & 0 deletions tests/django_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ def _check_runner(self, runner):
test_ids = [test.id() for test in suite]
self.assertEqual(test_ids, [
'app2.tests.DummyTestCase.test_pass',
'app.tests.DummyTestCase.test_negative_comment1',
'app.tests.DummyTestCase.test_negative_comment2',
'app.tests.DummyTestCase.test_pass',
])
suite = runner.build_suite(test_labels=[])
test_ids = [test.id() for test in suite]
self.assertEqual(set(test_ids), set([
'app.tests.DummyTestCase.test_pass',
'app.tests.DummyTestCase.test_negative_comment1',
'app.tests.DummyTestCase.test_negative_comment2',
'app2.tests.DummyTestCase.test_pass',
]))

Expand Down
13 changes: 12 additions & 1 deletion xmlrunner/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class _TestInfo(object):
SKIP: 'skipped',
}

def __init__(self, test_result, test_method, outcome=SUCCESS, err=None, subTest=None, filename=None, lineno=None):
def __init__(self, test_result, test_method, outcome=SUCCESS, err=None, subTest=None, filename=None, lineno=None, doc=None):
self.test_result = test_result
self.outcome = outcome
self.elapsed_time = 0
Expand Down Expand Up @@ -159,6 +159,7 @@ def __init__(self, test_result, test_method, outcome=SUCCESS, err=None, subTest=

self.filename = filename
self.lineno = lineno
self.doc = doc

def id(self):
return self.test_id
Expand Down Expand Up @@ -200,6 +201,7 @@ def __init__(self, stream=sys.stderr, descriptions=1, verbosity=1,
self.properties = properties # junit testsuite properties
self.filename = None
self.lineno = None
self.doc = None
if infoclass is None:
self.infoclass = _TestInfo
else:
Expand All @@ -213,6 +215,7 @@ def _prepare_callback(self, test_info, target_list, verbose_str,
"""
test_info.filename = self.filename
test_info.lineno = self.lineno
test_info.doc = self.doc
target_list.append(test_info)

def callback():
Expand Down Expand Up @@ -258,6 +261,8 @@ def startTest(self, test):
# Handle partial and partialmethod objects.
test_method = getattr(test_method, 'func', test_method)
_, self.lineno = inspect.getsourcelines(test_method)

self.doc = test_method.__doc__
except (AttributeError, IOError, TypeError):
# issue #188, #189, #195
# some frameworks can make test method opaque.
Expand Down Expand Up @@ -555,6 +560,12 @@ def _report_testcase(test_result, xml_testsuite, xml_document):
if test_result.lineno is not None:
testcase.setAttribute('line', str(test_result.lineno))

if test_result.doc is not None:
comment = str(test_result.doc)
# The use of '--' is forbidden in XML comments
comment = comment.replace('--', '&#45;&#45;')
testcase.appendChild(xml_document.createComment(comment))

result_elem_name = test_result.OUTCOME_ELEMENTS[test_result.outcome]

if result_elem_name is not None:
Expand Down