From 1a2c957fbb5fa4d98a7875d0b8690406d9b45140 Mon Sep 17 00:00:00 2001 From: Akihiro Motoki Date: Sat, 30 Dec 2017 15:15:56 +0900 Subject: [PATCH] Provide a way to disable mox completely This commit introduces 'use_mox' attribute to TestCase class. If this is set to False, TestCase does not setup mox. In addition, a common method to emit the deprecation warning was introduced in APITestCase. Part of blueprint mock-framework-in-unit-tests Change-Id: I1e3df7ea2a867b8a7aa69523bed2beaab9663dc0 --- horizon/test/helpers.py | 15 +++++++++++---- horizon/test/unit/tables/test_tables.py | 3 +++ openstack_dashboard/test/helpers.py | 25 +++++++++++++++++++------ 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/horizon/test/helpers.py b/horizon/test/helpers.py index 6fa0b6bf47..49c39c2f1f 100644 --- a/horizon/test/helpers.py +++ b/horizon/test/helpers.py @@ -117,14 +117,20 @@ class RequestFactoryWithMessages(RequestFactory): class TestCase(django_test.TestCase): """Base test case class for Horizon with numerous additional features. - * The ``mox`` mocking framework via ``self.mox``. + * The ``mox`` mocking framework via ``self.mox`` + if ``use_mox`` attribute is set to True. + Note that ``use_mox`` defaults to False. * A ``RequestFactory`` class which supports Django's ``contrib.messages`` framework via ``self.factory``. * A ready-to-go request object via ``self.request``. """ + + use_mox = False + def setUp(self): super(TestCase, self).setUp() - self.mox = mox.Mox() + if self.use_mox: + self.mox = mox.Mox() self._setup_test_data() self._setup_factory() self._setup_user() @@ -149,8 +155,9 @@ class TestCase(django_test.TestCase): def tearDown(self): super(TestCase, self).tearDown() - self.mox.UnsetStubs() - self.mox.VerifyAll() + if self.use_mox: + self.mox.UnsetStubs() + self.mox.VerifyAll() del os.environ["HORIZON_TEST_RUN"] def set_permissions(self, permissions=None): diff --git a/horizon/test/unit/tables/test_tables.py b/horizon/test/unit/tables/test_tables.py index 77ed4a4549..0d556836d5 100644 --- a/horizon/test/unit/tables/test_tables.py +++ b/horizon/test/unit/tables/test_tables.py @@ -390,6 +390,9 @@ class DisabledActionsTable(tables.DataTable): class DataTableTests(test.TestCase): + + use_mox = True + def test_table_instantiation(self): """Tests everything that happens when the table is instantiated.""" self.table = MyTable(self.request, TEST_DATA) diff --git a/openstack_dashboard/test/helpers.py b/openstack_dashboard/test/helpers.py index 896477474f..a6c52ec397 100644 --- a/openstack_dashboard/test/helpers.py +++ b/openstack_dashboard/test/helpers.py @@ -226,6 +226,7 @@ class TestCase(horizon_helpers.TestCase): :class:`~openstack_dashboard.test.test_data.utils.TestData` for more information. * The ``mox`` mocking framework via ``self.mox``. + if ``use_mox`` attribute is set to True. * A set of request context data via ``self.context``. * A ``RequestFactory`` class which supports Django's ``contrib.messages`` framework via ``self.factory``. @@ -238,6 +239,11 @@ class TestCase(horizon_helpers.TestCase): # boolean variable to store failures missing_mocks = False + # Most openstack_dashbaord tests depends on mox, + # we mark use_mox to True by default. + # Eventually we can drop this when mock migration has good progress. + use_mox = True + def fake_conn_request(self): # print a stacktrace to illustrate where the unmocked API call # is being made from @@ -509,6 +515,14 @@ class APITestCase(TestCase): api.neutron.neutronclient = self._original_neutronclient api.cinder.cinderclient = self._original_cinderclient + def _warn_client(self, service, removal_version): + LOG.warning( + "APITestCase has been deprecated for %(service)s-related " + "tests and will be removerd in '%(removal_version)s' release. " + "Please convert your to use APIMockTestCase instead.", + {'service': service, 'removal_version': removal_version} + ) + def stub_novaclient(self): if not hasattr(self, "novaclient"): self.mox.StubOutWithMock(nova_client, 'Client') @@ -524,9 +538,7 @@ class APITestCase(TestCase): return self.novaclient def stub_cinderclient(self): - LOG.warning("APITestCase has been deprecated for Cinder-related " - "tests and will be removerd in 'S' release. Please " - "convert your to use APIMockTestCase instead.") + self._warn_client('cinder', 'S') if not hasattr(self, "cinderclient"): self.mox.StubOutWithMock(cinder_client, 'Client') self.cinderclient = self.mox.CreateMock(cinder_client.Client) @@ -546,9 +558,7 @@ class APITestCase(TestCase): return self.keystoneclient def stub_glanceclient(self): - LOG.warning("APITestCase has been deprecated for Glance-related " - "tests and will be removerd in 'S' release. Please " - "convert your to use APIMockTestCase instead.") + self._warn_client('glance', 'S') if not hasattr(self, "glanceclient"): self.mox.StubOutWithMock(glanceclient, 'Client') self.glanceclient = self.mox.CreateMock(glanceclient.Client) @@ -579,6 +589,9 @@ class APITestCase(TestCase): class APIMockTestCase(APITestCase): + + use_mox = False + def stub_cinderclient(self): if not hasattr(self, "cinderclient"): self.cinderclient = mock.Mock()