blob: 27eb2c4849ab0336c8995d5ab4577aa94a1ac711 [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):
29 """
30 Returns a started `mock.patch` object for the supplied target.
31
32 The caller may then call the returned patcher to create a mock object.
33
34 The caller does not need to call stop() on the returned
35 patcher object, as this method automatically adds a cleanup
36 to the test class to stop the patcher.
37
38 :param target: String module.class or module.object expression to patch
39 :param **kwargs: Passed as-is to `mock.patch`. See mock documentation
40 for details.
41 """
42 p = mock.patch(target, **kwargs)
43 m = p.start()
44 self.addCleanup(p.stop)
45 return m