[Twisted-Python] Thoughts about testing

James Y Knight foom at fuhm.net
Thu Oct 27 04:02:02 EDT 2005


On Oct 27, 2005, at 2:10 AM, glyph at divmod.com wrote:

>
>
> On Wed, 26 Oct 2005 21:11:06 -0400, James Y Knight <foom at fuhm.net>  
> wrote:
>
>> On Oct 26, 2005, at 8:38 PM, Jonathan Lange wrote:
>>
>
>
>> Possibly "fixable" in trial with a horrible hack of looking up  
>> source  line numbers of classes/methods and sorting according to  
>> that, but  that seems rather...horrible.
>>
>
> Why?  It's just one attribute - no magical side effects or  
> anything.  It's documented, and this only affects sort order.   
> Seems like a pretty reasonable change to me.

Well, here's the 5 minute solution. It works...no points for style  
though.

James

Index: runner.py
===================================================================
--- runner.py   (revision 14859)
+++ runner.py   (working copy)
@@ -296,6 +296,34 @@
          return thing.__name__
      return thing.id()
+def sourceOrder(thing):
+    if isinstance(thing, pyunit.TestCase):
+        # ?!?!
+        thing = thing._parents[0]
+    if hasattr(thing, 'im_func'):
+        thing = thing.im_func
+    if hasattr(thing, 'func_code'):
+        thing = thing.func_code
+    if hasattr(thing, 'co_firstlineno'):
+        return thing.co_firstlineno
+
+    if isinstance(thing, (types.ClassType, type)):
+        so = None
+        for x in vars(thing).itervalues():
+            try:
+                newso = sourceOrder(x)
+            except TypeError:
+                # Not a sourceorderable
+                pass
+            else:
+                if so is not None:
+                    so = min(so, newso)
+                else:
+                    so = newso
+        if so is None:
+            return 0
+        return so
+    raise TypeError("Unknown test object type: %s %s %s" % (thing,  
type(thing), vars(thing)))
def isTestCase(obj):
      try:
@@ -316,6 +344,7 @@
          self.suiteFactory = TestSuite
          self.classSuiteFactory = ClassSuite
          self.sorter = name
+        self.testSorter = sourceOrder
          self._importErrors = []
      def _findTestClasses(self, module):
@@ -324,7 +353,7 @@
          for name, val in inspect.getmembers(module):
              if isTestCase(val):
                  classes.append(val)
-        return dsu(classes, self.sorter)
+        return dsu(classes, self.testSorter)
      def _findTestModules(self, package):
          modGlob = os.path.join(os.path.dirname(package.__file__),  
self.moduleGlob)
@@ -371,7 +400,7 @@
          factory = self.classSuiteFactory
          names = reflect.prefixedMethodNames(klass, self.methodPrefix)
          tests = dsu([ klass(self.methodPrefix+name) for name in  
names ],
-                    self.sorter)
+                    self.testSorter)
          suite = factory(klass)
          suite.addTests(tests)
          return NamedSuite(klass.__name__, suite)





More information about the Twisted-Python mailing list