Debug Tools
TestingUtilities Class Reference
Inheritance diagram for TestingUtilities:
Collaboration diagram for TestingUtilities:

Public Member Functions

def __init__ (self, args, kwargs)
 
def setUp (self)
 
def diffMatchPatchAssertEqual (self, expected, actual, msg=None)
 
def tearDown (self)
 
def assertTextEqual (self, goal, results, msg=None, trim_tabs=' ', trim_spaces=' ', trim_plus='+', trim_lines=None, indent="")
 
def unidiff_output (self, expected, actual)
 

Public Attributes

 diffMode
 

Static Public Attributes

 maxDiff = None
 Set the maximum size of the assertion error message when Unit Test fail.
 
int diffMode = 1
 Whether characters diff=0, words diff=1 or lines diff=2 will be used.
 

Detailed Description

    Holds common features across all Unit Tests.

Definition at line 115 of file testing_utilities.py.

Member Function Documentation

◆ assertTextEqual()

def assertTextEqual (   self,
  goal,
  results,
  msg = None,
  trim_tabs = '  ',
  trim_spaces = ' ',
  trim_plus = '+',
  trim_lines = None,
  indent = "" 
)
    Remove both input texts indentation and trailing white spaces, then assertEquals() both
    of the inputs.

Definition at line 180 of file testing_utilities.py.

References TestingUtilities.diffMatchPatchAssertEqual().

180  def assertTextEqual(self, goal, results, msg=None, trim_tabs=' ', trim_spaces=' ', trim_plus='+', trim_lines=None, indent=""):
181  """
182  Remove both input texts indentation and trailing white spaces, then assertEquals() both
183  of the inputs.
184  """
185  goal = wrap_text( goal, trim_tabs=trim_tabs, trim_spaces=trim_spaces,
186  trim_plus=trim_plus, indent=indent, trim_lines=trim_lines )
187 
188  results = wrap_text( results, trim_tabs=trim_tabs, trim_spaces=trim_spaces,
189  trim_plus=trim_plus, indent=indent, trim_lines=trim_lines )
190 
191  if diff_match_patch:
192  self.diffMatchPatchAssertEqual( goal, results, msg=msg )
193 
194  else:
195  super( TestingUtilities, self ).assertEqual( goal, results, msg )
196 
Here is the call graph for this function:

◆ diffMatchPatchAssertEqual()

def diffMatchPatchAssertEqual (   self,
  expected,
  actual,
  msg = None 
)
    How to wrap correctly the unit testing diff?
    https://stackoverflow.com/questions/52682351/how-to-wrap-correctly-the-unit-testing-diff

Definition at line 135 of file testing_utilities.py.

References TestingUtilities.diffMode.

Referenced by TestingUtilities.assertTextEqual().

135  def diffMatchPatchAssertEqual(self, expected, actual, msg=None):
136  """
137  How to wrap correctly the unit testing diff?
138  https://stackoverflow.com/questions/52682351/how-to-wrap-correctly-the-unit-testing-diff
139  """
140  # print( '\n\nexpected\n%s' % expected )
141  # print( '\n\nactual\n%s' % actual )
142 
143  # print( goal.encode( 'ascii' ) )
144  # print( results.encode( 'ascii' ) )
145 
146  # self.unidiff_output( goal, results )
147  if expected != actual:
148  diff_match = diffmatchpatch()
149 
150  if self.diffMode == 0:
151  diffs = diff_match.diff_main(expected, actual)
152 
153  else:
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') )
156 
157  lineText1 = diff_struct[0] # .chars1;
158  lineText2 = diff_struct[1] # .chars2;
159  lineArray = diff_struct[2] # .lineArray;
160 
161  diffs = diff_match.diff_main(lineText1, lineText2, False);
162  diff_match.diff_charsToLines(diffs, lineArray);
163  diff_match.diff_cleanupSemantic(diffs)
164 
165  if msg:
166  msg += '\n'
167 
168  else:
169  msg = "The strings does not match...\n"
170 
171  self.fail( msg + diff_match.diff_prettyText(diffs) )
172 
Here is the caller graph for this function:

◆ tearDown()

def tearDown (   self)
    Called right after each Unit Test finish its execution, to clean up settings values.

Definition at line 173 of file testing_utilities.py.

173  def tearDown(self):
174  """
175  Called right after each Unit Test finish its execution, to clean up settings values.
176  """
177  LockableType.USE_STRING = True
178  super(TestingUtilities, self).tearDown()
179 

◆ unidiff_output()

def unidiff_output (   self,
  expected,
  actual 
)
    Helper function. Returns a string containing the unified diff of two multiline strings.

    https://stackoverflow.com/questions/845276/how-to-print-the-comparison-of-two-multiline-strings-in-unified-diff-format
    https://stackoverflow.com/questions/15864641/python-difflib-comparing-files
    https://stackoverflow.com/questions/32359402/comparison-of-multi-line-strings-in-python-unit-test

Definition at line 197 of file testing_utilities.py.

197  def unidiff_output(self, expected, actual):
198  """
199  Helper function. Returns a string containing the unified diff of two multiline strings.
200 
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
204  """
205  expected = expected.splitlines( 1 )
206  actual = actual.splitlines( 1 )
207 
208  # diff = difflib.ndiff( expected, actual )
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 ) )
212 
213 

The documentation for this class was generated from the following file: