[Freeipa-devel] [PATCH] 0024 Fix expected exception checking in tests

Rob Crittenden rcritten at redhat.com
Thu Mar 15 21:36:42 UTC 2012


Petr Viktorin wrote:
> Can you spot the bug in this test code?
>
> try:
> do_invalid_operation()
> except ExpectedError:
> pass
>
>
> Our test suite had several of those.
> Nose provides nice tools, `raises` (a decorator) and `assert_raises` (a
> context manager) that make checking expected exceptions a lot easier and
> less error-prone. This patch makes our tests use them.
>
> If you didn't catch it, the error is that the test will pass when no
> exception is raised. Some of our tests handled that by adding an `else:
> assert False`, or an `assert False` at the end of the try block.
> For consistency, the patch switches these correct ones to
> raises/assert_raises as well.
>
> I've also uncovered and fixed a few test bugs that were hidden by this.
>


test_1a_automountmap_add_indirect() was failing, checking for the wrong 
exception.

I also suggest using @raises for clarity in another spot. Here are my 
suggested changes:

diff --git a/tests/test_xmlrpc/test_automount_plugin.py 
b/tests/test_xmlrpc/test
_automount_plugin.py
index 6abc44f..dedd234 100644
--- a/tests/test_xmlrpc/test_automount_plugin.py
+++ b/tests/test_xmlrpc/test_automount_plugin.py
@@ -79,12 +79,12 @@ class test_automount(XMLRPC_test):
          assert res
          assert_attr_equal(res, 'automountkey', self.keyname)

+    @raises(errors.DuplicateEntry)
      def test_4_automountkey_add(self):
          """
          Test adding a duplicate key using `xmlrpc.automountkey_add` 
method.
          """
-        with assert_raises(errors.DuplicateEntry):
-            api.Command['automountkey_add'](self.locname, self.mapname, 
**self.
key_kw)
+        res = api.Command['automountkey_add'](self.locname, 
self.mapname, **sel
f.key_kw)

      def test_5_automountmap_show(self):
          """
@@ -247,12 +247,12 @@ class test_automount_indirect(XMLRPC_test):
          assert res
          assert_attr_equal(res, 'automountmapname', self.mapname)

+    @raises(errors.DuplicateEntry)
      def test_1a_automountmap_add_indirect(self):
          """
          Test adding a duplicate indirect map.
          """
-        with assert_raises(errors.NotFound):
-            api.Command['automountmap_add_indirect'](self.locname, 
self.mapname
, **self.map_kw)['result']
+        api.Command['automountmap_add_indirect'](self.locname, 
self.mapname, **
self.map_kw)['result']

      def test_2_automountmap_show(self):
          """





More information about the Freeipa-devel mailing list