11-1 City and Country: Write a function that takes two parameters: a city name and a country name. This function returns a string in the format City, Country, such as 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 string you get when you call the preceding function with values like 'santiago' and 'chile' is correct. Run test_cities.py and confirm that test_city_country() passes.
Solution:
#city_functions.py
def city_country(City, Country):
output = City.title() +', ' + Country.title()
return output
#test_cities.py
import unittest
from city_functions import city_country
class CityTestCase(unittest.TestCase):
"""test city_functions.py"""
def test_city_country(self):
formatted_output = city_country('santiago', 'chile')
self.assertEqual(formatted_output, 'Santiago, Chile')
unittest.main()
Output:
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
11-2 Population: Modify the previous function to include the optional parameter population and return a string in the format City, Country - population xxx, such as Santiago, Chile - population 5000000 .
Run test_cities.py and confirm that test_city_country() passes.
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 and confirm that test_city_country_population() passes.
Solution:
#city_functions.py
def city_country(City, Country, population=''):
if population:
output = City.title() +', ' + Country.title() + ' - population ' + str(population)
else:
output = City.title() +', ' + Country.title()
return output
#test_cities.py
import unittest
from city_functions import city_country
class CityTestCase(unittest.TestCase):
"""test city_functions.py"""
def test_city_country(self):
formatted_output = city_country('santiago', 'chile')
self.assertEqual(formatted_output, 'Santiago, Chile')
def test_city_country_population(self):
formatted_output = city_country('santiago', 'chile', 5000000)
self.assertEqual(formatted_output, 'Santiago, Chile - population 5000000')
unittest.main()
Output:
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
11-3 Employee: Write a class called Employee whose method init () accepts first name, last name, and annual salary and stores them all in properties. Write a method called give_raise() that will increase the annual salary by $5000 by default, but can accept other annual salary increases as well.
Write a test case for Employee with two test methods: test_give_default_raise() and test_give_custom_raise(). Use the method setUp() to avoid creating a new instance of employee in each test method. Run this test case and confirm that both tests pass.
Solution:
#employee_class.py
class Employee():
"""build a record for a employee"""
def __init__(self, first_name, last_name, salary):
"""initial a employee"""
self.first_name = first_name
self.last_name = last_name
self.salary = salary
def give_raise(self, money=5000):
"""giving raise"""
self.salary += money
#test_employee_class.py
import unittest
from employee_class import Employee
class TestEmployeeClass(unittest.TestCase):
"""test Employee"""
def setUp(self):
"""set up a test sample"""
self.test_employee = Employee("Tim", "Ashero", 5000)
def test_give_default_raise(self):
"""test default raise"""
self.test_employee.give_raise()
test_salary = self.test_employee.salary
self.assertEqual(test_salary, 10000)
def test_give_custom_raise(self):
"""test custom raise"""
self.test_employee.give_raise(8000)
test_salary = self.test_employee.salary
self.assertEqual(test_salary, 13000)
unittest.main()
Output:
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK