TDD uses the implementation of xunit as an example. It also makes me feel the charm of Dynamic Language (optimistic language ). Code Come: 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 ()
It feels really good!