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

Side by Side 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, 4 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 unified diff | Download patch
« no previous file with comments | « testing_support/__init__.py ('k') | testing_support/git/unittest_helpers.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import collections
6 import inspect
7 import unittest
8
9
10 class AutoStubMixIn(object):
11 """Automatically restores stubbed functions on unit test teardDown.
12
13 It's an extremely lightweight mocking class that doesn't require bookeeping.
14 """
15 _saved = None
16
17 def mock(self, obj, member, mock):
18 self._saved = self._saved or collections.OrderedDict()
19 old_value = self._saved.setdefault(
20 obj, collections.OrderedDict()).setdefault(member, getattr(obj, member))
21 setattr(obj, member, mock)
22 return old_value
23
24 def tearDown(self):
25 """Restore all the mocked members."""
26 if self._saved:
27 for obj, items in self._saved.iteritems():
28 for member, previous_value in items.iteritems():
29 setattr(obj, member, previous_value)
30
31
32 class SimpleMock(object):
33 """Really simple manual class mock."""
34 def __init__(self, unit_test):
35 """Do not call __init__ if you want to use the global call list to detect
36 ordering across different instances.
37 """
38 self.calls = []
39 self.unit_test = unit_test
40 self.assertEqual = unit_test.assertEqual
41
42 def pop_calls(self):
43 """Returns the list of calls up to date.
44
45 Good to do self.assertEqual(expected, mock.pop_calls()).
46 """
47 calls = self.calls
48 self.calls = []
49 return calls
50
51 def check_calls(self, expected):
52 self.assertEqual(expected, self.pop_calls())
53
54 def _register_call(self, *args, **kwargs):
55 """Registers the name of the caller function."""
56 caller_name = kwargs.pop('caller_name', None) or inspect.stack()[1][3]
57 str_args = ', '.join(repr(arg) for arg in args)
58 str_kwargs = ', '.join('%s=%r' % (k, v) for k, v in kwargs.iteritems())
59 self.calls.append('%s(%s)' % (
60 caller_name, ', '.join(filter(None, [str_args, str_kwargs]))))
61
62
63 class TestCase(unittest.TestCase, AutoStubMixIn):
64 """Adds self.mock() and self.has_failed() to a TestCase."""
65 def tearDown(self):
66 AutoStubMixIn.tearDown(self)
67 unittest.TestCase.tearDown(self)
68
69 def has_failed(self):
70 """Returns True if the test has failed."""
71 return not self._resultForDoCleanups.wasSuccessful()
OLDNEW
« 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