Commits

Olemis Lang  committed 5dc8138

BH Multiproduct #115 : Adding logging capabiities in assertRaises for multi-product test cases

  • Participants
  • Parent commits 45b961d
  • Branches t115_bep3_product_env

Comments (0)

Files changed (1)

File t115/t115_r1431447_product_envs_bep3_p1.diff

 
 diff -r 9b04a5a324be bloodhound_multiproduct/multiproduct/env.py
 --- a/bloodhound_multiproduct/multiproduct/env.py	Mon Jan 14 14:05:28 2013 +0000
-+++ b/bloodhound_multiproduct/multiproduct/env.py	Mon Jan 14 19:26:46 2013 -0500
++++ b/bloodhound_multiproduct/multiproduct/env.py	Tue Jan 15 09:25:35 2013 -0500
 @@ -46,7 +46,7 @@
      def __init__(self, path, create=False, options=[]):
          super(Environment, self).__init__(path, create=create, options=options)
      def upgrade(self, backup=False, backup_dest=None):
 diff -r 9b04a5a324be bloodhound_multiproduct/tests/env.py
 --- a/bloodhound_multiproduct/tests/env.py	Mon Jan 14 14:05:28 2013 +0000
-+++ b/bloodhound_multiproduct/tests/env.py	Mon Jan 14 19:26:46 2013 -0500
-@@ -20,9 +20,17 @@
++++ b/bloodhound_multiproduct/tests/env.py	Tue Jan 15 09:25:35 2013 -0500
+@@ -18,11 +18,22 @@
  
+ """Tests for Apache(TM) Bloodhound's product environments"""
+ 
++from inspect import stack
  import os.path
  import shutil
 +import sys
  
 +if sys.version_info < (2, 7):
 +    import unittest2 as unittest
++    from unittest2.case import _AssertRaisesContext
 +else:
 +    import unittest
++    from unittest.case import _AssertRaisesContext
 +
 +from trac.config import Option
 +from trac.env import Environment
  from trac.test import EnvironmentStub
  from trac.tests.env import EnvironmentTestCase
  
-@@ -100,6 +108,14 @@
+@@ -39,6 +50,55 @@
+     to create product-specific subclasses.
+     """
+ 
++    # unittest2 extensions
++
++    exceptFailureMessage = None
++
++    class _AssertRaisesLoggingContext(_AssertRaisesContext):
++        """Add logging capabilities to assertRaises
++        """
++        def __init__(self, expected, test_case, expected_regexp=None):
++            _AssertRaisesContext.__init__(
++                    self, expected, test_case, expected_regexp)
++            self.test_case = test_case
++
++        @staticmethod
++        def _tb_locals(tb):
++            if tb is None:
++                # Inspect interpreter stack two levels up
++                ns = stack()[2][0].f_locals.copy()
++            else:
++                # Traceback already in context
++                ns = tb.tb_frame.f_locals.copy()
++            ns.pop('__builtins__', None)
++            return ns
++
++        def __exit__(self, exc_type, exc_value, tb):
++            try:
++                return _AssertRaisesContext.__exit__(self, 
++                    exc_type, exc_value, tb)
++            except self.failureException, exc:
++                msg = self.test_case.exceptFailureMessage 
++                if msg is not None:
++                    standardMsg = str(exc)
++                    msg = msg % self._tb_locals(tb)
++                    msg = self.test_case._formatMessage(msg, standardMsg)
++                    raise self.failureException(msg)
++                else:
++                    raise
++            finally:
++                # Clear message placeholder
++                self.test_case.exceptFailureMessage = None
++
++    def assertRaises(self, excClass, callableObj=None, *args, **kwargs):
++        """Adds logging capabilities on top of unittest2 implementation.
++        """
++        if callableObj is None:
++            return self._AssertRaisesLoggingContext(excClass, self)
++        else:
++            return unittest.TestCase.assertRaises(
++                    self, excClass, callableObj=None, *args, **kwargs)
++
+     # Product data
+ 
+     default_product = 'tp1'
+@@ -100,6 +160,14 @@
              # table remains but database version is deleted
              pass
  
  class ProductEnvTestCase(EnvironmentTestCase, MultiproductTestCase):
      r"""Test cases for Trac environments rewritten for product environments
      """
-@@ -131,9 +147,98 @@
+@@ -131,9 +199,99 @@
  
          EnvironmentTestCase.tearDown(self)
  
 +                    setattr(self.env.__class__, attrnm, 
 +                        property_mock(attrnm, self.env))
 +
++                    self.exceptFailureMessage = 'Property %(attrnm)s'
 +                    with self.assertRaises(AttrSuccess) as cm_test_attr:
 +                        getattr(self.product_env, attrnm)
 +                else:
  
 diff -r 9b04a5a324be bloodhound_theme/setup.py
 --- a/bloodhound_theme/setup.py	Mon Jan 14 14:05:28 2013 +0000
-+++ b/bloodhound_theme/setup.py	Mon Jan 14 19:26:46 2013 -0500
++++ b/bloodhound_theme/setup.py	Tue Jan 15 09:25:35 2013 -0500
 @@ -19,6 +19,7 @@
  #  under the License.