--PYTHON practice test code programming from entry to practice

City and State 11-1: Write a function that takes two parameters: the name of a city and a country name. This function returns a string format City, Country, such as Santiago, Chile. This function is stored in a module called the city_functions.py.
  Create a program called test_cities.py, and just write a function test (do not forget to import unittest module and function to be tested). Write named test_city_country () method, while using similar verification 'santiago' and 'chile' such value to call the function, resulting string is correct. Run test_cities.py, validation testing test_city_country () passed.

city_functions.py


def city_country(city, country):    return city.title() + ', ' + country.title()
[url=][/url]
test_cities.py


import unittestfrom city_functions import city_countryclass CityFunctionsTestCase(unittest.TestCase):    """测试city_functions.py"""    def test_city_country(self):        """测试函数能够返回这样的字符串:Santiago, Chile"""        c_c = city_country('santiago', 'chile')        self.assertEqual(c_c, 'Santiago, Chile')if __name__ == "__main__":    unittest.main()[url=][/url]

  Run test_cities.py:

Ran 1 test in 0.002sOK

  Confirmed test test_city_country () passed.
Population Number 11-2: front modification function, essential to include the third parameter Population, and returns a format City, Country - population xxx string, such as Santiago, Chile - population 5000000. Run test_cities.py, validation testing test_city_country () does not pass.
  Modifying the above function, the parameter is set to optional population. Test_cities.py run again, validation testing test_city_country () passed.
  And then write a name test_city_country_population () test, may be used to verify the value such as 'santiago', 'chile' and 'population = 5000000' call this function. Test_cities.py run again, validation testing test_city_country_population () passed.

city_functions.py


def city_country(city, country, population):    return city.title() + '. ' + country.title() + '- population ' + str(population)

  Modification function -> Run test_cities.py -> confirmation test test_city_country () failed.

[url=]

 

[/url]
city_functions.py


def city_country(city, country, population=''):    if population:        return city.title() + ', ' + country.title() + ' - population ' + str(population)    else:        return city.title() + ', ' + country.title()[url=]

 

[/url]

  Modify the function -> Run test_cities.py -> confirm test test_city_country () passed.

    def test_city_country_population (self): "" "test function can return a string like: Santiago, Chile - population 5000000" "" c_c_p = city_country ( 'santiago', 'chile', population = 5000000) self.assertEqual (c_c_p, ' santiago, Chile - population 5000000 ')

  Add test_cities.py in a named test_city_country_population () Test -> Run test_cities.py again -> confirm test test_city_country_population () passed.
More technical information may concern: gzitcast

Guess you like

Origin www.cnblogs.com/heimaguangzhou/p/11790382.html