From 5632b8dc552be924001e3bde0127f9c9fd21f038 Mon Sep 17 00:00:00 2001 From: Sean Dague Date: Thu, 17 Dec 2015 11:25:37 -0500 Subject: [PATCH] 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 --- nova/test.py | 12 ++++++++++ .../api_sample_tests/test_services.py | 24 ++++++++----------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/nova/test.py b/nova/test.py index 3dbfe764872f..cffaed044e9b 100644 --- a/nova/test.py +++ b/nova/test.py @@ -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) diff --git a/nova/tests/functional/api_sample_tests/test_services.py b/nova/tests/functional/api_sample_tests/test_services.py index 15128204c3cd..a12e04b9b5a8 100644 --- a/nova/tests/functional/api_sample_tests/test_services.py +++ b/nova/tests/functional/api_sample_tests/test_services.py @@ -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."""