From 8b7fed8c37ed9db81e9b71d6e0e6f4950c5c550f Mon Sep 17 00:00:00 2001 From: Johannes Erdfelt Date: Tue, 6 Mar 2012 16:58:03 +0000 Subject: [PATCH] Clear created attributes when tearing down tests unittest will keep each test case around after completion pinning any objects references by attributes on the test case. This can bloat the memory used during a full test suite run to the point where the test suite will require much more memory than running all of the services does. On systems witout lots of memory, test suite failures will occur as test cases cannot fork to execute programs. Clearing out __dict__ of any attributes that don't start with a _ (since some are needed by unittest itself) reduces memory significantly. This does require the super class tearDown to be called last in some cases however. Change-Id: I0e660b6c2a77c5613c5a523ba0a64c3d7f6dd128 --- HACKING.rst | 12 ------------ nova/test.py | 6 ++++++ nova/testing/README.rst | 22 ++++++++++++++++++++++ nova/tests/test_network.py | 8 ++++---- 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/HACKING.rst b/HACKING.rst index 194553432..b7e2564d6 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -181,18 +181,6 @@ For more information on creating unit tests and utilizing the testing infrastructure in OpenStack Nova, please read nova/testing/README.rst. -Unit Tests and assertRaises ---------------------------- -When asserting that a test should raise an exception, test against the -most specific exception possible. An overly broad exception type (like -Exception) can mask errors in the unit test itself. - -Example:: - - self.assertRaises(exception.InstanceNotFound, db.instance_get_by_uuid, - elevated, instance_uuid) - - openstack-common ---------------- diff --git a/nova/test.py b/nova/test.py index b38de5303..87e1ce322 100644 --- a/nova/test.py +++ b/nova/test.py @@ -177,6 +177,12 @@ class TestCase(unittest.TestCase): except Exception: pass + # Delete attributes that don't start with _ so they don't pin + # memory around unnecessarily for the duration of the test + # suite + for key in [k for k in self.__dict__.keys() if k[0] != '_']: + del self.__dict__[key] + def flags(self, **kw): """Override flag variables for a test.""" for k, v in kw.iteritems(): diff --git a/nova/testing/README.rst b/nova/testing/README.rst index 036f1c77d..67fa33d1d 100644 --- a/nova/testing/README.rst +++ b/nova/testing/README.rst @@ -33,6 +33,17 @@ Using Fakes TBD +test.TestCase +------------- +The TestCase class from nova.test (generally imported as test) will +automatically manage self.stubs using the stubout module and self.mox +using the mox module during the setUp step. They will automatically +verify and clean up during the tearDown step. + +If using test.TestCase, calling the super class setUp is required and +calling the super class tearDown is required to be last if tearDown +is overriden. + Writing Functional Tests ------------------------ @@ -42,3 +53,14 @@ Writing Integration Tests ------------------------- TBD + +Tests and assertRaises +---------------------- +When asserting that a test should raise an exception, test against the +most specific exception possible. An overly broad exception type (like +Exception) can mask errors in the unit test itself. + +Example:: + + self.assertRaises(exception.InstanceNotFound, db.instance_get_by_uuid, + elevated, instance_uuid) diff --git a/nova/tests/test_network.py b/nova/tests/test_network.py index ba9dd6e12..7e7ae6b4b 100644 --- a/nova/tests/test_network.py +++ b/nova/tests/test_network.py @@ -135,8 +135,8 @@ class FlatNetworkTestCase(test.TestCase): is_admin=False) def tearDown(self): - super(FlatNetworkTestCase, self).tearDown() self.network.instance_dns_manager.delete_dns_file() + super(FlatNetworkTestCase, self).tearDown() def test_get_instance_nw_info(self): fake_get_instance_nw_info = fake_network.fake_get_instance_nw_info @@ -1277,8 +1277,8 @@ class FloatingIPTestCase(test.TestCase): is_admin=False) def tearDown(self): - super(FloatingIPTestCase, self).tearDown() self.network.floating_dns_manager.delete_dns_file() + super(FloatingIPTestCase, self).tearDown() def test_double_deallocation(self): instance_ref = db.api.instance_create(self.context, @@ -1480,8 +1480,8 @@ class InstanceDNSTestCase(test.TestCase): is_admin=False) def tearDown(self): - super(InstanceDNSTestCase, self).tearDown() self.network.instance_dns_manager.delete_dns_file() + super(InstanceDNSTestCase, self).tearDown() def test_dns_domains_private(self): zone1 = 'testzone' @@ -1525,10 +1525,10 @@ class LdapDNSTestCase(test.TestCase): self.driver.create_domain(domain2) def tearDown(self): - super(LdapDNSTestCase, self).tearDown() self.driver.delete_domain(domain1) self.driver.delete_domain(domain2) sys.modules['ldap'] = self.saved_ldap + super(LdapDNSTestCase, self).tearDown() def test_ldap_dns_domains(self): domains = self.driver.get_domains()