TDD用了xUnit的實現來舉例,也同時讓我感受了下動態語言(樂觀語言)的魅力,僅僅帖點代碼來:class TestCase:
def __init__(self,name):
self.name = name
def setUp(pass):
pass
def tearDown(self):
pass
def run(self,result):
result.testStart()
self.setUp()
method = getattr(self,self.name)
method()
self.tearDown
class WasRun(TestCase):
def __init__(self,name):
self.wasRun = None
TestCase.__init__(self,name)
def run(self):
self.testMethod()
def testMethod(self):
self.setUp()
self.wasRun = 1
def setUp(self):
self.wasRun = None
self.wasSetup=1
class TestCaseTest(TestCase):
def testRunning(self):
test1 = WasRun("testMethod")
test2 = WasRun("setUp")
print test2.wasSetUp
assert(test.wasSetUp)
assert(not test.wasRun)
test.setUp()
test.run()
print test2.wasSetUp
assert(test.wasRun)
TestCaseTest("testRunning").run()
>>> class A:
def __init__(self):
self.tests = []
def add(self,test):
self.tests.append(test)
>>> class test:
def __init__(self,name):
self.name = name
def run(self):
print self.name
>>> class MainClass:
def __init__(self):
pass
def runTest(self):
t = test("chenfei")
t1 = test("lfchen")
t2 = test("qixiang")
t3 = test("zhao")
a = A()
a.add(t)
a.add(t1)
a.add(t2)
a.add(t3)
for tt in a.tests:
tt.run()
>>> MainClass().runTest()
感覺還真不錯!