Create a file called test_cities.py that tests the function you just wrote (remember that you need to import unittest and the function you want to test). Write a method called test_city_country() to verify that calling your function with values such as 'santiago' and 'chile' results in the correct string. Run test_cities.py, and make sure test_city_country() passes.
Codes:
#city_function.py
def cities(City, Country):
return City+', '+Country
#test_cities.py
import unittest
from city_function import cities
class CitiesTestCase(unittest.TestCase):
def test_city_country(self):
format_city = cities('Guangzhou', 'China')
self.assertEqual(format_city, 'Guangzhou, China')
unittest.main()
Results:
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
11-2. Population: Modify your function so it requires a third parameter, population. It should now return a single string of the form City, Country – population xxx, such as Santiago, Chile – population 5000000. Run test_cities.py again. Make sure test_city_country() fails this time.
Modify the function so the population parameter is optional. Run test_cities.py again, and make sure test_city_country() passes again.
Write a second test called test_city_country_population() that verifies you can call your function with the values 'santiago', 'chile', and 'population=5000000'. Run test_cities.py again, and make sure this new test
passes.
Codes:
#city_function.py
def cities(City, Country, population = 0):
if population:
return City+', '+Country+' - population '+str(population)
else:
return City+', '+Country
#test_cities.py
import unittest
from city_function import cities
class CitiesTestCase(unittest.TestCase):
def test_city_country(self):
format_city = cities('Guangzhou', 'China')
self.assertEqual(format_city, 'Guangzhou, China')
def test_city_country_population(self):
format_city = cities('Guangzhou', 'China', 14000000)
self.assertEqual(format_city, 'Guangzhou, China - population 14000000')
unittest.main()
Results:
E
======================================================================
ERROR: test_city_country (__main__.CitiesTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_cities.py", line 7, in test_city_country
format_city = cities('Guangzhou', 'China')
TypeError: cities() missing 1 required positional argument: 'population'
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
..
----------------------------------------------------------------------
Ran 2 tests in 0.011s
OK
11-3. Employee: Write a class called Employee. The __init__() method should take in a first name, a last name, and an annual salary, and store each of these as attributes. Write a method called give_raise() that adds $5000 to the annual salary by default but also accepts a different raise amount.
Write a test case for Employee. Write two test methods, test_give_default_raise() and test_give_custom_raise(). Use the setUp() method so you don’t have to create a new employee instance in each test method. Run your test case, and make sure both tests pass.
Codes:
#employee_class.py
class Employee():
def __init__(self,first,last,salary):
self.first = first
self.last = last
self.salary = salary
def give_raise(self, r = 5000):
self.salary += r
#test_employee.py
import unittest
from employee_class import Employee
class EmployeeTestCase(unittest.TestCase):
def setUp(self):
self.my_employee = Employee('Zachary','Lei',10000)
def test_give_default_raise(self):
self.my_employee.give_raise()
self.assertEqual(self.my_employee.salary, 15000)
def test_give_custom_raise(self):
self.my_employee.give_raise(10000)
self.assertEqual(self.my_employee.salary, 20000)
unittest.main()
Results:
..
----------------------------------------------------------------------
Ran 2 tests in 0.013s
OK