Week 6 Homework #Advanced Programming Techniques

Chapter 11


11-1 Cities and countries

Write a function that takes two parameters: a city name and a country name. This function returns a string in the format City, Country, eg Santiago, Chile . Store this function in a module called city_functions.py.
Create a program called test_cities.py to 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 method called test_city_country() and verify that the strings you get when you call the preceding functions with values ​​like 'santiago' and 'chile' are correct. Run test_cities.py and confirm that test_city_country() passes.

city_functions.py

def get_formatted_city(city, country):
    """Generate a neatly formatted full name."""
    full_name = city + ', ' + country
    return full_name.title()

test_cities.py

import unittest
from city_functions import get_formatted_city

class NamesTestCase(unittest.TestCase):
    def test_formatted_city_name(self):
        formatted_name = get_formatted_city('santiago', 'chile')
        self.assertEqual(formatted_name, 'Santiago, Chile')

unittest.main()

operation result

.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
[Finished in 0.2s]



11-2 Population

Modify the preceding function to include the third mandatory parameter population and return a string in the format City, Country - population xxx, such as Santiago, Chile - population 5000000 . Run test_cities.py and verify that the test test_city_country() fails. Modify the above function to make the parameter population optional. Run test_cities.py again and confirm that test_city_country() passes again. Write another test called test_city_country_population() and verify that the function can be called with values ​​like 'santiago', 'chile', and 'population=5000000'. Run test_cities.py again to confirm that test_city_country_population() passes.

city_functions.py

def get_formatted_city(city, country, population=''):
    """Generate a neatly formatted full name."""
    if population:
        full_name = city + ', ' + country + ' - population ' + str(population)
    else:
        full_name = city + ', ' + country
    return full_name.title()

test_cities.py

import unittest
from city_functions import get_formatted_city
class NamesTestCase(unittest.TestCase):
    def test_formatted_city_name_1(self):
        formatted_name = get_formatted_city('santiago', 'chile')
        self.assertEqual(formatted_name, 'Santiago, Chile')

    def test_formatted_city_name_2(self):
        formatted_name = get_formatted_city('santiago', 'chile', 5000000)
        self.assertEqual(formatted_name, 'Santiago, Chile - Population 5000000')

unittest.main()

operation result

..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK
[Finished in 0.2s]



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324523844&siteId=291194637