Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import unittest
- import itertools
- import inspect
- from functools import wraps
- from collections import OrderedDict
- test_classes = {}
- def ordered(func):
- """ From http://metapython.blogspot.com.br/2011/01/ordered-unit-tests.html
- """
- if isinstance(func, type):
- return _ordered_class(func)
- cls_dict = inspect.currentframe().f_locals
- if id(cls_dict) not in test_classes:
- test_classes[id(cls_dict)] = {"last_added": -1, "last_executed": -1}
- tgt_dict = test_classes[id(cls_dict)]
- tgt_dict[tgt_dict["last_added"] + 1] = func
- tgt_dict["last_added"] += 1
- func._execute_group = tgt_dict
- @wraps(func)
- def test_dispatcher(self, *args, **kw):
- tgt_dict = func._execute_group
- to_execute = tgt_dict["last_executed"] + 1
- if to_execute > tgt_dict["last_added"]:
- to_execute = 0
- tgt_dict["last_executed"] = to_execute
- return tgt_dict[to_execute](self, *args, **kw)
- return test_dispatcher
- class OrderedClass(type):
- @classmethod
- def __prepare__(metacls, name, bases): # No keywords in this case
- return OrderedDict()
- def __new__(cls, classname, bases, classdict):
- counter = itertools.count()
- for name, attr in classdict.items():
- if not hasattr(attr, '_oc_order'):
- try:
- attr._oc_order = next(counter)
- except AttributeError:
- continue
- return type.__new__(cls, classname, bases, dict(classdict))
- class OrderedTestCaseClass(OrderedClass):
- def __new__(cls, classname, bases, classdict):
- # note that classdict is an ordered dict
- for (name, attr) in classdict.items():
- if inspect.isfunction(attr):
- classdict[name] = ordered(attr)
- return OrderedClass.__new__(cls, classname, bases, classdict)
- class OrderedTestCase(unittest.TestCase, metaclass=OrderedTestCaseClass):
- pass
- #ordered = lambda x:x #switch off
- if __name__ == "__main__":
- class A(unittest.TestCase):
- exec_order = []
- def test_5(self):
- A.exec_order.append(0)
- print(0)
- def test_bla(self):
- A.exec_order.append(1)
- print(1)
- def test_a(self):
- A.exec_order.append(2)
- print(2)
- def test_0(self):
- A.exec_order.append(3)
- print(3)
- def test_2(self):
- A.exec_order.append(4)
- print(4)
- self.assertNotEqual(A.exec_order, [0,1,2,3,4])
- class B(unittest.TestCase):
- exec_order = []
- @ordered
- def test_5(self):
- B.exec_order.append(0)
- print(0)
- @ordered
- def test_bla(self):
- B.exec_order.append(1)
- print(1)
- @ordered
- def test_a(self):
- B.exec_order.append(2)
- print(2)
- @ordered
- def test_0(self):
- B.exec_order.append(3)
- print(3)
- @ordered
- def test_2(self):
- B.exec_order.append(4)
- print(4)
- self.assertEqual(B.exec_order, [0,1,2,3,4])
- class C(unittest.TestCase):
- exec_order = []
- @ordered
- def test_5(self):
- C.exec_order.append(5)
- print(5)
- @ordered
- def test_bla(self):
- C.exec_order.append(6)
- print(6)
- @ordered
- def test_a(self):
- C.exec_order.append(7)
- print(7)
- @ordered
- def test_0(self):
- C.exec_order.append(8)
- print(8)
- @ordered
- def test_2(self):
- C.exec_order.append(9)
- print(9)
- self.assertEqual(C.exec_order, [5,6,7,8,9])
- class D(OrderedTestCase):
- exec_order = []
- def test_10(self):
- D.exec_order.append(10)
- print(10)
- def test_blabla(self):
- D.exec_order.append(11)
- print(11)
- def test_ohyeah(self):
- D.exec_order.append(12)
- print(12)
- def test_00(self):
- D.exec_order.append(13)
- print(13)
- def test_123(self):
- D.exec_order.append(14)
- print(14)
- self.assertEqual(D.exec_order, [10, 11, 12, 13, 14])
- unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment