Skip to content

Commit cc64f71

Browse files
committed
quick fixes for inspect.
problem: `inspect.getsourcefile()` gets confused when the test method has decorators, e.g. when using `@mock.patch(...)`, it will consider the filename to be `/.../.../mock.py` rather than the test file.
1 parent 112c159 commit cc64f71

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

xmlrunner/result.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,9 @@ def startTest(self, test):
269269
else:
270270
# regular unittest.TestCase?
271271
test_method = getattr(test, test._testMethodName)
272-
self.filename = inspect.getsourcefile(test_method)
272+
test_class = type(test)
273+
# Note: inspect can get confused with decorators, so use class.
274+
self.filename = inspect.getsourcefile(test_class)
273275
_, self.lineno = inspect.getsourcelines(test_method)
274276
finally:
275277
pass
@@ -556,7 +558,10 @@ def _report_testcase(test_result, xml_testsuite, xml_document):
556558
testcase.setAttribute('timestamp', test_result.timestamp)
557559

558560
if test_result.filename is not None:
559-
testcase.setAttribute('file', os.path.relpath(test_result.filename))
561+
# Try to make filename relative to current directory.
562+
filename = os.path.relpath(test_result.filename)
563+
filename = test_result.filename if filename.startswith('../') else filename
564+
testcase.setAttribute('file', filename)
560565

561566
if test_result.lineno is not None:
562567
testcase.setAttribute('line', str(test_result.lineno))

0 commit comments

Comments
 (0)