Matthew Treinish | 8708621 | 2013-10-28 20:21:54 +0000 | [diff] [blame] | 1 | # 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 Pipes | 8fe5392 | 2014-01-14 20:08:16 -0500 | [diff] [blame] | 15 | import mock |
Matthew Treinish | 8990017 | 2014-03-03 20:45:02 -0500 | [diff] [blame] | 16 | from oslotest import base |
| 17 | from oslotest import moxstubout |
Matthew Treinish | 8708621 | 2013-10-28 20:21:54 +0000 | [diff] [blame] | 18 | |
| 19 | |
Matthew Treinish | 8990017 | 2014-03-03 20:45:02 -0500 | [diff] [blame] | 20 | class TestCase(base.BaseTestCase): |
Matthew Treinish | 8708621 | 2013-10-28 20:21:54 +0000 | [diff] [blame] | 21 | |
| 22 | def setUp(self): |
| 23 | super(TestCase, self).setUp() |
Matthew Treinish | dde9d0a | 2013-10-31 18:26:52 -0400 | [diff] [blame] | 24 | mox_fixture = self.useFixture(moxstubout.MoxStubout()) |
Matthew Treinish | 8708621 | 2013-10-28 20:21:54 +0000 | [diff] [blame] | 25 | self.mox = mox_fixture.mox |
| 26 | self.stubs = mox_fixture.stubs |
Jay Pipes | 8fe5392 | 2014-01-14 20:08:16 -0500 | [diff] [blame] | 27 | |
| 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 |