PATCH: Create resource classes from test classes

Mark Mitchell mark at codesourcery.com
Thu Sep 1 23:21:43 UTC 2005


Several people have suggested that it should be possible to reuse a
Test class as if it were a Resource.  In particular, the resource
SetUp method would be the same as the test Run method; the resource
fails if the test fails.  For example, this allows you to use
CompilerTest as a resource to build a program, and then have a bunch
of tests that depend on the existence of the program (to run it, for
example).  I'll be checking in some follow-on changes to CompilerTest
in the near future to implement some of that.

Here's the basic infrastructure.

Committed.

--
Mark Mitchell
CodeSourcery, LLC
mark at codesourcery.com

2005-09-01  Mark Mitchell  <mark at codesourcery.com>

	* qm/test/resource.py (resource_adapter): New function.

Index: qm/test/resource.py
===================================================================
RCS file: /home/qm/Repository/qm/qm/test/resource.py,v
retrieving revision 1.10
diff -c -5 -p -r1.10 resource.py
*** qm/test/resource.py	17 Apr 2003 22:48:48 -0000	1.10
--- qm/test/resource.py	1 Sep 2005 23:18:57 -0000
***************
*** 17,29 ****
  # Imports
  ########################################################################
  
  import qm
  import qm.test.runnable
  
  ########################################################################
! # classes
  ########################################################################
  
  class Resource(qm.test.runnable.Runnable):
      """A 'Resource' sets up before a test and cleans up afterwards.
  
--- 17,30 ----
  # Imports
  ########################################################################
  
  import qm
  import qm.test.runnable
+ from   qm.test.test import Test
  
  ########################################################################
! # Classes
  ########################################################################
  
  class Resource(qm.test.runnable.Runnable):
      """A 'Resource' sets up before a test and cleans up afterwards.
  
*************** class Resource(qm.test.runnable.Runnable
*** 103,107 ****
--- 104,147 ----
          This method should not return a value.
  
          Derived classes may override this method."""
  
          pass
+ 
+ 
+ 
+ ########################################################################
+ # Functions
+ ########################################################################
+ 
+ def resource_adapter(test_class):
+     """Return a resource class based on 'test_class'.
+ 
+     'test_class' -- A 'Test' class.  This argument is not the name of
+     a 'Test' class; it is the actuall class object itself.
+ 
+     returns -- A 'Resource' class.  The 'Resource' class 'SetUp'
+     method is equivalent to the 'Test' class 'Run' method.  The
+     'CleanUp' action is empty.
+ 
+     If this function is called more than once with the same
+     'test_class', it will return a new class each time."""
+ 
+     assert test_class.kind == Test.kind
+ 
+     # Construct a new class.  By listing 'Resource' first, we ensure
+     # that the 'kind' field for the new class is 'Resource.kind'.
+     class ResourceAdapter(Resource, test_class):
+         """A 'ResourceAdapter' is a resource based on a 'Test' class.
+ 
+         The 'SetUp' method for this class behaves just like the 'Run'
+         method for the test class on which this resource is based.""" 
+ 
+         def SetUp(self, context, result):
+ 
+             # To set up the resource, just run the underlying test class.
+             self.Run(context, result)
+ 
+ 
+ 
+     # Return the new class.        
+     return ResourceAdapter



More information about the qmtest mailing list