document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. # -*- coding: utf-8 -*-
  2. import unittest
  3.  
  4. from inspect import currentframe
  5. from functools import wraps
  6.  
  7. test_classes = {}
  8.  
  9. def ordered(func):
  10.     cls_dict = currentframe(1).f_locals
  11.     if id(cls_dict) not in test_classes:
  12.         test_classes[id(cls_dict)] = {"last_added": -1, "last_executed": -1}
  13.     tgt_dict = test_classes[id(cls_dict)]
  14.     tgt_dict[tgt_dict["last_added"] + 1] = func
  15.     tgt_dict["last_added"] += 1
  16.     func._execute_group = tgt_dict
  17.     @wraps(func)
  18.     def test_dispatcher(self, *args, **kw):
  19.         tgt_dict = func._execute_group
  20.         to_execute = tgt_dict["last_executed"] + 1
  21.         if to_execute > tgt_dict["last_added"]:
  22.             to_execute = 0
  23.         tgt_dict["last_executed"] = to_execute
  24.         return tgt_dict[to_execute](self, *args, **kw)
  25.     return test_dispatcher
  26.  
  27. #ordered = lambda x:x #switch off
  28.  
  29. if __name__ == "__main__":
  30.     class A(unittest.TestCase):
  31.         exec_order = []
  32.         def test_5(self):
  33.             A.exec_order.append(0)
  34.             print 0,
  35.         def test_bla(self):
  36.             A.exec_order.append(1)
  37.             print 1,
  38.         def test_a(self):
  39.             A.exec_order.append(2)
  40.             print 2,
  41.         def test_0(self):
  42.             A.exec_order.append(3)
  43.             print 3,
  44.         def test_2(self):
  45.             A.exec_order.append(4)
  46.             print 4,
  47.             self.assertNotEquals(A.exec_order, [0,1,2,3,4])
  48.  
  49.     class B(unittest.TestCase):
  50.         exec_order = []
  51.         @ordered
  52.         def test_5(self):
  53.             B.exec_order.append(0)
  54.             print 0,
  55.         @ordered
  56.         def test_bla(self):
  57.             B.exec_order.append(1)
  58.             print 1,
  59.         @ordered
  60.         def test_a(self):
  61.             B.exec_order.append(2)
  62.             print 2,
  63.         @ordered
  64.         def test_0(self):
  65.             B.exec_order.append(3)
  66.             print 3,
  67.         @ordered
  68.         def test_2(self):
  69.             B.exec_order.append(4)
  70.             print 4,
  71.             self.assertEquals(B.exec_order, [0,1,2,3,4])
  72.            
  73.    
  74.     class C(unittest.TestCase):
  75.         exec_order = []
  76.         @ordered
  77.         def test_5(self):
  78.             C.exec_order.append(5)
  79.             print 0,
  80.         @ordered
  81.         def test_bla(self):
  82.             C.exec_order.append(6)
  83.             print 1,
  84.         @ordered
  85.         def test_a(self):
  86.             C.exec_order.append(7)
  87.             print 2,
  88.         @ordered
  89.         def test_0(self):
  90.             C.exec_order.append(8)
  91.             print 3,
  92.         @ordered
  93.         def test_2(self):
  94.             C.exec_order.append(9)
  95.             print 4,
  96.             self.assertEquals(C.exec_order, [5,6,7,8,9])
  97.            
  98.     unittest.main()
');