blob: fe9268e76640aa63c09db63394686482bb4d5268 [file] [log] [blame]
Matthew Treinish87086212013-10-28 20:21:54 +00001# Copyright 2013 IBM Corp.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
Jay Pipes8fe53922014-01-14 20:08:16 -050015import mock
Matthew Treinish89900172014-03-03 20:45:02 -050016from oslotest import base
17from oslotest import moxstubout
Matthew Treinish87086212013-10-28 20:21:54 +000018
19
Matthew Treinish89900172014-03-03 20:45:02 -050020class TestCase(base.BaseTestCase):
Matthew Treinish87086212013-10-28 20:21:54 +000021
22 def setUp(self):
23 super(TestCase, self).setUp()
Matthew Treinishdde9d0a2013-10-31 18:26:52 -040024 mox_fixture = self.useFixture(moxstubout.MoxStubout())
Matthew Treinish87086212013-10-28 20:21:54 +000025 self.mox = mox_fixture.mox
26 self.stubs = mox_fixture.stubs
Jay Pipes8fe53922014-01-14 20:08:16 -050027
28 def patch(self, target, **kwargs):
Ken'ichi Ohmichic864f592015-11-19 08:45:06 +000029 """Returns a started `mock.patch` object for the supplied target.
Jay Pipes8fe53922014-01-14 20:08:16 -050030
31 The caller may then call the returned patcher to create a mock object.
32
33 The caller does not need to call stop() on the returned
34 patcher object, as this method automatically adds a cleanup
35 to the test class to stop the patcher.
36
37 :param target: String module.class or module.object expression to patch
38 :param **kwargs: Passed as-is to `mock.patch`. See mock documentation
39 for details.
40 """
41 p = mock.patch(target, **kwargs)
42 m = p.start()
43 self.addCleanup(p.stop)
44 return m