+++ /dev/null
-"""Test case implementation"""\r
-\r
-import collections\r
-import sys\r
-import functools\r
-import difflib\r
-import pprint\r
-import re\r
-import warnings\r
-\r
-from . import result\r
-from .util import (\r
- strclass, safe_repr, unorderable_list_difference,\r
- _count_diff_all_purpose, _count_diff_hashable\r
-)\r
-\r
-\r
-__unittest = True\r
-\r
-\r
-DIFF_OMITTED = ('\nDiff is %s characters long. '\r
- 'Set self.maxDiff to None to see it.')\r
-\r
-class SkipTest(Exception):\r
- """\r
- Raise this exception in a test to skip it.\r
-\r
- Usually you can use TestResult.skip() or one of the skipping decorators\r
- instead of raising this directly.\r
- """\r
- pass\r
-\r
-class _ExpectedFailure(Exception):\r
- """\r
- Raise this when a test is expected to fail.\r
-\r
- This is an implementation detail.\r
- """\r
-\r
- def __init__(self, exc_info):\r
- super(_ExpectedFailure, self).__init__()\r
- self.exc_info = exc_info\r
-\r
-class _UnexpectedSuccess(Exception):\r
- """\r
- The test was supposed to fail, but it didn't!\r
- """\r
- pass\r
-\r
-def _id(obj):\r
- return obj\r
-\r
-def skip(reason):\r
- """\r
- Unconditionally skip a test.\r
- """\r
- def decorator(test_item):\r
- if not (isinstance(test_item, type) and issubclass(test_item, TestCase)):\r
- @functools.wraps(test_item)\r
- def skip_wrapper(*args, **kwargs):\r
- raise SkipTest(reason)\r
- test_item = skip_wrapper\r
-\r
- test_item.__unittest_skip__ = True\r
- test_item.__unittest_skip_why__ = reason\r
- return test_item\r
- return decorator\r
-\r
-def skipIf(condition, reason):\r
- """\r
- Skip a test if the condition is true.\r
- """\r
- if condition:\r
- return skip(reason)\r
- return _id\r
-\r
-def skipUnless(condition, reason):\r
- """\r
- Skip a test unless the condition is true.\r
- """\r
- if not condition:\r
- return skip(reason)\r
- return _id\r
-\r
-\r
-def expectedFailure(func):\r
- @functools.wraps(func)\r
- def wrapper(*args, **kwargs):\r
- try:\r
- func(*args, **kwargs)\r
- except Exception:\r
- raise _ExpectedFailure(sys.exc_info())\r
- raise _UnexpectedSuccess\r
- return wrapper\r
-\r
-\r
-class _AssertRaisesContext(object):\r
- """A context manager used to implement TestCase.assertRaises* methods."""\r
-\r
- def __init__(self, expected, test_case, expected_regexp=None):\r
- self.expected = expected\r
- self.failureException = test_case.failureException\r
- self.expected_regexp = expected_regexp\r
-\r
- def __enter__(self):\r
- return self\r
-\r
- def __exit__(self, exc_type, exc_value, tb):\r
- if exc_type is None:\r
- try:\r
- exc_name = self.expected.__name__\r
- except AttributeError:\r
- exc_name = str(self.expected)\r
- raise self.failureException(\r
- "{0} not raised".format(exc_name))\r
- if not issubclass(exc_type, self.expected):\r
- # let unexpected exceptions pass through\r
- return False\r
- self.exception = exc_value # store for later retrieval\r
- if self.expected_regexp is None:\r
- return True\r
-\r
- expected_regexp = self.expected_regexp\r
- if isinstance(expected_regexp, basestring):\r
- expected_regexp = re.compile(expected_regexp)\r
- if not expected_regexp.search(str(exc_value)):\r
- raise self.failureException('"%s" does not match "%s"' %\r
- (expected_regexp.pattern, str(exc_value)))\r
- return True\r
-\r
-\r
-class TestCase(object):\r
- """A class whose instances are single test cases.\r
-\r
- By default, the test code itself should be placed in a method named\r
- 'runTest'.\r
-\r
- If the fixture may be used for many test cases, create as\r
- many test methods as are needed. When instantiating such a TestCase\r
- subclass, specify in the constructor arguments the name of the test method\r
- that the instance is to execute.\r
-\r
- Test authors should subclass TestCase for their own tests. Construction\r
- and deconstruction of the test's environment ('fixture') can be\r
- implemented by overriding the 'setUp' and 'tearDown' methods respectively.\r
-\r
- If it is necessary to override the __init__ method, the base class\r
- __init__ method must always be called. It is important that subclasses\r
- should not change the signature of their __init__ method, since instances\r
- of the classes are instantiated automatically by parts of the framework\r
- in order to be run.\r
- """\r
-\r
- # This attribute determines which exception will be raised when\r
- # the instance's assertion methods fail; test methods raising this\r
- # exception will be deemed to have 'failed' rather than 'errored'\r
-\r
- failureException = AssertionError\r
-\r
- # This attribute determines whether long messages (including repr of\r
- # objects used in assert methods) will be printed on failure in *addition*\r
- # to any explicit message passed.\r
-\r
- longMessage = False\r
-\r
- # This attribute sets the maximum length of a diff in failure messages\r
- # by assert methods using difflib. It is looked up as an instance attribute\r
- # so can be configured by individual tests if required.\r
-\r
- maxDiff = 80*8\r
-\r
- # If a string is longer than _diffThreshold, use normal comparison instead\r
- # of difflib. See #11763.\r
- _diffThreshold = 2**16\r
-\r
- # Attribute used by TestSuite for classSetUp\r
-\r
- _classSetupFailed = False\r
-\r
- def __init__(self, methodName='runTest'):\r
- """Create an instance of the class that will use the named test\r
- method when executed. Raises a ValueError if the instance does\r
- not have a method with the specified name.\r
- """\r
- self._testMethodName = methodName\r
- self._resultForDoCleanups = None\r
- try:\r
- testMethod = getattr(self, methodName)\r
- except AttributeError:\r
- raise ValueError("no such test method in %s: %s" %\r
- (self.__class__, methodName))\r
- self._testMethodDoc = testMethod.__doc__\r
- self._cleanups = []\r
-\r
- # Map types to custom assertEqual functions that will compare\r
- # instances of said type in more detail to generate a more useful\r
- # error message.\r
- self._type_equality_funcs = {}\r
- self.addTypeEqualityFunc(dict, self.assertDictEqual)\r
- self.addTypeEqualityFunc(list, self.assertListEqual)\r
- self.addTypeEqualityFunc(tuple, self.assertTupleEqual)\r
- self.addTypeEqualityFunc(set, self.assertSetEqual)\r
- self.addTypeEqualityFunc(frozenset, self.assertSetEqual)\r
- self.addTypeEqualityFunc(unicode, self.assertMultiLineEqual)\r
-\r
- def addTypeEqualityFunc(self, typeobj, function):\r
- """Add a type specific assertEqual style function to compare a type.\r
-\r
- This method is for use by TestCase subclasses that need to register\r
- their own type equality functions to provide nicer error messages.\r
-\r
- Args:\r
- typeobj: The data type to call this function on when both values\r
- are of the same type in assertEqual().\r
- function: The callable taking two arguments and an optional\r
- msg= argument that raises self.failureException with a\r
- useful error message when the two arguments are not equal.\r
- """\r
- self._type_equality_funcs[typeobj] = function\r
-\r
- def addCleanup(self, function, *args, **kwargs):\r
- """Add a function, with arguments, to be called when the test is\r
- completed. Functions added are called on a LIFO basis and are\r
- called after tearDown on test failure or success.\r
-\r
- Cleanup items are called even if setUp fails (unlike tearDown)."""\r
- self._cleanups.append((function, args, kwargs))\r
-\r
- def setUp(self):\r
- "Hook method for setting up the test fixture before exercising it."\r
- pass\r
-\r
- def tearDown(self):\r
- "Hook method for deconstructing the test fixture after testing it."\r
- pass\r
-\r
- @classmethod\r
- def setUpClass(cls):\r
- "Hook method for setting up class fixture before running tests in the class."\r
-\r
- @classmethod\r
- def tearDownClass(cls):\r
- "Hook method for deconstructing the class fixture after running all tests in the class."\r
-\r
- def countTestCases(self):\r
- return 1\r
-\r
- def defaultTestResult(self):\r
- return result.TestResult()\r
-\r
- def shortDescription(self):\r
- """Returns a one-line description of the test, or None if no\r
- description has been provided.\r
-\r
- The default implementation of this method returns the first line of\r
- the specified test method's docstring.\r
- """\r
- doc = self._testMethodDoc\r
- return doc and doc.split("\n")[0].strip() or None\r
-\r
-\r
- def id(self):\r
- return "%s.%s" % (strclass(self.__class__), self._testMethodName)\r
-\r
- def __eq__(self, other):\r
- if type(self) is not type(other):\r
- return NotImplemented\r
-\r
- return self._testMethodName == other._testMethodName\r
-\r
- def __ne__(self, other):\r
- return not self == other\r
-\r
- def __hash__(self):\r
- return hash((type(self), self._testMethodName))\r
-\r
- def __str__(self):\r
- return "%s (%s)" % (self._testMethodName, strclass(self.__class__))\r
-\r
- def __repr__(self):\r
- return "<%s testMethod=%s>" % \\r
- (strclass(self.__class__), self._testMethodName)\r
-\r
- def _addSkip(self, result, reason):\r
- addSkip = getattr(result, 'addSkip', None)\r
- if addSkip is not None:\r
- addSkip(self, reason)\r
- else:\r
- warnings.warn("TestResult has no addSkip method, skips not reported",\r
- RuntimeWarning, 2)\r
- result.addSuccess(self)\r
-\r
- def run(self, result=None):\r
- orig_result = result\r
- if result is None:\r
- result = self.defaultTestResult()\r
- startTestRun = getattr(result, 'startTestRun', None)\r
- if startTestRun is not None:\r
- startTestRun()\r
-\r
- self._resultForDoCleanups = result\r
- result.startTest(self)\r
-\r
- testMethod = getattr(self, self._testMethodName)\r
- if (getattr(self.__class__, "__unittest_skip__", False) or\r
- getattr(testMethod, "__unittest_skip__", False)):\r
- # If the class or method was skipped.\r
- try:\r
- skip_why = (getattr(self.__class__, '__unittest_skip_why__', '')\r
- or getattr(testMethod, '__unittest_skip_why__', ''))\r
- self._addSkip(result, skip_why)\r
- finally:\r
- result.stopTest(self)\r
- return\r
- try:\r
- success = False\r
- try:\r
- self.setUp()\r
- except SkipTest as e:\r
- self._addSkip(result, str(e))\r
- except KeyboardInterrupt:\r
- raise\r
- except:\r
- result.addError(self, sys.exc_info())\r
- else:\r
- try:\r
- testMethod()\r
- except KeyboardInterrupt:\r
- raise\r
- except self.failureException:\r
- result.addFailure(self, sys.exc_info())\r
- except _ExpectedFailure as e:\r
- addExpectedFailure = getattr(result, 'addExpectedFailure', None)\r
- if addExpectedFailure is not None:\r
- addExpectedFailure(self, e.exc_info)\r
- else:\r
- warnings.warn("TestResult has no addExpectedFailure method, reporting as passes",\r
- RuntimeWarning)\r
- result.addSuccess(self)\r
- except _UnexpectedSuccess:\r
- addUnexpectedSuccess = getattr(result, 'addUnexpectedSuccess', None)\r
- if addUnexpectedSuccess is not None:\r
- addUnexpectedSuccess(self)\r
- else:\r
- warnings.warn("TestResult has no addUnexpectedSuccess method, reporting as failures",\r
- RuntimeWarning)\r
- result.addFailure(self, sys.exc_info())\r
- except SkipTest as e:\r
- self._addSkip(result, str(e))\r
- except:\r
- result.addError(self, sys.exc_info())\r
- else:\r
- success = True\r
-\r
- try:\r
- self.tearDown()\r
- except KeyboardInterrupt:\r
- raise\r
- except:\r
- result.addError(self, sys.exc_info())\r
- success = False\r
-\r
- cleanUpSuccess = self.doCleanups()\r
- success = success and cleanUpSuccess\r
- if success:\r
- result.addSuccess(self)\r
- finally:\r
- result.stopTest(self)\r
- if orig_result is None:\r
- stopTestRun = getattr(result, 'stopTestRun', None)\r
- if stopTestRun is not None:\r
- stopTestRun()\r
-\r
- def doCleanups(self):\r
- """Execute all cleanup functions. Normally called for you after\r
- tearDown."""\r
- result = self._resultForDoCleanups\r
- ok = True\r
- while self._cleanups:\r
- function, args, kwargs = self._cleanups.pop(-1)\r
- try:\r
- function(*args, **kwargs)\r
- except KeyboardInterrupt:\r
- raise\r
- except:\r
- ok = False\r
- result.addError(self, sys.exc_info())\r
- return ok\r
-\r
- def __call__(self, *args, **kwds):\r
- return self.run(*args, **kwds)\r
-\r
- def debug(self):\r
- """Run the test without collecting errors in a TestResult"""\r
- self.setUp()\r
- getattr(self, self._testMethodName)()\r
- self.tearDown()\r
- while self._cleanups:\r
- function, args, kwargs = self._cleanups.pop(-1)\r
- function(*args, **kwargs)\r
-\r
- def skipTest(self, reason):\r
- """Skip this test."""\r
- raise SkipTest(reason)\r
-\r
- def fail(self, msg=None):\r
- """Fail immediately, with the given message."""\r
- raise self.failureException(msg)\r
-\r
- def assertFalse(self, expr, msg=None):\r
- """Check that the expression is false."""\r
- if expr:\r
- msg = self._formatMessage(msg, "%s is not false" % safe_repr(expr))\r
- raise self.failureException(msg)\r
-\r
- def assertTrue(self, expr, msg=None):\r
- """Check that the expression is true."""\r
- if not expr:\r
- msg = self._formatMessage(msg, "%s is not true" % safe_repr(expr))\r
- raise self.failureException(msg)\r
-\r
- def _formatMessage(self, msg, standardMsg):\r
- """Honour the longMessage attribute when generating failure messages.\r
- If longMessage is False this means:\r
- * Use only an explicit message if it is provided\r
- * Otherwise use the standard message for the assert\r
-\r
- If longMessage is True:\r
- * Use the standard message\r
- * If an explicit message is provided, plus ' : ' and the explicit message\r
- """\r
- if not self.longMessage:\r
- return msg or standardMsg\r
- if msg is None:\r
- return standardMsg\r
- try:\r
- # don't switch to '{}' formatting in Python 2.X\r
- # it changes the way unicode input is handled\r
- return '%s : %s' % (standardMsg, msg)\r
- except UnicodeDecodeError:\r
- return '%s : %s' % (safe_repr(standardMsg), safe_repr(msg))\r
-\r
-\r
- def assertRaises(self, excClass, callableObj=None, *args, **kwargs):\r
- """Fail unless an exception of class excClass is thrown\r
- by callableObj when invoked with arguments args and keyword\r
- arguments kwargs. If a different type of exception is\r
- thrown, it will not be caught, and the test case will be\r
- deemed to have suffered an error, exactly as for an\r
- unexpected exception.\r
-\r
- If called with callableObj omitted or None, will return a\r
- context object used like this::\r
-\r
- with self.assertRaises(SomeException):\r
- do_something()\r
-\r
- The context manager keeps a reference to the exception as\r
- the 'exception' attribute. This allows you to inspect the\r
- exception after the assertion::\r
-\r
- with self.assertRaises(SomeException) as cm:\r
- do_something()\r
- the_exception = cm.exception\r
- self.assertEqual(the_exception.error_code, 3)\r
- """\r
- context = _AssertRaisesContext(excClass, self)\r
- if callableObj is None:\r
- return context\r
- with context:\r
- callableObj(*args, **kwargs)\r
-\r
- def _getAssertEqualityFunc(self, first, second):\r
- """Get a detailed comparison function for the types of the two args.\r
-\r
- Returns: A callable accepting (first, second, msg=None) that will\r
- raise a failure exception if first != second with a useful human\r
- readable error message for those types.\r
- """\r
- #\r
- # NOTE(gregory.p.smith): I considered isinstance(first, type(second))\r
- # and vice versa. I opted for the conservative approach in case\r
- # subclasses are not intended to be compared in detail to their super\r
- # class instances using a type equality func. This means testing\r
- # subtypes won't automagically use the detailed comparison. Callers\r
- # should use their type specific assertSpamEqual method to compare\r
- # subclasses if the detailed comparison is desired and appropriate.\r
- # See the discussion in http://bugs.python.org/issue2578.\r
- #\r
- if type(first) is type(second):\r
- asserter = self._type_equality_funcs.get(type(first))\r
- if asserter is not None:\r
- return asserter\r
-\r
- return self._baseAssertEqual\r
-\r
- def _baseAssertEqual(self, first, second, msg=None):\r
- """The default assertEqual implementation, not type specific."""\r
- if not first == second:\r
- standardMsg = '%s != %s' % (safe_repr(first), safe_repr(second))\r
- msg = self._formatMessage(msg, standardMsg)\r
- raise self.failureException(msg)\r
-\r
- def assertEqual(self, first, second, msg=None):\r
- """Fail if the two objects are unequal as determined by the '=='\r
- operator.\r
- """\r
- assertion_func = self._getAssertEqualityFunc(first, second)\r
- assertion_func(first, second, msg=msg)\r
-\r
- def assertNotEqual(self, first, second, msg=None):\r
- """Fail if the two objects are equal as determined by the '=='\r
- operator.\r
- """\r
- if not first != second:\r
- msg = self._formatMessage(msg, '%s == %s' % (safe_repr(first),\r
- safe_repr(second)))\r
- raise self.failureException(msg)\r
-\r
-\r
- def assertAlmostEqual(self, first, second, places=None, msg=None, delta=None):\r
- """Fail if the two objects are unequal as determined by their\r
- difference rounded to the given number of decimal places\r
- (default 7) and comparing to zero, or by comparing that the\r
- between the two objects is more than the given delta.\r
-\r
- Note that decimal places (from zero) are usually not the same\r
- as significant digits (measured from the most signficant digit).\r
-\r
- If the two objects compare equal then they will automatically\r
- compare almost equal.\r
- """\r
- if first == second:\r
- # shortcut\r
- return\r
- if delta is not None and places is not None:\r
- raise TypeError("specify delta or places not both")\r
-\r
- if delta is not None:\r
- if abs(first - second) <= delta:\r
- return\r
-\r
- standardMsg = '%s != %s within %s delta' % (safe_repr(first),\r
- safe_repr(second),\r
- safe_repr(delta))\r
- else:\r
- if places is None:\r
- places = 7\r
-\r
- if round(abs(second-first), places) == 0:\r
- return\r
-\r
- standardMsg = '%s != %s within %r places' % (safe_repr(first),\r
- safe_repr(second),\r
- places)\r
- msg = self._formatMessage(msg, standardMsg)\r
- raise self.failureException(msg)\r
-\r
- def assertNotAlmostEqual(self, first, second, places=None, msg=None, delta=None):\r
- """Fail if the two objects are equal as determined by their\r
- difference rounded to the given number of decimal places\r
- (default 7) and comparing to zero, or by comparing that the\r
- between the two objects is less than the given delta.\r
-\r
- Note that decimal places (from zero) are usually not the same\r
- as significant digits (measured from the most signficant digit).\r
-\r
- Objects that are equal automatically fail.\r
- """\r
- if delta is not None and places is not None:\r
- raise TypeError("specify delta or places not both")\r
- if delta is not None:\r
- if not (first == second) and abs(first - second) > delta:\r
- return\r
- standardMsg = '%s == %s within %s delta' % (safe_repr(first),\r
- safe_repr(second),\r
- safe_repr(delta))\r
- else:\r
- if places is None:\r
- places = 7\r
- if not (first == second) and round(abs(second-first), places) != 0:\r
- return\r
- standardMsg = '%s == %s within %r places' % (safe_repr(first),\r
- safe_repr(second),\r
- places)\r
-\r
- msg = self._formatMessage(msg, standardMsg)\r
- raise self.failureException(msg)\r
-\r
- # Synonyms for assertion methods\r
-\r
- # The plurals are undocumented. Keep them that way to discourage use.\r
- # Do not add more. Do not remove.\r
- # Going through a deprecation cycle on these would annoy many people.\r
- assertEquals = assertEqual\r
- assertNotEquals = assertNotEqual\r
- assertAlmostEquals = assertAlmostEqual\r
- assertNotAlmostEquals = assertNotAlmostEqual\r
- assert_ = assertTrue\r
-\r
- # These fail* assertion method names are pending deprecation and will\r
- # be a DeprecationWarning in 3.2; http://bugs.python.org/issue2578\r
- def _deprecate(original_func):\r
- def deprecated_func(*args, **kwargs):\r
- warnings.warn(\r
- 'Please use {0} instead.'.format(original_func.__name__),\r
- PendingDeprecationWarning, 2)\r
- return original_func(*args, **kwargs)\r
- return deprecated_func\r
-\r
- failUnlessEqual = _deprecate(assertEqual)\r
- failIfEqual = _deprecate(assertNotEqual)\r
- failUnlessAlmostEqual = _deprecate(assertAlmostEqual)\r
- failIfAlmostEqual = _deprecate(assertNotAlmostEqual)\r
- failUnless = _deprecate(assertTrue)\r
- failUnlessRaises = _deprecate(assertRaises)\r
- failIf = _deprecate(assertFalse)\r
-\r
- def assertSequenceEqual(self, seq1, seq2, msg=None, seq_type=None):\r
- """An equality assertion for ordered sequences (like lists and tuples).\r
-\r
- For the purposes of this function, a valid ordered sequence type is one\r
- which can be indexed, has a length, and has an equality operator.\r
-\r
- Args:\r
- seq1: The first sequence to compare.\r
- seq2: The second sequence to compare.\r
- seq_type: The expected datatype of the sequences, or None if no\r
- datatype should be enforced.\r
- msg: Optional message to use on failure instead of a list of\r
- differences.\r
- """\r
- if seq_type is not None:\r
- seq_type_name = seq_type.__name__\r
- if not isinstance(seq1, seq_type):\r
- raise self.failureException('First sequence is not a %s: %s'\r
- % (seq_type_name, safe_repr(seq1)))\r
- if not isinstance(seq2, seq_type):\r
- raise self.failureException('Second sequence is not a %s: %s'\r
- % (seq_type_name, safe_repr(seq2)))\r
- else:\r
- seq_type_name = "sequence"\r
-\r
- differing = None\r
- try:\r
- len1 = len(seq1)\r
- except (TypeError, NotImplementedError):\r
- differing = 'First %s has no length. Non-sequence?' % (\r
- seq_type_name)\r
-\r
- if differing is None:\r
- try:\r
- len2 = len(seq2)\r
- except (TypeError, NotImplementedError):\r
- differing = 'Second %s has no length. Non-sequence?' % (\r
- seq_type_name)\r
-\r
- if differing is None:\r
- if seq1 == seq2:\r
- return\r
-\r
- seq1_repr = safe_repr(seq1)\r
- seq2_repr = safe_repr(seq2)\r
- if len(seq1_repr) > 30:\r
- seq1_repr = seq1_repr[:30] + '...'\r
- if len(seq2_repr) > 30:\r
- seq2_repr = seq2_repr[:30] + '...'\r
- elements = (seq_type_name.capitalize(), seq1_repr, seq2_repr)\r
- differing = '%ss differ: %s != %s\n' % elements\r
-\r
- for i in xrange(min(len1, len2)):\r
- try:\r
- item1 = seq1[i]\r
- except (TypeError, IndexError, NotImplementedError):\r
- differing += ('\nUnable to index element %d of first %s\n' %\r
- (i, seq_type_name))\r
- break\r
-\r
- try:\r
- item2 = seq2[i]\r
- except (TypeError, IndexError, NotImplementedError):\r
- differing += ('\nUnable to index element %d of second %s\n' %\r
- (i, seq_type_name))\r
- break\r
-\r
- if item1 != item2:\r
- differing += ('\nFirst differing element %d:\n%s\n%s\n' %\r
- (i, item1, item2))\r
- break\r
- else:\r
- if (len1 == len2 and seq_type is None and\r
- type(seq1) != type(seq2)):\r
- # The sequences are the same, but have differing types.\r
- return\r
-\r
- if len1 > len2:\r
- differing += ('\nFirst %s contains %d additional '\r
- 'elements.\n' % (seq_type_name, len1 - len2))\r
- try:\r
- differing += ('First extra element %d:\n%s\n' %\r
- (len2, seq1[len2]))\r
- except (TypeError, IndexError, NotImplementedError):\r
- differing += ('Unable to index element %d '\r
- 'of first %s\n' % (len2, seq_type_name))\r
- elif len1 < len2:\r
- differing += ('\nSecond %s contains %d additional '\r
- 'elements.\n' % (seq_type_name, len2 - len1))\r
- try:\r
- differing += ('First extra element %d:\n%s\n' %\r
- (len1, seq2[len1]))\r
- except (TypeError, IndexError, NotImplementedError):\r
- differing += ('Unable to index element %d '\r
- 'of second %s\n' % (len1, seq_type_name))\r
- standardMsg = differing\r
- diffMsg = '\n' + '\n'.join(\r
- difflib.ndiff(pprint.pformat(seq1).splitlines(),\r
- pprint.pformat(seq2).splitlines()))\r
- standardMsg = self._truncateMessage(standardMsg, diffMsg)\r
- msg = self._formatMessage(msg, standardMsg)\r
- self.fail(msg)\r
-\r
- def _truncateMessage(self, message, diff):\r
- max_diff = self.maxDiff\r
- if max_diff is None or len(diff) <= max_diff:\r
- return message + diff\r
- return message + (DIFF_OMITTED % len(diff))\r
-\r
- def assertListEqual(self, list1, list2, msg=None):\r
- """A list-specific equality assertion.\r
-\r
- Args:\r
- list1: The first list to compare.\r
- list2: The second list to compare.\r
- msg: Optional message to use on failure instead of a list of\r
- differences.\r
-\r
- """\r
- self.assertSequenceEqual(list1, list2, msg, seq_type=list)\r
-\r
- def assertTupleEqual(self, tuple1, tuple2, msg=None):\r
- """A tuple-specific equality assertion.\r
-\r
- Args:\r
- tuple1: The first tuple to compare.\r
- tuple2: The second tuple to compare.\r
- msg: Optional message to use on failure instead of a list of\r
- differences.\r
- """\r
- self.assertSequenceEqual(tuple1, tuple2, msg, seq_type=tuple)\r
-\r
- def assertSetEqual(self, set1, set2, msg=None):\r
- """A set-specific equality assertion.\r
-\r
- Args:\r
- set1: The first set to compare.\r
- set2: The second set to compare.\r
- msg: Optional message to use on failure instead of a list of\r
- differences.\r
-\r
- assertSetEqual uses ducktyping to support different types of sets, and\r
- is optimized for sets specifically (parameters must support a\r
- difference method).\r
- """\r
- try:\r
- difference1 = set1.difference(set2)\r
- except TypeError, e:\r
- self.fail('invalid type when attempting set difference: %s' % e)\r
- except AttributeError, e:\r
- self.fail('first argument does not support set difference: %s' % e)\r
-\r
- try:\r
- difference2 = set2.difference(set1)\r
- except TypeError, e:\r
- self.fail('invalid type when attempting set difference: %s' % e)\r
- except AttributeError, e:\r
- self.fail('second argument does not support set difference: %s' % e)\r
-\r
- if not (difference1 or difference2):\r
- return\r
-\r
- lines = []\r
- if difference1:\r
- lines.append('Items in the first set but not the second:')\r
- for item in difference1:\r
- lines.append(repr(item))\r
- if difference2:\r
- lines.append('Items in the second set but not the first:')\r
- for item in difference2:\r
- lines.append(repr(item))\r
-\r
- standardMsg = '\n'.join(lines)\r
- self.fail(self._formatMessage(msg, standardMsg))\r
-\r
- def assertIn(self, member, container, msg=None):\r
- """Just like self.assertTrue(a in b), but with a nicer default message."""\r
- if member not in container:\r
- standardMsg = '%s not found in %s' % (safe_repr(member),\r
- safe_repr(container))\r
- self.fail(self._formatMessage(msg, standardMsg))\r
-\r
- def assertNotIn(self, member, container, msg=None):\r
- """Just like self.assertTrue(a not in b), but with a nicer default message."""\r
- if member in container:\r
- standardMsg = '%s unexpectedly found in %s' % (safe_repr(member),\r
- safe_repr(container))\r
- self.fail(self._formatMessage(msg, standardMsg))\r
-\r
- def assertIs(self, expr1, expr2, msg=None):\r
- """Just like self.assertTrue(a is b), but with a nicer default message."""\r
- if expr1 is not expr2:\r
- standardMsg = '%s is not %s' % (safe_repr(expr1),\r
- safe_repr(expr2))\r
- self.fail(self._formatMessage(msg, standardMsg))\r
-\r
- def assertIsNot(self, expr1, expr2, msg=None):\r
- """Just like self.assertTrue(a is not b), but with a nicer default message."""\r
- if expr1 is expr2:\r
- standardMsg = 'unexpectedly identical: %s' % (safe_repr(expr1),)\r
- self.fail(self._formatMessage(msg, standardMsg))\r
-\r
- def assertDictEqual(self, d1, d2, msg=None):\r
- self.assertIsInstance(d1, dict, 'First argument is not a dictionary')\r
- self.assertIsInstance(d2, dict, 'Second argument is not a dictionary')\r
-\r
- if d1 != d2:\r
- standardMsg = '%s != %s' % (safe_repr(d1, True), safe_repr(d2, True))\r
- diff = ('\n' + '\n'.join(difflib.ndiff(\r
- pprint.pformat(d1).splitlines(),\r
- pprint.pformat(d2).splitlines())))\r
- standardMsg = self._truncateMessage(standardMsg, diff)\r
- self.fail(self._formatMessage(msg, standardMsg))\r
-\r
- def assertDictContainsSubset(self, expected, actual, msg=None):\r
- """Checks whether actual is a superset of expected."""\r
- missing = []\r
- mismatched = []\r
- for key, value in expected.iteritems():\r
- if key not in actual:\r
- missing.append(key)\r
- elif value != actual[key]:\r
- mismatched.append('%s, expected: %s, actual: %s' %\r
- (safe_repr(key), safe_repr(value),\r
- safe_repr(actual[key])))\r
-\r
- if not (missing or mismatched):\r
- return\r
-\r
- standardMsg = ''\r
- if missing:\r
- standardMsg = 'Missing: %s' % ','.join(safe_repr(m) for m in\r
- missing)\r
- if mismatched:\r
- if standardMsg:\r
- standardMsg += '; '\r
- standardMsg += 'Mismatched values: %s' % ','.join(mismatched)\r
-\r
- self.fail(self._formatMessage(msg, standardMsg))\r
-\r
- def assertItemsEqual(self, expected_seq, actual_seq, msg=None):\r
- """An unordered sequence specific comparison. It asserts that\r
- actual_seq and expected_seq have the same element counts.\r
- Equivalent to::\r
-\r
- self.assertEqual(Counter(iter(actual_seq)),\r
- Counter(iter(expected_seq)))\r
-\r
- Asserts that each element has the same count in both sequences.\r
- Example:\r
- - [0, 1, 1] and [1, 0, 1] compare equal.\r
- - [0, 0, 1] and [0, 1] compare unequal.\r
- """\r
- first_seq, second_seq = list(actual_seq), list(expected_seq)\r
- with warnings.catch_warnings():\r
- if sys.py3kwarning:\r
- # Silence Py3k warning raised during the sorting\r
- for _msg in ["(code|dict|type) inequality comparisons",\r
- "builtin_function_or_method order comparisons",\r
- "comparing unequal types"]:\r
- warnings.filterwarnings("ignore", _msg, DeprecationWarning)\r
- try:\r
- first = collections.Counter(first_seq)\r
- second = collections.Counter(second_seq)\r
- except TypeError:\r
- # Handle case with unhashable elements\r
- differences = _count_diff_all_purpose(first_seq, second_seq)\r
- else:\r
- if first == second:\r
- return\r
- differences = _count_diff_hashable(first_seq, second_seq)\r
-\r
- if differences:\r
- standardMsg = 'Element counts were not equal:\n'\r
- lines = ['First has %d, Second has %d: %r' % diff for diff in differences]\r
- diffMsg = '\n'.join(lines)\r
- standardMsg = self._truncateMessage(standardMsg, diffMsg)\r
- msg = self._formatMessage(msg, standardMsg)\r
- self.fail(msg)\r
-\r
- def assertMultiLineEqual(self, first, second, msg=None):\r
- """Assert that two multi-line strings are equal."""\r
- self.assertIsInstance(first, basestring,\r
- 'First argument is not a string')\r
- self.assertIsInstance(second, basestring,\r
- 'Second argument is not a string')\r
-\r
- if first != second:\r
- # don't use difflib if the strings are too long\r
- if (len(first) > self._diffThreshold or\r
- len(second) > self._diffThreshold):\r
- self._baseAssertEqual(first, second, msg)\r
- firstlines = first.splitlines(True)\r
- secondlines = second.splitlines(True)\r
- if len(firstlines) == 1 and first.strip('\r\n') == first:\r
- firstlines = [first + '\n']\r
- secondlines = [second + '\n']\r
- standardMsg = '%s != %s' % (safe_repr(first, True),\r
- safe_repr(second, True))\r
- diff = '\n' + ''.join(difflib.ndiff(firstlines, secondlines))\r
- standardMsg = self._truncateMessage(standardMsg, diff)\r
- self.fail(self._formatMessage(msg, standardMsg))\r
-\r
- def assertLess(self, a, b, msg=None):\r
- """Just like self.assertTrue(a < b), but with a nicer default message."""\r
- if not a < b:\r
- standardMsg = '%s not less than %s' % (safe_repr(a), safe_repr(b))\r
- self.fail(self._formatMessage(msg, standardMsg))\r
-\r
- def assertLessEqual(self, a, b, msg=None):\r
- """Just like self.assertTrue(a <= b), but with a nicer default message."""\r
- if not a <= b:\r
- standardMsg = '%s not less than or equal to %s' % (safe_repr(a), safe_repr(b))\r
- self.fail(self._formatMessage(msg, standardMsg))\r
-\r
- def assertGreater(self, a, b, msg=None):\r
- """Just like self.assertTrue(a > b), but with a nicer default message."""\r
- if not a > b:\r
- standardMsg = '%s not greater than %s' % (safe_repr(a), safe_repr(b))\r
- self.fail(self._formatMessage(msg, standardMsg))\r
-\r
- def assertGreaterEqual(self, a, b, msg=None):\r
- """Just like self.assertTrue(a >= b), but with a nicer default message."""\r
- if not a >= b:\r
- standardMsg = '%s not greater than or equal to %s' % (safe_repr(a), safe_repr(b))\r
- self.fail(self._formatMessage(msg, standardMsg))\r
-\r
- def assertIsNone(self, obj, msg=None):\r
- """Same as self.assertTrue(obj is None), with a nicer default message."""\r
- if obj is not None:\r
- standardMsg = '%s is not None' % (safe_repr(obj),)\r
- self.fail(self._formatMessage(msg, standardMsg))\r
-\r
- def assertIsNotNone(self, obj, msg=None):\r
- """Included for symmetry with assertIsNone."""\r
- if obj is None:\r
- standardMsg = 'unexpectedly None'\r
- self.fail(self._formatMessage(msg, standardMsg))\r
-\r
- def assertIsInstance(self, obj, cls, msg=None):\r
- """Same as self.assertTrue(isinstance(obj, cls)), with a nicer\r
- default message."""\r
- if not isinstance(obj, cls):\r
- standardMsg = '%s is not an instance of %r' % (safe_repr(obj), cls)\r
- self.fail(self._formatMessage(msg, standardMsg))\r
-\r
- def assertNotIsInstance(self, obj, cls, msg=None):\r
- """Included for symmetry with assertIsInstance."""\r
- if isinstance(obj, cls):\r
- standardMsg = '%s is an instance of %r' % (safe_repr(obj), cls)\r
- self.fail(self._formatMessage(msg, standardMsg))\r
-\r
- def assertRaisesRegexp(self, expected_exception, expected_regexp,\r
- callable_obj=None, *args, **kwargs):\r
- """Asserts that the message in a raised exception matches a regexp.\r
-\r
- Args:\r
- expected_exception: Exception class expected to be raised.\r
- expected_regexp: Regexp (re pattern object or string) expected\r
- to be found in error message.\r
- callable_obj: Function to be called.\r
- args: Extra args.\r
- kwargs: Extra kwargs.\r
- """\r
- context = _AssertRaisesContext(expected_exception, self, expected_regexp)\r
- if callable_obj is None:\r
- return context\r
- with context:\r
- callable_obj(*args, **kwargs)\r
-\r
- def assertRegexpMatches(self, text, expected_regexp, msg=None):\r
- """Fail the test unless the text matches the regular expression."""\r
- if isinstance(expected_regexp, basestring):\r
- expected_regexp = re.compile(expected_regexp)\r
- if not expected_regexp.search(text):\r
- msg = msg or "Regexp didn't match"\r
- msg = '%s: %r not found in %r' % (msg, expected_regexp.pattern, text)\r
- raise self.failureException(msg)\r
-\r
- def assertNotRegexpMatches(self, text, unexpected_regexp, msg=None):\r
- """Fail the test if the text matches the regular expression."""\r
- if isinstance(unexpected_regexp, basestring):\r
- unexpected_regexp = re.compile(unexpected_regexp)\r
- match = unexpected_regexp.search(text)\r
- if match:\r
- msg = msg or "Regexp matched"\r
- msg = '%s: %r matches %r in %r' % (msg,\r
- text[match.start():match.end()],\r
- unexpected_regexp.pattern,\r
- text)\r
- raise self.failureException(msg)\r
-\r
-\r
-class FunctionTestCase(TestCase):\r
- """A test case that wraps a test function.\r
-\r
- This is useful for slipping pre-existing test functions into the\r
- unittest framework. Optionally, set-up and tidy-up functions can be\r
- supplied. As with TestCase, the tidy-up ('tearDown') function will\r
- always be called if the set-up ('setUp') function ran successfully.\r
- """\r
-\r
- def __init__(self, testFunc, setUp=None, tearDown=None, description=None):\r
- super(FunctionTestCase, self).__init__()\r
- self._setUpFunc = setUp\r
- self._tearDownFunc = tearDown\r
- self._testFunc = testFunc\r
- self._description = description\r
-\r
- def setUp(self):\r
- if self._setUpFunc is not None:\r
- self._setUpFunc()\r
-\r
- def tearDown(self):\r
- if self._tearDownFunc is not None:\r
- self._tearDownFunc()\r
-\r
- def runTest(self):\r
- self._testFunc()\r
-\r
- def id(self):\r
- return self._testFunc.__name__\r
-\r
- def __eq__(self, other):\r
- if not isinstance(other, self.__class__):\r
- return NotImplemented\r
-\r
- return self._setUpFunc == other._setUpFunc and \\r
- self._tearDownFunc == other._tearDownFunc and \\r
- self._testFunc == other._testFunc and \\r
- self._description == other._description\r
-\r
- def __ne__(self, other):\r
- return not self == other\r
-\r
- def __hash__(self):\r
- return hash((type(self), self._setUpFunc, self._tearDownFunc,\r
- self._testFunc, self._description))\r
-\r
- def __str__(self):\r
- return "%s (%s)" % (strclass(self.__class__),\r
- self._testFunc.__name__)\r
-\r
- def __repr__(self):\r
- return "<%s tec=%s>" % (strclass(self.__class__),\r
- self._testFunc)\r
-\r
- def shortDescription(self):\r
- if self._description is not None:\r
- return self._description\r
- doc = self._testFunc.__doc__\r
- return doc and doc.split("\n")[0].strip() or None\r