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
This commit is contained in:
Akihiro Motoki 2017-12-30 15:15:56 +09:00
parent d76e3ea5df
commit 1a2c957fbb
3 changed files with 33 additions and 10 deletions

View File

@ -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):

View File

@ -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)

View File

@ -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()