# -*- coding: utf-8 -*-
import unittest
from inspect import currentframe
from functools import wraps
test_classes = {}
def ordered(func):
cls_dict = currentframe(1).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
#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.assertNotEquals(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.assertEquals(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 0,
@ordered
def test_bla(self):
C.exec_order.append(6)
print 1,
@ordered
def test_a(self):
C.exec_order.append(7)
print 2,
@ordered
def test_0(self):
C.exec_order.append(8)
print 3,
@ordered
def test_2(self):
C.exec_order.append(9)
print 4,
self.assertEquals(C.exec_order, [5,6,7,8,9])
unittest.main()