Skip to content

Commit

Permalink
xmlrunner: Expose python docstrings as comments in the XML result file (
Browse files Browse the repository at this point in the history
#214)

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

These comments could for instance be retrieved by XSLT to add
description to test title.

* tests: Added Python Docstring to test new support
  • Loading branch information
oliviermartin authored Feb 20, 2020
1 parent 67bc432 commit 6556d19
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
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

0 comments on commit 6556d19

Please sign in to comment.