Python programming: From getting started to practicing--"jobs"--11th chapter (test code)

Source: Internet
Author: User

11th Chapter

11-1 Cities and countries: write a function that accepts two parameters: a city name and a country name. This function returns a format of the city, country string, such as Santiago, Chile. Will
This function is stored in a module named city_functions.py.
Create a program named test_cities.py and test the function you just wrote (Don't forget, you need to import the module unittest and the function you want to test). Write a test_city_country () that is named
method to verify that the resulting string is correct using a value similar to ' Santiago ' and ' Chile ' to invoke the aforementioned function. Run Test_cities. PY, confirm the test
Test Test_city_country () passed.
11-2 Population: Modify the previous function to include the third necessary formal parameter population, and return a string formatted as city, country-population xxx,
such as Santiago, Chile-population 5000000. Run test_cities.py to confirm that the test test_city_country () failed.
Modify the above function to set the parameter population to optional. Run test_cities.py again and confirm that the test test_city_country () has passed again.
Write a test called test_city_country_population () and verify that you can use values like ' Santiago ', ' Chile ' and ' population=5000000 ' to invoke
This function. Run test_cities.py again to confirm that the test test_city_country_population () passed.

11-1

Module city_functions.py

def Get_formatted_name (city,country):     " " the function returns a string formatted as city, country " " = City + Country    return Full_name.title ()     

test_cities.py

ImportUnitTest fromCity_functionsImportGet_city_countryclasscitycountrytestcase (unittest. TestCase):" "Test city_functions.py" "    deftest_city_country (self):" "values similar to ' Santiago ' and ' Chile ' are used when invoking the aforementioned function" "Formatted_name=get_city_country ('Santiago','Chile') self.assertequal (Formatted_name,'Santiago Chile')#The dog, the size of this place is wrong.Unittest.main ()

Results:

11-2

Module city_functions.py

def get_city_country (city,country,population):     " " the function returns a string formatted as city, country " "     full_name "+ str    (population)return Full_ Name.title ()

test_cities.py

ImportUnitTest fromCity_functionsImportGet_city_countryclasscitycountrytestcase (unittest. TestCase):" "Test city_functions.py" "    deftest_city_country (self):" "values similar to ' Santiago ' and ' Chile ' are used when invoking the aforementioned function" "Formatted_name=get_city_country ('Santiago','Chile', 5000000) self.assertequal (Formatted_name,'Santiago chile-population 5000000') Unittest.main ()#Defective part of my population here, in fact, is not capitalized, so according to others should be city_functions.py not full_name to define

Results:

11-3 Employee: Write a class called employee, whose method __init__ () accepts first, last, and yearly, and stores them in the attribute. Write a method named Give_raise (), which defaults to
The annual salary increase of $5000, but also can accept other salary increase.
Write a test case for employee that contains two test methods: Test_give_default_raise () and Test_give_custom_raise (). Use the method Setup () to avoid
A new employee instance is created in each test method. Run the test case and confirm that two tests have passed.

11-3

ImportUnitTestclassEmployee ():"""collect information about the employer"""    def __init__(self,first,last,salary):" "storing personal information in attributes" "Self.first=First self.last=Last self.salary=Salarydefgive_raise (self):" "the method of annual salary increase amount" "self.salary= 5000returnself.salaryclassTemployee (unittest. TestCase):" "Create a test employee instance" "    defsetUp (self): Self.first='Wang'Self.last='Shi'self.salary= Int (300)    deftest_give_default_raise (self):"""Test if the employee's salary is the default value"""self.assertequal (self. Employee.salary,300)        deftest_give_custom_raise (self):"""test whether the salary increase is the value of"""self.assertequal (self. Employee.give_raise (),5000) Unittest.main ()

Results:

Python programming: From getting started to practicing--"jobs"--11th chapter (test code)

Related Article

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.