31 from debug_tools
import getLogger
32 log = getLogger( 127, __name__ )
34 from .utilities
import wrap_text
35 from .utilities
import diffmatchpatch
36 from .utilities
import diff_match_patch
37 from .lockable_type
import LockableType
42 def __init__(self, stacktrace, message):
43 super(AssertionErrorData, self).__init__()
50 def __init__(self, *args, **kwargs):
54 self.
cached_super = super(MultipleAssertionFailures, self)
66 errors.append(
"%s\nAssertionError %s: %s" % ( error.stacktrace, index, error.message ) )
68 self.fail(
'\n\n' +
"\n".join( errors ) )
71 def assertEqual(self, goal, results, msg=None):
76 except unittest.TestCase.failureException
as error:
82 except Exception
as exception:
83 badtraces = traceback.format_list( traceback.extract_stack() )
86 def _goodStackTraces(self):
88 Get only the relevant part of stacktrace. 96 stacktrace = traceback.extract_stack()
99 for stack
in stacktrace:
100 filename = stack.filename
102 if found
and not stop
and not filename.find(
'lib' ) < filename.find(
'unittest' ):
105 if not found
and filename.find(
'lib' ) < filename.find(
'unittest' ):
109 stackline =
' File "%s", line %s, in %s\n %s' % ( stack.filename, stack.lineno, stack.name, stack.line )
110 goodtraces.append( stackline )
117 Holds common features across all Unit Tests. 125 def __init__(self, *args, **kwargs):
126 diffMode = kwargs.pop(
'diffMode', -1)
127 if diffMode > -1: self.
diffMode = diffMode
129 super(TestingUtilities, self).__init__(*args, **kwargs)
132 if diff_match_patch: self.addTypeEqualityFunc(str, self.
assertTextEqual)
133 super(TestingUtilities, self).setUp()
137 How to wrap correctly the unit testing diff? 138 https://stackoverflow.com/questions/52682351/how-to-wrap-correctly-the-unit-testing-diff 147 if expected != actual:
151 diffs = diff_match.diff_main(expected, actual)
154 diff_struct = diff_match.diff_linesToWords(expected, actual,
155 re.compile(
r'\b| ')
if self.
diffMode == 1
else re.compile(
r'\n|\r\n') )
157 lineText1 = diff_struct[0]
158 lineText2 = diff_struct[1]
159 lineArray = diff_struct[2]
161 diffs = diff_match.diff_main(lineText1, lineText2,
False);
162 diff_match.diff_charsToLines(diffs, lineArray);
163 diff_match.diff_cleanupSemantic(diffs)
169 msg =
"The strings does not match...\n" 171 self.fail( msg + diff_match.diff_prettyText(diffs) )
175 Called right after each Unit Test finish its execution, to clean up settings values. 177 LockableType.USE_STRING =
True 178 super(TestingUtilities, self).
tearDown()
180 def assertTextEqual(self, goal, results, msg=None, trim_tabs=' ', trim_spaces=' ', trim_plus='+', trim_lines=None, indent=""):
182 Remove both input texts indentation and trailing white spaces, then assertEquals() both 185 goal = wrap_text( goal, trim_tabs=trim_tabs, trim_spaces=trim_spaces,
186 trim_plus=trim_plus, indent=indent, trim_lines=trim_lines )
188 results = wrap_text( results, trim_tabs=trim_tabs, trim_spaces=trim_spaces,
189 trim_plus=trim_plus, indent=indent, trim_lines=trim_lines )
195 super( TestingUtilities, self ).assertEqual( goal, results, msg )
199 Helper function. Returns a string containing the unified diff of two multiline strings. 201 https://stackoverflow.com/questions/845276/how-to-print-the-comparison-of-two-multiline-strings-in-unified-diff-format 202 https://stackoverflow.com/questions/15864641/python-difflib-comparing-files 203 https://stackoverflow.com/questions/32359402/comparison-of-multi-line-strings-in-python-unit-test 205 expected = expected.splitlines( 1 )
206 actual = actual.splitlines( 1 )
209 if expected != actual:
210 diff = difflib.context_diff( expected, actual, fromfile=
'expected input', tofile=
'actual output', lineterm=
'\n' )
211 self.fail(
'\n' +
''.join( diff ) )