20211107
11-1
city_functions.py
def get_city_country(city, country):
return city.title() + ', ' + country.title()
test_cities.py
import unittest
from city_functions import get_city_country
class CityTestCase(unittest.TestCase):
def test_get_city_country(self):
formatted_city = get_city_country('changsha', 'china')
self.assertEqual(formatted_city, 'Changsha, China')
unittest.main()
11-2
city_functions.py
def get_city_country(city, country, population=''):
if population:
return city.title() + ', ' + country.title() + \
' -population ' + population
else:
return city.title() + ', ' + country.title()
test_cities.py
import unittest
from city_functions import get_city_country
class CityTestCase(unittest.TestCase):
def test_get_city_country(self):
formatted_city = get_city_country('changsha', 'china')
self.assertEqual(formatted_city, 'Changsha, China')
def test_get_city_country_population(self):
formatted_city = get_city_country('changsha', 'china', '12966836')
self.assertEqual(formatted_city, \
'Changsha, China -population 12966836')
unittest.main()
11-3
empoy.py
class Employee():
def __init__(self, givenname, surname, salary):
self.givenname = givenname
self.surname = surname
self.salary = salary
def give_raise(self, add_salary = 5000):
self.salary += add_salary
return self.salary #---------------don't missed
testemploy.py
import unittest
from employ import Employee
class TestEmployee(unittest.TestCase):
def setUp(self):
self.emp1 = Employee('g', 's', 1000000)
def test_give_default_raise(self):
raised_salary = self.emp1.give_raise()
self.assertEqual(raised_salary, 1005000)
def test_give_custom_raise(self):
raised_salary = self.emp1.give_raise(10000)
self.assertEqual(raised_salary, 1010000)
unittest.main()
Python编程:从入门到实践 练习答案 Chapter11
最新推荐文章于 2024-03-08 20:31:31 发布