auto_stub.py 2.25 KB
Newer Older
1 2 3 4
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

5 6 7
__version__ = '1.0'

import collections
8 9 10 11 12 13 14 15 16 17 18 19
import inspect
import unittest


class AutoStubMixIn(object):
  """Automatically restores stubbed functions on unit test teardDown.

  It's an extremely lightweight mocking class that doesn't require bookeeping.
  """
  _saved = None

  def mock(self, obj, member, mock):
20
    self._saved = self._saved or collections.OrderedDict()
21
    old_value = self._saved.setdefault(
22
        obj, collections.OrderedDict()).setdefault(member, getattr(obj, member))
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
    setattr(obj, member, mock)
    return old_value

  def tearDown(self):
    """Restore all the mocked members."""
    if self._saved:
      for obj, items in self._saved.iteritems():
        for member, previous_value in items.iteritems():
          setattr(obj, member, previous_value)


class SimpleMock(object):
  """Really simple manual class mock."""
  def __init__(self, unit_test):
    """Do not call __init__ if you want to use the global call list to detect
    ordering across different instances.
    """
    self.calls = []
    self.unit_test = unit_test
42
    self.assertEqual = unit_test.assertEqual
43 44 45 46

  def pop_calls(self):
    """Returns the list of calls up to date.

47
    Good to do self.assertEqual(expected, mock.pop_calls()).
48 49 50 51 52 53
    """
    calls = self.calls
    self.calls = []
    return calls

  def check_calls(self, expected):
54
    self.assertEqual(expected, self.pop_calls())
55 56 57 58 59 60 61 62 63 64 65

  def _register_call(self, *args, **kwargs):
    """Registers the name of the caller function."""
    caller_name = kwargs.pop('caller_name', None) or inspect.stack()[1][3]
    str_args = ', '.join(repr(arg) for arg in args)
    str_kwargs = ', '.join('%s=%r' % (k, v) for k, v in kwargs.iteritems())
    self.calls.append('%s(%s)' % (
        caller_name, ', '.join(filter(None, [str_args, str_kwargs]))))


class TestCase(unittest.TestCase, AutoStubMixIn):
66
  """Adds self.mock() and self.has_failed() to a TestCase."""
67 68 69 70 71 72 73
  def tearDown(self):
    AutoStubMixIn.tearDown(self)
    unittest.TestCase.tearDown(self)

  def has_failed(self):
    """Returns True if the test has failed."""
    return not self._resultForDoCleanups.wasSuccessful()