"""练习11-1 城市和国家
1.编写一个函数,它接受两个形参:一个城市名和一个国家名。
这个函数返回一个格式为City, Country的字符串,如Santiago, Chile。
将这个函数存储在一个名为city _functions.py的模块中。
2.创建一个名为test_cities.py的程序,对刚编写的函数进行测试(别忘了,你需要导入模块unittest以及要测试的函数)。
编写一个名为test_city_country()的方法,核实使用类似于'santiago'和'chile'这样的值来调用前述函数时,得到的字符串是正确的。
3.运行test_cities.py,确认测试test_city_country()通过了。"""
# 1.编写一个函数将这个函数存储在一个名为city _functions.py的模块中(注意:这跟下面的代码不在一个模块)
# def city_country(city, country): # 定义函数并设置(城市,国家)两个形参
# """返回一个类似于'Bei Jin, China'的字符串。"""
# return f"{city.title()}, {country.title()}" # 返回两个参数并首字母大写
# 2.创建一个名为test_cities.py的程序对刚编写的函数进行测试
from city_functions import city_country # 导入1
import unittest # 导入测试模块
class CitiesTestCase(unittest.TestCase): # 创建一个类(继承unittest.TestCase类)
"""测试city_functions.py。"""
def test_city_country(self): # 编写测试方法
"""传入简单的城市和国家可行吗?"""
santiago_chile = city_country('bei jin', 'china') # 你要测试的函数
self.assertEqual(santiago_chile,'Bei Jin, China') # 如果结果是你预测的
# 3.运行test_cities.py,确认测试test_city_country()通过了
unittest.main()
"""练习11-2 人口数量
1.修改前面的函数,使其包含第三个必不可少的形参——population,
并返回一个格式为City, Country – population xxx的字符串,
如Santiago, Chile – population 5000000。
运行test_cities.py,确认测试test_city_country()未通过。
2.修改上述函数,将形参population设置为可选的。
再次运行test_cities.py,确认测试test_city_country()又通过了。
3.再编写一个名为test_city_country_population()的测试,
核实可以使用类似于'santiago'、'chile'和'population=5000000'这样的值来调用这个函数。
4.再次运行test_cities.py,确认测试test_city_country_population()通过了。
修改后的city_functions.py,包含必不可少的形参population:
"""
# 1.修改前面的函数,确认测试test_city_country()未通过(注意:这跟下面的代码不在一个模块)
# def city_country(city, country, population): # (城市,国家,人口)
# """返回一个形如'Santiago, Chile - population 5000000'的字符串。"""
# output_string = city.title() + ", " + country.title() # 城市,国家
# output_string += ' - population ' + str(population) # 人口
# return output_string # 返回
# 2.修改前面的函数未让测试通过
# def city_country(city, country, population=0):
# """返回一个表示城市和国家的字符串。"""
# output_string = city.title() + ", " + country.title()
# if population:
# output_string += ' - population ' + str(population)
# return output_string
import unittest
from city_functions import city_country
class CitiesTestCase(unittest.TestCase):
"""测试'city_functions.py'。"""
def test_city_country(self):
"""传入简单的城市和国家可行吗?"""
santiago_chile = city_country('santiago', 'chile') # 你要测试的函数
self.assertEqual(santiago_chile, 'Santiago, Chile') # 如果结果是你预测的
# 3.再编写一个名为test_city_country_population()的测试
# def test_city_country_population(self):
# """可向形参population传递值吗?"""
# santiago_chile = city_country('santiago', 'chile', population=5000000) # 你要测试的函数
# self.assertEqual(santiago_chile, 'Santiago, Chile - population 5000000') # 如果结果是你预测的
unittest.main()
"""练习11-3 雇员
1.编写一个名为Employee的类,其方法__init__()接受名、姓和年薪,并将它们都存储在属性中。
编写一个名为give_raise()的方法,它默认将年薪增加5000美元,但也能够接受其他的年薪增加量。
2.为Employee编写一个测试用例,其中包含两个测试方法:test_give_default_raise()和test_give_custom_raise()。
使用方法setUp(),以免在每个测试方法中都创建新的雇员实例。运行这个测试用例,确认两个测试都通过了。
"""
# 1.编写一个名为Employee的类,其方法__init__()接受名、姓和年薪,并将它们都存储在属性中。(注意:这跟下面的代码不在一个模块)
# class Employee():
# """一个表示雇员的类。"""
# class Employee():
# """一个表示雇员的类。"""
# def __init__(self, name, salary): # (名字,工资)
# """初始化雇员。"""
# self.name = name.title()
# self.salary = salary
# def give_raise(self, amount=5000): # 默认工资增长5000
# """给雇员加薪。"""
# self.salary += amount # 工资+5000元
# 2.为Employee编写一个测试用例,其中包含两个测试方法
import unittest
from city_functions import Employee # 导入上面的模块中的类
class TestEmployee(unittest.TestCase):
"""测试模块employee。"""
def setUp(self):
"""创建一个Employee实例,以便在测试中使用。"""
self.eric = Employee('chen si yuan', 65000) # 创建对象(名字,工资)
def test_give_default_raise(self):
"""测试使用默认的年薪增加量是否可行。"""
self.eric.give_raise() # 默认增长工资
self.assertEqual(self.eric.salary, 70000) # 预测结果为70000
def test_give_custom_raise(self):
"""测试自定义年薪增加量是否可行。"""
self.eric.give_raise(10000) # 自定义工资增长10000
self.assertEqual(self.eric.salary, 75000) # # 预测结果为75000
unittest.main()