summaryrefslogtreecommitdiffstats
path: root/nova/testing
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2012-09-20 22:44:05 +0000
committerGerrit Code Review <review@openstack.org>2012-09-20 22:44:05 +0000
commit48fc829da2b2ca1106a59517d6df322fe7ba9d38 (patch)
tree1a8365b646c2d867f7342a6c1297395f6a0e39ee /nova/testing
parent1ec8b5d06cf5f5ff0dc0e2a5318ccfc05756d2a4 (diff)
parentc82dba22c24cd7cd4d7ce3629da24644414cb374 (diff)
Merge "Clarify dangerous use of exceptions in unit tests"
Diffstat (limited to 'nova/testing')
-rw-r--r--nova/testing/README.rst30
1 files changed, 25 insertions, 5 deletions
diff --git a/nova/testing/README.rst b/nova/testing/README.rst
index 67fa33d1d..4c341b7ed 100644
--- a/nova/testing/README.rst
+++ b/nova/testing/README.rst
@@ -54,13 +54,33 @@ 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.
+Tests and Exceptions
+--------------------
+A properly written test asserts that particular behavior occurs. This can
+be a success condition or a failure condition, including an exception.
+When asserting that a particular exception is raised, the most specific
+exception possible should be used.
+
+In particular, testing for Exception being raised is almost always a
+mistake since it will match (almost) every exception, even those
+unrelated to the exception intended to be tested.
+
+This applies to catching exceptions manually with a try/except block,
+or using assertRaises().
Example::
self.assertRaises(exception.InstanceNotFound, db.instance_get_by_uuid,
elevated, instance_uuid)
+
+If a stubbed function/method needs a generic exception for testing
+purposes, test.TestingException is available.
+
+Example::
+
+ def stubbed_method(self):
+ raise test.TestingException()
+ self.stubs.Set(cls, 'inner_method', stubbed_method)
+
+ obj = cls()
+ self.assertRaises(test.TestingException, obj.outer_method)