summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRémi Cardona <remi.cardona@logilab.fr>2015-09-10 15:53:25 +0200
committerRémi Cardona <remi.cardona@logilab.fr>2015-09-10 15:53:25 +0200
commitd41ea6b6eb567799c6a534cf004da520d6ae4f6e (patch)
treea71a1ddcfe6e05854750d74a1e3396c355a4062b
parenta21a01fde3c1225572773cb73232229d3505266a (diff)
downloadlogilab-common-d41ea6b6eb567799c6a534cf004da520d6ae4f6e.tar.gz
[testlib] Remove deprecated assert methods
Including those that are already defined in unittest2 >= 0.5.1. Closes #1716063.
-rw-r--r--logilab/common/testlib.py455
-rw-r--r--test/unittest_testlib.py48
2 files changed, 0 insertions, 503 deletions
diff --git a/logilab/common/testlib.py b/logilab/common/testlib.py
index a7724de..16f08d7 100644
--- a/logilab/common/testlib.py
+++ b/logilab/common/testlib.py
@@ -609,470 +609,15 @@ class TestCase(unittest.TestCase):
"""return a new instance of the defaultTestResult"""
return SkipAwareTestResult()
- skip = _deprecate(unittest.TestCase.skipTest)
- assertEquals = _deprecate(unittest.TestCase.assertEqual)
- assertNotEquals = _deprecate(unittest.TestCase.assertNotEqual)
- assertAlmostEquals = _deprecate(unittest.TestCase.assertAlmostEqual)
- assertNotAlmostEquals = _deprecate(unittest.TestCase.assertNotAlmostEqual)
-
def innerSkip(self, msg=None):
"""mark a generative test as skipped for the <msg> reason"""
msg = msg or 'test was skipped'
raise InnerTestSkipped(msg)
- @deprecated('Please use assertDictEqual instead.')
- def assertDictEquals(self, dict1, dict2, msg=None, context=None):
- """compares two dicts
-
- If the two dict differ, the first difference is shown in the error
- message
- :param dict1: a Python Dictionary
- :param dict2: a Python Dictionary
- :param msg: custom message (String) in case of failure
- """
- dict1 = dict(dict1)
- msgs = []
- for key, value in dict2.items():
- try:
- if dict1[key] != value:
- msgs.append('%r != %r for key %r' % (dict1[key], value,
- key))
- del dict1[key]
- except KeyError:
- msgs.append('missing %r key' % key)
- if dict1:
- msgs.append('dict2 is lacking %r' % dict1)
- if msg:
- self.failureException(msg)
- elif msgs:
- if context is not None:
- base = '%s\n' % context
- else:
- base = ''
- self.fail(base + '\n'.join(msgs))
-
- @deprecated('Please use assertCountEqual instead.')
- def assertUnorderedIterableEquals(self, got, expected, msg=None):
- """compares two iterable and shows difference between both
-
- :param got: the unordered Iterable that we found
- :param expected: the expected unordered Iterable
- :param msg: custom message (String) in case of failure
- """
- got, expected = list(got), list(expected)
- self.assertSetEqual(set(got), set(expected), msg)
- if len(got) != len(expected):
- if msg is None:
- msg = ['Iterable have the same elements but not the same number',
- '\t<element>\t<expected>i\t<got>']
- got_count = {}
- expected_count = {}
- for element in got:
- got_count[element] = got_count.get(element, 0) + 1
- for element in expected:
- expected_count[element] = expected_count.get(element, 0) + 1
- # we know that got_count.key() == expected_count.key()
- # because of assertSetEqual
- for element, count in got_count.iteritems():
- other_count = expected_count[element]
- if other_count != count:
- msg.append('\t%s\t%s\t%s' % (element, other_count, count))
-
- self.fail(msg)
-
- assertUnorderedIterableEqual = assertUnorderedIterableEquals
- assertUnordIterEquals = assertUnordIterEqual = assertUnorderedIterableEqual
-
- @deprecated('Please use assertSetEqual instead.')
- def assertSetEquals(self,got,expected, msg=None):
- """compares two sets and shows difference between both
-
- Don't use it for iterables other than sets.
-
- :param got: the Set that we found
- :param expected: the second Set to be compared to the first one
- :param msg: custom message (String) in case of failure
- """
-
- if not(isinstance(got, set) and isinstance(expected, set)):
- warnings.warn("the assertSetEquals function if now intended for set only."\
- "use assertUnorderedIterableEquals instead.",
- DeprecationWarning, 2)
- return self.assertUnorderedIterableEquals(got, expected, msg)
-
- items={}
- items['missing'] = expected - got
- items['unexpected'] = got - expected
- if any(items.itervalues()):
- if msg is None:
- msg = '\n'.join('%s:\n\t%s' % (key, "\n\t".join(str(value) for value in values))
- for key, values in items.iteritems() if values)
- self.fail(msg)
-
- @deprecated('Please use assertListEqual instead.')
- def assertListEquals(self, list_1, list_2, msg=None):
- """compares two lists
-
- If the two list differ, the first difference is shown in the error
- message
-
- :param list_1: a Python List
- :param list_2: a second Python List
- :param msg: custom message (String) in case of failure
- """
- _l1 = list_1[:]
- for i, value in enumerate(list_2):
- try:
- if _l1[0] != value:
- from pprint import pprint
- pprint(list_1)
- pprint(list_2)
- self.fail('%r != %r for index %d' % (_l1[0], value, i))
- del _l1[0]
- except IndexError:
- if msg is None:
- msg = 'list_1 has only %d elements, not %s '\
- '(at least %r missing)'% (i, len(list_2), value)
- self.fail(msg)
- if _l1:
- if msg is None:
- msg = 'list_2 is lacking %r' % _l1
- self.fail(msg)
-
- @deprecated('Non-standard. Please use assertMultiLineEqual instead.')
- def assertLinesEquals(self, string1, string2, msg=None, striplines=False):
- """compare two strings and assert that the text lines of the strings
- are equal.
-
- :param string1: a String
- :param string2: a String
- :param msg: custom message (String) in case of failure
- :param striplines: Boolean to trigger line stripping before comparing
- """
- lines1 = string1.splitlines()
- lines2 = string2.splitlines()
- if striplines:
- lines1 = [l.strip() for l in lines1]
- lines2 = [l.strip() for l in lines2]
- self.assertListEqual(lines1, lines2, msg)
- assertLineEqual = assertLinesEquals
-
- @deprecated('Non-standard: please copy test method to your TestCase class')
- def assertXMLWellFormed(self, stream, msg=None, context=2):
- """asserts the XML stream is well-formed (no DTD conformance check)
-
- :param context: number of context lines in standard message
- (show all data if negative).
- Only available with element tree
- """
- try:
- from xml.etree.ElementTree import parse
- self._assertETXMLWellFormed(stream, parse, msg)
- except ImportError:
- from xml.sax import make_parser, SAXParseException
- parser = make_parser()
- try:
- parser.parse(stream)
- except SAXParseException as ex:
- if msg is None:
- stream.seek(0)
- for _ in range(ex.getLineNumber()):
- line = stream.readline()
- pointer = ('' * (ex.getLineNumber() - 1)) + '^'
- msg = 'XML stream not well formed: %s\n%s%s' % (ex, line, pointer)
- self.fail(msg)
-
- @deprecated('Non-standard: please copy test method to your TestCase class')
- def assertXMLStringWellFormed(self, xml_string, msg=None, context=2):
- """asserts the XML string is well-formed (no DTD conformance check)
-
- :param context: number of context lines in standard message
- (show all data if negative).
- Only available with element tree
- """
- try:
- from xml.etree.ElementTree import fromstring
- except ImportError:
- from elementtree.ElementTree import fromstring
- self._assertETXMLWellFormed(xml_string, fromstring, msg)
-
- def _assertETXMLWellFormed(self, data, parse, msg=None, context=2):
- """internal function used by /assertXML(String)?WellFormed/ functions
-
- :param data: xml_data
- :param parse: appropriate parser function for this data
- :param msg: error message
- :param context: number of context lines in standard message
- (show all data if negative).
- Only available with element tree
- """
- from xml.parsers.expat import ExpatError
- try:
- from xml.etree.ElementTree import ParseError
- except ImportError:
- # compatibility for <python2.7
- ParseError = ExpatError
- try:
- parse(data)
- except (ExpatError, ParseError) as ex:
- if msg is None:
- if hasattr(data, 'readlines'): #file like object
- data.seek(0)
- lines = data.readlines()
- else:
- lines = data.splitlines(True)
- nb_lines = len(lines)
- context_lines = []
-
- # catch when ParseError doesn't set valid lineno
- if ex.lineno is not None:
- if context < 0:
- start = 1
- end = nb_lines
- else:
- start = max(ex.lineno-context, 1)
- end = min(ex.lineno+context, nb_lines)
- line_number_length = len('%i' % end)
- line_pattern = " %%%ii: %%s" % line_number_length
-
- for line_no in range(start, ex.lineno):
- context_lines.append(line_pattern % (line_no, lines[line_no-1]))
- context_lines.append(line_pattern % (ex.lineno, lines[ex.lineno-1]))
- context_lines.append('%s^\n' % (' ' * (1 + line_number_length + 2 +ex.offset)))
- for line_no in range(ex.lineno+1, end+1):
- context_lines.append(line_pattern % (line_no, lines[line_no-1]))
-
- rich_context = ''.join(context_lines)
- msg = 'XML stream not well formed: %s\n%s' % (ex, rich_context)
- self.fail(msg)
-
- @deprecated('Non-standard: please copy test method to your TestCase class')
- def assertXMLEqualsTuple(self, element, tup):
- """compare an ElementTree Element to a tuple formatted as follow:
- (tagname, [attrib[, children[, text[, tail]]]])"""
- # check tag
- self.assertTextEquals(element.tag, tup[0])
- # check attrib
- if len(element.attrib) or len(tup)>1:
- if len(tup)<=1:
- self.fail( "tuple %s has no attributes (%s expected)"%(tup,
- dict(element.attrib)))
- self.assertDictEqual(element.attrib, tup[1])
- # check children
- if len(element) or len(tup)>2:
- if len(tup)<=2:
- self.fail( "tuple %s has no children (%i expected)"%(tup,
- len(element)))
- if len(element) != len(tup[2]):
- self.fail( "tuple %s has %i children%s (%i expected)"%(tup,
- len(tup[2]),
- ('', 's')[len(tup[2])>1], len(element)))
- for index in range(len(tup[2])):
- self.assertXMLEqualsTuple(element[index], tup[2][index])
- #check text
- if element.text or len(tup)>3:
- if len(tup)<=3:
- self.fail( "tuple %s has no text value (%r expected)"%(tup,
- element.text))
- self.assertTextEquals(element.text, tup[3])
- #check tail
- if element.tail or len(tup)>4:
- if len(tup)<=4:
- self.fail( "tuple %s has no tail value (%r expected)"%(tup,
- element.tail))
- self.assertTextEquals(element.tail, tup[4])
-
- def _difftext(self, lines1, lines2, junk=None, msg_prefix='Texts differ'):
- junk = junk or (' ', '\t')
- # result is a generator
- result = difflib.ndiff(lines1, lines2, charjunk=lambda x: x in junk)
- read = []
- for line in result:
- read.append(line)
- # lines that don't start with a ' ' are diff ones
- if not line.startswith(' '):
- self.fail('\n'.join(['%s\n'%msg_prefix]+read + list(result)))
-
- @deprecated('Non-standard. Please use assertMultiLineEqual instead.')
- def assertTextEquals(self, text1, text2, junk=None,
- msg_prefix='Text differ', striplines=False):
- """compare two multiline strings (using difflib and splitlines())
-
- :param text1: a Python BaseString
- :param text2: a second Python Basestring
- :param junk: List of Caracters
- :param msg_prefix: String (message prefix)
- :param striplines: Boolean to trigger line stripping before comparing
- """
- msg = []
- if not isinstance(text1, string_types):
- msg.append('text1 is not a string (%s)'%(type(text1)))
- if not isinstance(text2, string_types):
- msg.append('text2 is not a string (%s)'%(type(text2)))
- if msg:
- self.fail('\n'.join(msg))
- lines1 = text1.strip().splitlines(True)
- lines2 = text2.strip().splitlines(True)
- if striplines:
- lines1 = [line.strip() for line in lines1]
- lines2 = [line.strip() for line in lines2]
- self._difftext(lines1, lines2, junk, msg_prefix)
- assertTextEqual = assertTextEquals
-
- @deprecated('Non-standard: please copy test method to your TestCase class')
- def assertStreamEquals(self, stream1, stream2, junk=None,
- msg_prefix='Stream differ'):
- """compare two streams (using difflib and readlines())"""
- # if stream2 is stream2, readlines() on stream1 will also read lines
- # in stream2, so they'll appear different, although they're not
- if stream1 is stream2:
- return
- # make sure we compare from the beginning of the stream
- stream1.seek(0)
- stream2.seek(0)
- # compare
- self._difftext(stream1.readlines(), stream2.readlines(), junk,
- msg_prefix)
-
- assertStreamEqual = assertStreamEquals
-
- @deprecated('Non-standard: please copy test method to your TestCase class')
- def assertFileEquals(self, fname1, fname2, junk=(' ', '\t')):
- """compares two files using difflib"""
- self.assertStreamEqual(open(fname1), open(fname2), junk,
- msg_prefix='Files differs\n-:%s\n+:%s\n'%(fname1, fname2))
-
- assertFileEqual = assertFileEquals
-
- @deprecated('Non-standard: please copy test method to your TestCase class')
- def assertDirEquals(self, path_a, path_b):
- """compares two files using difflib"""
- assert osp.exists(path_a), "%s doesn't exists" % path_a
- assert osp.exists(path_b), "%s doesn't exists" % path_b
-
- all_a = [ (ipath[len(path_a):].lstrip('/'), idirs, ifiles)
- for ipath, idirs, ifiles in os.walk(path_a)]
- all_a.sort(key=itemgetter(0))
-
- all_b = [ (ipath[len(path_b):].lstrip('/'), idirs, ifiles)
- for ipath, idirs, ifiles in os.walk(path_b)]
- all_b.sort(key=itemgetter(0))
-
- iter_a, iter_b = iter(all_a), iter(all_b)
- partial_iter = True
- ipath_a, idirs_a, ifiles_a = data_a = None, None, None
- while True:
- try:
- ipath_a, idirs_a, ifiles_a = datas_a = next(iter_a)
- partial_iter = False
- ipath_b, idirs_b, ifiles_b = datas_b = next(iter_b)
- partial_iter = True
-
-
- self.assertTrue(ipath_a == ipath_b,
- "unexpected %s in %s while looking %s from %s" %
- (ipath_a, path_a, ipath_b, path_b))
-
-
- errors = {}
- sdirs_a = set(idirs_a)
- sdirs_b = set(idirs_b)
- errors["unexpected directories"] = sdirs_a - sdirs_b
- errors["missing directories"] = sdirs_b - sdirs_a
-
- sfiles_a = set(ifiles_a)
- sfiles_b = set(ifiles_b)
- errors["unexpected files"] = sfiles_a - sfiles_b
- errors["missing files"] = sfiles_b - sfiles_a
-
-
- msgs = [ "%s: %s"% (name, items)
- for name, items in errors.items() if items]
-
- if msgs:
- msgs.insert(0, "%s and %s differ :" % (
- osp.join(path_a, ipath_a),
- osp.join(path_b, ipath_b),
- ))
- self.fail("\n".join(msgs))
-
- for files in (ifiles_a, ifiles_b):
- files.sort()
-
- for index, path in enumerate(ifiles_a):
- self.assertFileEquals(osp.join(path_a, ipath_a, path),
- osp.join(path_b, ipath_b, ifiles_b[index]))
-
- except StopIteration:
- break
-
- assertDirEqual = assertDirEquals
-
- def assertIsInstance(self, obj, klass, msg=None, strict=False):
- """check if an object is an instance of a class
-
- :param obj: the Python Object to be checked
- :param klass: the target class
- :param msg: a String for a custom message
- :param strict: if True, check that the class of <obj> is <klass>;
- else check with 'isinstance'
- """
- if strict:
- warnings.warn('[API] Non-standard. Strict parameter has vanished',
- DeprecationWarning, stacklevel=2)
- if msg is None:
- if strict:
- msg = '%r is not of class %s but of %s'
- else:
- msg = '%r is not an instance of %s but of %s'
- msg = msg % (obj, klass, type(obj))
- if strict:
- self.assertTrue(obj.__class__ is klass, msg)
- else:
- self.assertTrue(isinstance(obj, klass), msg)
-
- @deprecated('Please use assertIsNone instead.')
- def assertNone(self, obj, msg=None):
- """assert obj is None
-
- :param obj: Python Object to be tested
- """
- if msg is None:
- msg = "reference to %r when None expected"%(obj,)
- self.assertTrue( obj is None, msg )
-
- @deprecated('Please use assertIsNotNone instead.')
- def assertNotNone(self, obj, msg=None):
- """assert obj is not None"""
- if msg is None:
- msg = "unexpected reference to None"
- self.assertTrue( obj is not None, msg )
-
- @deprecated('Non-standard. Please use assertAlmostEqual instead.')
- def assertFloatAlmostEquals(self, obj, other, prec=1e-5,
- relative=False, msg=None):
- """compares if two floats have a distance smaller than expected
- precision.
-
- :param obj: a Float
- :param other: another Float to be comparted to <obj>
- :param prec: a Float describing the precision
- :param relative: boolean switching to relative/absolute precision
- :param msg: a String for a custom message
- """
- if msg is None:
- msg = "%r != %r" % (obj, other)
- if relative:
- prec = prec*math.fabs(obj)
- self.assertTrue(math.fabs(obj - other) < prec, msg)
-
if sys.version_info >= (3,2):
assertItemsEqual = unittest.TestCase.assertCountEqual
else:
assertCountEqual = unittest.TestCase.assertItemsEqual
- if sys.version_info < (2,7):
- def assertIsNotNone(self, value, *args, **kwargs):
- self.assertNotEqual(None, value, *args, **kwargs)
TestCase.assertItemsEqual = deprecated('assertItemsEqual is deprecated, use assertCountEqual')(
TestCase.assertItemsEqual)
diff --git a/test/unittest_testlib.py b/test/unittest_testlib.py
index 3dc9d07..bc60669 100644
--- a/test/unittest_testlib.py
+++ b/test/unittest_testlib.py
@@ -113,18 +113,6 @@ class TestlibTC(TestCase):
self.tc.assertListEqual(l1, l3)
self.tc.assertListEqual(l3, l1)
- def test_xml_valid(self):
- """tests xml is valid"""
- valid = """<root>
- <hello />
- <world>Logilab</world>
- </root>"""
- invalid = """<root><h2> </root>"""
- self.tc.assertXMLStringWellFormed(valid)
- self.assertRaises(AssertionError, self.tc.assertXMLStringWellFormed, invalid)
- invalid = """<root><h2 </h2> </root>"""
- self.assertRaises(AssertionError, self.tc.assertXMLStringWellFormed, invalid)
-
def test_equality_for_sets(self):
s1 = set('ab')
s2 = set('a')
@@ -132,42 +120,6 @@ class TestlibTC(TestCase):
self.tc.assertSetEqual(s1, s1)
self.tc.assertSetEqual(set(), set())
- def test_file_equality(self):
- foo = join(dirname(__file__), 'data', 'foo.txt')
- spam = join(dirname(__file__), 'data', 'spam.txt')
- self.assertRaises(AssertionError, self.tc.assertFileEqual, foo, spam)
- self.tc.assertFileEqual(foo, foo)
-
- def test_dir_equality(self):
- ref = join(dirname(__file__), 'data', 'reference_dir')
- same = join(dirname(__file__), 'data', 'same_dir')
- subdir_differ = join(dirname(__file__), 'data', 'subdir_differ_dir')
- file_differ = join(dirname(__file__), 'data', 'file_differ_dir')
- content_differ = join(dirname(__file__), 'data', 'content_differ_dir')
- ed1 = join(dirname(__file__), 'data', 'empty_dir_1')
- ed2 = join(dirname(__file__), 'data', 'empty_dir_2')
-
- for path in (ed1, ed2, join(subdir_differ, 'unexpected')):
- self.mkdir(path)
-
- self.assertDirEqual(ed1, ed2)
- self.assertDirEqual(ref, ref)
- self.assertDirEqual( ref, same)
- self.assertRaises(AssertionError, self.assertDirEqual, ed1, ref)
- self.assertRaises(AssertionError, self.assertDirEqual, ref, ed2)
- self.assertRaises(AssertionError, self.assertDirEqual, subdir_differ, ref)
- self.assertRaises(AssertionError, self.assertDirEqual, file_differ, ref)
- self.assertRaises(AssertionError, self.assertDirEqual, ref, content_differ)
-
- def test_stream_equality(self):
- foo = join(dirname(__file__), 'data', 'foo.txt')
- spam = join(dirname(__file__), 'data', 'spam.txt')
- stream1 = open(foo)
- self.tc.assertStreamEqual(stream1, stream1)
- stream1 = open(foo)
- stream2 = open(spam)
- self.assertRaises(AssertionError, self.tc.assertStreamEqual, stream1, stream2)
-
def test_text_equality(self):
self.assertRaises(AssertionError, self.tc.assertMultiLineEqual, "toto", 12)
self.assertRaises(AssertionError, self.tc.assertMultiLineEqual, "toto", 12)