ravishi

Ordered Unittests in Python 3

Mar 26th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.38 KB | None | 0 0
  1. import unittest
  2. import itertools
  3. import inspect
  4. from functools import wraps
  5. from collections import OrderedDict
  6.  
  7.  
  8. test_classes = {}
  9.  
  10.  
  11. def ordered(func):
  12.     """ From http://metapython.blogspot.com.br/2011/01/ordered-unit-tests.html
  13.    """
  14.     if isinstance(func, type):
  15.         return _ordered_class(func)
  16.     cls_dict = inspect.currentframe().f_locals
  17.     if id(cls_dict) not in test_classes:
  18.         test_classes[id(cls_dict)] = {"last_added": -1, "last_executed": -1}
  19.     tgt_dict = test_classes[id(cls_dict)]
  20.     tgt_dict[tgt_dict["last_added"] + 1] = func
  21.     tgt_dict["last_added"] += 1
  22.     func._execute_group = tgt_dict
  23.     @wraps(func)
  24.     def test_dispatcher(self, *args, **kw):
  25.         tgt_dict = func._execute_group
  26.         to_execute = tgt_dict["last_executed"] + 1
  27.         if to_execute > tgt_dict["last_added"]:
  28.             to_execute = 0
  29.         tgt_dict["last_executed"] = to_execute
  30.         return tgt_dict[to_execute](self, *args, **kw)
  31.     return test_dispatcher
  32.  
  33.  
  34. class OrderedClass(type):
  35.     @classmethod
  36.     def __prepare__(metacls, name, bases): # No keywords in this case
  37.         return OrderedDict()
  38.  
  39.     def __new__(cls, classname, bases, classdict):
  40.         counter = itertools.count()
  41.         for name, attr in classdict.items():
  42.             if not hasattr(attr, '_oc_order'):
  43.                 try:
  44.                     attr._oc_order = next(counter)
  45.                 except AttributeError:
  46.                     continue
  47.         return type.__new__(cls, classname, bases, dict(classdict))
  48.  
  49.  
  50. class OrderedTestCaseClass(OrderedClass):
  51.     def __new__(cls, classname, bases, classdict):
  52.         # note that classdict is an ordered dict
  53.         for (name, attr) in classdict.items():
  54.             if inspect.isfunction(attr):
  55.                 classdict[name] = ordered(attr)
  56.         return OrderedClass.__new__(cls, classname, bases, classdict)
  57.  
  58.  
  59. class OrderedTestCase(unittest.TestCase, metaclass=OrderedTestCaseClass):
  60.     pass
  61.  
  62. #ordered = lambda x:x #switch off
  63.  
  64. if __name__ == "__main__":
  65.     class A(unittest.TestCase):
  66.         exec_order = []
  67.         def test_5(self):
  68.             A.exec_order.append(0)
  69.             print(0)
  70.         def test_bla(self):
  71.             A.exec_order.append(1)
  72.             print(1)
  73.         def test_a(self):
  74.             A.exec_order.append(2)
  75.             print(2)
  76.         def test_0(self):
  77.             A.exec_order.append(3)
  78.             print(3)
  79.         def test_2(self):
  80.             A.exec_order.append(4)
  81.             print(4)
  82.             self.assertNotEqual(A.exec_order, [0,1,2,3,4])
  83.  
  84.     class B(unittest.TestCase):
  85.         exec_order = []
  86.         @ordered
  87.         def test_5(self):
  88.             B.exec_order.append(0)
  89.             print(0)
  90.         @ordered
  91.         def test_bla(self):
  92.             B.exec_order.append(1)
  93.             print(1)
  94.         @ordered
  95.         def test_a(self):
  96.             B.exec_order.append(2)
  97.             print(2)
  98.         @ordered
  99.         def test_0(self):
  100.             B.exec_order.append(3)
  101.             print(3)
  102.         @ordered
  103.         def test_2(self):
  104.             B.exec_order.append(4)
  105.             print(4)
  106.             self.assertEqual(B.exec_order, [0,1,2,3,4])
  107.  
  108.  
  109.     class C(unittest.TestCase):
  110.         exec_order = []
  111.         @ordered
  112.         def test_5(self):
  113.             C.exec_order.append(5)
  114.             print(5)
  115.         @ordered
  116.         def test_bla(self):
  117.             C.exec_order.append(6)
  118.             print(6)
  119.         @ordered
  120.         def test_a(self):
  121.             C.exec_order.append(7)
  122.             print(7)
  123.         @ordered
  124.         def test_0(self):
  125.             C.exec_order.append(8)
  126.             print(8)
  127.         @ordered
  128.         def test_2(self):
  129.             C.exec_order.append(9)
  130.             print(9)
  131.             self.assertEqual(C.exec_order, [5,6,7,8,9])
  132.  
  133.     class D(OrderedTestCase):
  134.         exec_order = []
  135.         def test_10(self):
  136.             D.exec_order.append(10)
  137.             print(10)
  138.         def test_blabla(self):
  139.             D.exec_order.append(11)
  140.             print(11)
  141.         def test_ohyeah(self):
  142.             D.exec_order.append(12)
  143.             print(12)
  144.         def test_00(self):
  145.             D.exec_order.append(13)
  146.             print(13)
  147.         def test_123(self):
  148.             D.exec_order.append(14)
  149.             print(14)
  150.             self.assertEqual(D.exec_order, [10, 11, 12, 13, 14])
  151.  
  152.     unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment