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:
parent
d76e3ea5df
commit
1a2c957fbb
@ -117,14 +117,20 @@ class RequestFactoryWithMessages(RequestFactory):
|
|||||||
class TestCase(django_test.TestCase):
|
class TestCase(django_test.TestCase):
|
||||||
"""Base test case class for Horizon with numerous additional features.
|
"""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``
|
* A ``RequestFactory`` class which supports Django's ``contrib.messages``
|
||||||
framework via ``self.factory``.
|
framework via ``self.factory``.
|
||||||
* A ready-to-go request object via ``self.request``.
|
* A ready-to-go request object via ``self.request``.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
use_mox = False
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestCase, self).setUp()
|
super(TestCase, self).setUp()
|
||||||
self.mox = mox.Mox()
|
if self.use_mox:
|
||||||
|
self.mox = mox.Mox()
|
||||||
self._setup_test_data()
|
self._setup_test_data()
|
||||||
self._setup_factory()
|
self._setup_factory()
|
||||||
self._setup_user()
|
self._setup_user()
|
||||||
@ -149,8 +155,9 @@ class TestCase(django_test.TestCase):
|
|||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
super(TestCase, self).tearDown()
|
super(TestCase, self).tearDown()
|
||||||
self.mox.UnsetStubs()
|
if self.use_mox:
|
||||||
self.mox.VerifyAll()
|
self.mox.UnsetStubs()
|
||||||
|
self.mox.VerifyAll()
|
||||||
del os.environ["HORIZON_TEST_RUN"]
|
del os.environ["HORIZON_TEST_RUN"]
|
||||||
|
|
||||||
def set_permissions(self, permissions=None):
|
def set_permissions(self, permissions=None):
|
||||||
|
@ -390,6 +390,9 @@ class DisabledActionsTable(tables.DataTable):
|
|||||||
|
|
||||||
|
|
||||||
class DataTableTests(test.TestCase):
|
class DataTableTests(test.TestCase):
|
||||||
|
|
||||||
|
use_mox = True
|
||||||
|
|
||||||
def test_table_instantiation(self):
|
def test_table_instantiation(self):
|
||||||
"""Tests everything that happens when the table is instantiated."""
|
"""Tests everything that happens when the table is instantiated."""
|
||||||
self.table = MyTable(self.request, TEST_DATA)
|
self.table = MyTable(self.request, TEST_DATA)
|
||||||
|
@ -226,6 +226,7 @@ class TestCase(horizon_helpers.TestCase):
|
|||||||
:class:`~openstack_dashboard.test.test_data.utils.TestData`
|
:class:`~openstack_dashboard.test.test_data.utils.TestData`
|
||||||
for more information.
|
for more information.
|
||||||
* The ``mox`` mocking framework via ``self.mox``.
|
* 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 set of request context data via ``self.context``.
|
||||||
* A ``RequestFactory`` class which supports Django's ``contrib.messages``
|
* A ``RequestFactory`` class which supports Django's ``contrib.messages``
|
||||||
framework via ``self.factory``.
|
framework via ``self.factory``.
|
||||||
@ -238,6 +239,11 @@ class TestCase(horizon_helpers.TestCase):
|
|||||||
# boolean variable to store failures
|
# boolean variable to store failures
|
||||||
missing_mocks = False
|
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):
|
def fake_conn_request(self):
|
||||||
# print a stacktrace to illustrate where the unmocked API call
|
# print a stacktrace to illustrate where the unmocked API call
|
||||||
# is being made from
|
# is being made from
|
||||||
@ -509,6 +515,14 @@ class APITestCase(TestCase):
|
|||||||
api.neutron.neutronclient = self._original_neutronclient
|
api.neutron.neutronclient = self._original_neutronclient
|
||||||
api.cinder.cinderclient = self._original_cinderclient
|
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):
|
def stub_novaclient(self):
|
||||||
if not hasattr(self, "novaclient"):
|
if not hasattr(self, "novaclient"):
|
||||||
self.mox.StubOutWithMock(nova_client, 'Client')
|
self.mox.StubOutWithMock(nova_client, 'Client')
|
||||||
@ -524,9 +538,7 @@ class APITestCase(TestCase):
|
|||||||
return self.novaclient
|
return self.novaclient
|
||||||
|
|
||||||
def stub_cinderclient(self):
|
def stub_cinderclient(self):
|
||||||
LOG.warning("APITestCase has been deprecated for Cinder-related "
|
self._warn_client('cinder', 'S')
|
||||||
"tests and will be removerd in 'S' release. Please "
|
|
||||||
"convert your to use APIMockTestCase instead.")
|
|
||||||
if not hasattr(self, "cinderclient"):
|
if not hasattr(self, "cinderclient"):
|
||||||
self.mox.StubOutWithMock(cinder_client, 'Client')
|
self.mox.StubOutWithMock(cinder_client, 'Client')
|
||||||
self.cinderclient = self.mox.CreateMock(cinder_client.Client)
|
self.cinderclient = self.mox.CreateMock(cinder_client.Client)
|
||||||
@ -546,9 +558,7 @@ class APITestCase(TestCase):
|
|||||||
return self.keystoneclient
|
return self.keystoneclient
|
||||||
|
|
||||||
def stub_glanceclient(self):
|
def stub_glanceclient(self):
|
||||||
LOG.warning("APITestCase has been deprecated for Glance-related "
|
self._warn_client('glance', 'S')
|
||||||
"tests and will be removerd in 'S' release. Please "
|
|
||||||
"convert your to use APIMockTestCase instead.")
|
|
||||||
if not hasattr(self, "glanceclient"):
|
if not hasattr(self, "glanceclient"):
|
||||||
self.mox.StubOutWithMock(glanceclient, 'Client')
|
self.mox.StubOutWithMock(glanceclient, 'Client')
|
||||||
self.glanceclient = self.mox.CreateMock(glanceclient.Client)
|
self.glanceclient = self.mox.CreateMock(glanceclient.Client)
|
||||||
@ -579,6 +589,9 @@ class APITestCase(TestCase):
|
|||||||
|
|
||||||
|
|
||||||
class APIMockTestCase(APITestCase):
|
class APIMockTestCase(APITestCase):
|
||||||
|
|
||||||
|
use_mox = False
|
||||||
|
|
||||||
def stub_cinderclient(self):
|
def stub_cinderclient(self):
|
||||||
if not hasattr(self, "cinderclient"):
|
if not hasattr(self, "cinderclient"):
|
||||||
self.cinderclient = mock.Mock()
|
self.cinderclient = mock.Mock()
|
||||||
|
Loading…
Reference in New Issue
Block a user