标签:一个 ini last zhang __init__ code unittest lse format
def get_formatted_name(first,last,middle=‘‘): if middle: full_name = first + ‘ ‘ + middle + ‘ ‘ + last else: full_name = first + ‘ ‘ + last return full_name.title()
from name_function import get_formatted_name print("enter") while True: firstname = input("enter first: ") if firstname == ‘q‘: print("exit") break lastname = input("enter second: ") if lastname == ‘q‘: print("exit") break fullname = get_formatted_name(firstname,lastname) print(‘full name: ‘,fullname)
1 enter 2 enter first: zhang 3 enter second: san 4 full name: Zhang San 5 enter first: q 6 exit
import unittest from name_function import get_formatted_name class NameTestCase(unittest.TestCase): def test_first_last_name(self): formatted_name = get_formatted_name(‘janis‘,‘joplin‘) self.assertEqual(formatted_name,‘Janis Joplin‘) def test_first_last_middle_name(self): formatted_name = get_formatted_name(‘wolfgang‘,‘mozart‘,‘amadeus‘) self.assertEqual(formatted_name,‘Wolfgang Amadeus Mozart‘) unittest.main()
.. ---------------------------------------------------------------------- Ran 2 tests in 0.000s OK
class AnoymousSurvey(): def __init__(self,question): self.question = question self.responses = [] def show_question(self): print(self.question) def store_response(self,new_response): self.responses.append(new_response) def show_results(self): print("Survey results: ") for response in self.responses: print("- ",response)
from survey import AnoymousSurvey question = "what language you first learn" my_survey = AnoymousSurvey(question) my_survey.show_question() print("enter q to quit") while True: response = input("language: ") if response == ‘q‘: break my_survey.store_response(response) print("--") print("thank your input") my_survey.show_results()
from survey import AnoymousSurvey import unittest class TestAnonymousSurvey(unittest.TestCase): def test_store_single_response(self): question = "what language you first learn1" my_survey = AnoymousSurvey(question) my_survey.show_question() my_survey.store_response(‘English‘) self.assertIn(‘English‘,my_survey.responses) def test_store_three_response(self): question = "what language you first learn2" my_survey = AnoymousSurvey(question) my_survey.show_question() responses = [‘English‘,‘Spanish‘,‘Chinese‘] for response in responses: my_survey.store_response(response) self.assertIn(response,my_survey.responses) if __name__ == ‘__main__‘: unittest.main()
import unittest from survey import AnoymousSurvey class TestAnonymousSurvey(unittest.TestCase): def SetUp(self): question = "what language you first learn" self.my_survey = AnoymousSurvey(question) self.responses = [‘English‘,‘Spanish‘,‘Chinese‘] def test_store_single_response(self): self.my_survey.store_response(self.responses[0]) self.assertIn(self.responses[0],self.my_survey.responses) def test_store_three_response(self): for response in self.responses: self.my_survey.store_response(response) for response in self.responses: self.assertIn(response,self.my_survey.response) if __name__ == ‘__main__‘: unittest.main()
标签:一个 ini last zhang __init__ code unittest lse format
原文地址:https://www.cnblogs.com/zxylock/p/9448176.html