Skip to content

Commit

Permalink
Avoid a traceback on tornado.testing test classes
Browse files Browse the repository at this point in the history
https://gist.github.com/s0undt3ch/9298a69a3492404d89a832de9efb1e68

This only happens when XML reporting is enabled.
Why only now, I have no clue.
  • Loading branch information
s0undt3ch committed Mar 18, 2019
1 parent 55e150c commit dce4ffa
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions tests/support/xmlunit.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from __future__ import absolute_import
import io
import sys
import time
import inspect
import logging

# Import 3rd-party libs
Expand All @@ -27,6 +29,7 @@
try:
import xmlrunner.runner
import xmlrunner.result
import xmlrunner.unittest
HAS_XMLRUNNER = True

class _DelegateIO(object):
Expand Down Expand Up @@ -56,9 +59,48 @@ def __getattr__(self, attr):

class _XMLTestResult(xmlrunner.result._XMLTestResult):
def startTest(self, test):
log.debug('>>>>> START >>>>> {0}'.format(test.id()))
log.debug('>>>>> START >>>>> %s', test.id())
# xmlrunner classes are NOT new-style classes
xmlrunner.result._XMLTestResult.startTest(self, test)
# xmlrunner.result._XMLTestResult.startTest(self, test)

# ----- Re-Implement startTest -------------------------------------------------------------------------->
# The reason being that _XMLTestResult does not like tornado testing wrapping it's test class
# https://gist.github.com/s0undt3ch/9298a69a3492404d89a832de9efb1e68
self.start_time = time.time()
xmlrunner.unittest.TestResult.startTest(self, test)

try:
if getattr(test, '_dt_test', None) is not None:
# doctest.DocTestCase
self.filename = test._dt_test.filename
self.lineno = test._dt_test.lineno
else:
# regular unittest.TestCase?
test_method = getattr(test, test._testMethodName)
test_class = type(test)
# Note: inspect can get confused with decorators, so use class.
self.filename = inspect.getsourcefile(test_class)
# Handle partial and partialmethod objects.
test_method = getattr(test_method, 'func', test_method)

# ----- Code which avoids the inspect tracebacks ------------------------------------------------>
try:
from tornado.testing import _TestMethodWrapper
if isinstance(test_method, _TestMethodWrapper):
test_method = test_method.orig_method
except (ImportError, AttributeError):
pass
# <---- Code which avoids the inspect tracebacks -------------------------------------------------
_, self.lineno = inspect.getsourcelines(test_method)
finally:
pass

if self.showAll:
self.stream.write(' ' + self.getDescription(test))
self.stream.write(" ... ")
self.stream.flush()
# <---- Re-Implement startTest ---------------------------------------------------------------------------

if self.buffer:
# Let's override the values of self._stdXXX_buffer
# We want a similar sys.stdXXX file like behaviour
Expand All @@ -68,7 +110,7 @@ def startTest(self, test):
sys.stdout = self._stdout_buffer

def stopTest(self, test):
log.debug('<<<<< END <<<<<<< {0}'.format(test.id()))
log.debug('<<<<< END <<<<<<< %s', test.id())
# xmlrunner classes are NOT new-style classes
return xmlrunner.result._XMLTestResult.stopTest(self, test)

Expand Down

0 comments on commit dce4ffa

Please sign in to comment.