introduce ``stub_out`` method to base test class

We would like to fully remove mox from the test tree. Even for tests
that don't use mox's validation, many of them are using the symbol
patching with self.stubs.Set. We can do the same thing with the
monkeypatch fixture instead.

This introduces self.stub_out to nova/test.py and an example of what a
stubs => stub_out change would look like.

The teardown function in the converted test was removed at the same
time, as those should no longer be used.

Part of bp:remove-mox

Change-Id: I5afc3a45d9ad9c629072891b52cd5625d28b99fc
This commit is contained in:
Sean Dague 2015-12-17 11:25:37 -05:00
parent 050e691948
commit 5632b8dc55
2 changed files with 22 additions and 14 deletions

View File

@ -256,6 +256,18 @@ class TestCase(testtools.TestCase):
if key != 'id':
del self.__dict__[key]
def stub_out(self, old, new):
"""Replace a function for the duration of the test.
Use the monkey patch fixture to replace a function for the
duration of a test. Useful when you want to provide fake
methods instead of mocks during testing.
This should be used instead of self.stubs.Set (which is based
on mox) going forward.
"""
self.useFixture(fixtures.MonkeyPatch(old, new))
def flags(self, **kw):
"""Override flag variables for a test."""
group = kw.pop('group', None)

View File

@ -16,7 +16,6 @@
from oslo_config import cfg
from oslo_utils import timeutils
from nova import db
from nova.tests.functional.api_sample_tests import api_sample_base
from nova.tests.unit.api.openstack.compute import test_services
@ -44,19 +43,16 @@ class ServicesJsonTest(api_sample_base.ApiSampleTestBaseV21):
def setUp(self):
super(ServicesJsonTest, self).setUp()
self.stubs.Set(db, "service_get_all",
test_services.fake_db_api_service_get_all)
self.stubs.Set(timeutils, "utcnow", test_services.fake_utcnow)
self.stubs.Set(timeutils, "utcnow_ts",
test_services.fake_utcnow_ts)
self.stubs.Set(db, "service_get_by_host_and_binary",
test_services.fake_service_get_by_host_binary)
self.stubs.Set(db, "service_update",
test_services.fake_service_update)
def tearDown(self):
super(ServicesJsonTest, self).tearDown()
timeutils.clear_time_override()
self.stub_out("nova.db.service_get_all",
test_services.fake_db_api_service_get_all)
self.stub_out("oslo_utils.timeutils.utcnow", test_services.fake_utcnow)
self.stub_out("oslo_utils.timeutils.utcnow_ts",
test_services.fake_utcnow_ts)
self.stub_out("nova.db.service_get_by_host_and_binary",
test_services.fake_service_get_by_host_binary)
self.stub_out("nova.db.service_update",
test_services.fake_service_update)
self.addCleanup(timeutils.clear_time_override)
def test_services_list(self):
"""Return a list of all agent builds."""