学习TDD同时学到Python
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
![](/Images/OutliningIndicators/None.gif)
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
![](/Images/OutliningIndicators/None.gif)
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()
![](/Images/OutliningIndicators/None.gif)
>>> class A:
def __init__(self):
self.tests = []
def add(self,test):
self.tests.append(test)
![](/Images/OutliningIndicators/None.gif)
>>> class test:
def __init__(self,name):
self.name = name
def run(self):
print self.name
![](/Images/OutliningIndicators/None.gif)
>>> 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()
![](/Images/OutliningIndicators/None.gif)
>>> MainClass().runTest()
感觉还真不错!
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
感觉还真不错!
posted on 2006-09-10 21:10 flyingchen 阅读(570) 评论(0) 编辑 收藏 举报