Skip to content

Commit 6556d19

Browse files
xmlrunner: Expose python docstrings as comments in the XML result file (#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
1 parent 67bc432 commit 6556d19

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

tests/django_example/app/tests.py

+9
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,13 @@
44
# Create your tests here.
55
class DummyTestCase(TestCase):
66
def test_pass(self):
7+
"""Test Pass"""
8+
pass
9+
10+
def test_negative_comment1(self):
11+
"""Use a close comment XML tag -->"""
12+
pass
13+
14+
def test_negative_comment2(self):
15+
"""Check XML tag </testsuites>"""
716
pass

tests/django_test.py

+4
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,16 @@ def _check_runner(self, runner):
5757
test_ids = [test.id() for test in suite]
5858
self.assertEqual(test_ids, [
5959
'app2.tests.DummyTestCase.test_pass',
60+
'app.tests.DummyTestCase.test_negative_comment1',
61+
'app.tests.DummyTestCase.test_negative_comment2',
6062
'app.tests.DummyTestCase.test_pass',
6163
])
6264
suite = runner.build_suite(test_labels=[])
6365
test_ids = [test.id() for test in suite]
6466
self.assertEqual(set(test_ids), set([
6567
'app.tests.DummyTestCase.test_pass',
68+
'app.tests.DummyTestCase.test_negative_comment1',
69+
'app.tests.DummyTestCase.test_negative_comment2',
6670
'app2.tests.DummyTestCase.test_pass',
6771
]))
6872

xmlrunner/result.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class _TestInfo(object):
128128
SKIP: 'skipped',
129129
}
130130

131-
def __init__(self, test_result, test_method, outcome=SUCCESS, err=None, subTest=None, filename=None, lineno=None):
131+
def __init__(self, test_result, test_method, outcome=SUCCESS, err=None, subTest=None, filename=None, lineno=None, doc=None):
132132
self.test_result = test_result
133133
self.outcome = outcome
134134
self.elapsed_time = 0
@@ -159,6 +159,7 @@ def __init__(self, test_result, test_method, outcome=SUCCESS, err=None, subTest=
159159

160160
self.filename = filename
161161
self.lineno = lineno
162+
self.doc = doc
162163

163164
def id(self):
164165
return self.test_id
@@ -200,6 +201,7 @@ def __init__(self, stream=sys.stderr, descriptions=1, verbosity=1,
200201
self.properties = properties # junit testsuite properties
201202
self.filename = None
202203
self.lineno = None
204+
self.doc = None
203205
if infoclass is None:
204206
self.infoclass = _TestInfo
205207
else:
@@ -213,6 +215,7 @@ def _prepare_callback(self, test_info, target_list, verbose_str,
213215
"""
214216
test_info.filename = self.filename
215217
test_info.lineno = self.lineno
218+
test_info.doc = self.doc
216219
target_list.append(test_info)
217220

218221
def callback():
@@ -258,6 +261,8 @@ def startTest(self, test):
258261
# Handle partial and partialmethod objects.
259262
test_method = getattr(test_method, 'func', test_method)
260263
_, self.lineno = inspect.getsourcelines(test_method)
264+
265+
self.doc = test_method.__doc__
261266
except (AttributeError, IOError, TypeError):
262267
# issue #188, #189, #195
263268
# some frameworks can make test method opaque.
@@ -555,6 +560,12 @@ def _report_testcase(test_result, xml_testsuite, xml_document):
555560
if test_result.lineno is not None:
556561
testcase.setAttribute('line', str(test_result.lineno))
557562

563+
if test_result.doc is not None:
564+
comment = str(test_result.doc)
565+
# The use of '--' is forbidden in XML comments
566+
comment = comment.replace('--', '&#45;&#45;')
567+
testcase.appendChild(xml_document.createComment(comment))
568+
558569
result_elem_name = test_result.OUTCOME_ELEMENTS[test_result.outcome]
559570

560571
if result_elem_name is not None:

0 commit comments

Comments
 (0)