Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(467)

Unified Diff: testing_support/auto_stub.py

Issue 424223002: Add auto_stub mocking library. (Closed) Base URL: https://chromium.googlesource.com/infra/testing/testing_support@master
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « testing_support/__init__.py ('k') | testing_support/git/unittest_helpers.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: testing_support/auto_stub.py
diff --git a/testing_support/auto_stub.py b/testing_support/auto_stub.py
new file mode 100644
index 0000000000000000000000000000000000000000..91a67af62d26013c41a8c128ee8d17e415d7f9e7
--- /dev/null
+++ b/testing_support/auto_stub.py
@@ -0,0 +1,71 @@
+# 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.
+
+import collections
+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):
+ self._saved = self._saved or collections.OrderedDict()
+ old_value = self._saved.setdefault(
+ obj, collections.OrderedDict()).setdefault(member, getattr(obj, member))
+ 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
+ self.assertEqual = unit_test.assertEqual
+
+ def pop_calls(self):
+ """Returns the list of calls up to date.
+
+ Good to do self.assertEqual(expected, mock.pop_calls()).
+ """
+ calls = self.calls
+ self.calls = []
+ return calls
+
+ def check_calls(self, expected):
+ self.assertEqual(expected, self.pop_calls())
+
+ 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):
+ """Adds self.mock() and self.has_failed() to a TestCase."""
+ 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()
« no previous file with comments | « testing_support/__init__.py ('k') | testing_support/git/unittest_helpers.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698