Python Unit Test UnitTest

Source: Internet
Author: User

August 9, 2018 release, follow up comments and other text description ———————————— first need to test the code name_function.py is a simple function, get two names before and after the combination
def get_formatted_name (first,last,middle="):    if  Middle:         ' ' ' ' +    lastelse:        ' + last     return full_name.title ()

names.py lets the user enter a name and outputs
 fromName_functionImportGet_formatted_namePrint("Enter")  whileTrue:firstname= Input ("Enter First:")    ifFirstName = ='Q':        Print("Exit")         BreakLastName= Input ("Enter Second:")    ifLastName = ='Q':        Print("Exit")         BreakFullName=get_formatted_name (firstname,lastname)Print('Full Name:', fullname)
Run results
1 Enter 2 Enter First:zhang 3 Enter Second:san 4 Full Name:  Zhang San5Enter First:q6 exit

If you want to add a middle name to the first and last of the name_function.py function, in order to verify that the previous function works correctly, you can verify that Get_formatted_name (First,last) is tested after each modification, It's too much trouble. Python provides a way to automatically test the output of a function, unittest.
ImportUnitTest fromName_functionImportGet_formatted_nameclassnametestcase (unittest. TestCase):defTest_first_last_name (self): Formatted_name= Get_formatted_name ('Janis','Joplin') self.assertequal (Formatted_name,'Janis Joplin')     defTest_first_last_middle_name (self): Formatted_name= Get_formatted_name ('Wolfgang','Mozart','Amadeus') self.assertequal (Formatted_name,'Wolfgang Amadeus Mozart') Unittest.main ()
The above code, the import module and the function to be tested, by calling the function, asserting the result with the returned formatted_name, and finally Unittest.main () executes the above results. Run results
.. ---------------------------------------------------------------------- in 0.000s OK

Write another piece of code survey.py
classAnoymoussurvey ():def __init__(self,question): Self.question=question Self.responses= []     defshow_question (self):Print(self.question)defStore_response (self,new_response): Self.responses.append (new_response)defshow_results (self):Print("Survey Results:")         forResponseinchself.responses:Print("- ", response)

language_servey.py
 fromSurveyImportAnoymoussurvey Question="What language you first learn"My_survey=Anoymoussurvey (question) my_survey.show_question ()Print("enter Q to quit")  whileTrue:response= Input ("Language:")    ifResponse = ='Q':         Breakmy_survey.store_response (response)Print("--")Print("Thank your input") my_survey.show_results ()

Enter the information by the user, then save the list, then print it and then use unittest,test_survey.py
 fromSurveyImportAnoymoussurveyImportUnitTestclassTestanonymoussurvey (unittest. TestCase):defTest_store_single_response (self): question="What language you first learn1"My_survey=Anoymoussurvey (question) my_survey.show_question () My_survey.store_response ('中文版') Self.assertin ('中文版', my_survey.responses)defTest_store_three_response (self): question="What language you first learn2"My_survey=Anoymoussurvey (question) my_survey.show_question () Responses= ['中文版','Spanish','Chinese']         forResponseinchresponses:my_survey.store_response (response) Self.assertin (response,my_survey.responses) if __name__=='__main__': Unittest.main ()

Or use the Setup method
ImportUnitTest fromSurveyImportAnoymoussurveyclassTestanonymoussurvey (unittest. TestCase):defSetUp (self): question="What language you first learn"Self.my_survey=Anoymoussurvey (question) self.responses= ['中文版','Spanish','Chinese']     defTest_store_single_response (self): Self.my_survey.store_response (Self.responses[0]) Self.assertin (SELF.R esponses[0],self.my_survey.responses)defTest_store_three_response (self): forResponseinchself.responses:self.my_survey.store_response (response) forResponseinchself.responses:self.assertIn (response,self.my_survey.response)if __name__=='__main__': Unittest.main ()

Python Unit Test UnitTest

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.