Skip to content

Commit b79520e

Browse files
committed
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.
1 parent f58d7b7 commit b79520e

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

xmlrunner/result.py

Lines changed: 12 additions & 1 deletion
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,11 @@ 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+
if test_method.__doc__:
266+
self.doc = test_method.__doc__
267+
else:
268+
self.doc = None
261269
except (AttributeError, IOError, TypeError):
262270
# issue #188, #189, #195
263271
# some frameworks can make test method opaque.
@@ -555,6 +563,9 @@ def _report_testcase(test_result, xml_testsuite, xml_document):
555563
if test_result.lineno is not None:
556564
testcase.setAttribute('line', str(test_result.lineno))
557565

566+
if test_result.doc is not None:
567+
testcase.appendChild(xml_document.createComment(str(test_result.doc)))
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)