Learn TDD and Python

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.