|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
检测城市、国家的输出情况,添加可灵活判断关键字参数的情况,city_func.py如下:
- def city_country(city, country, population=0):
- """返回一个形如'Santiago, Chile'
- 或者'Santiago, Chile -population 5000000'的字符串"""
- outstr = city.title() + ', ' + country.title()
- if population:
- outstr += ' -population ' + str(population)
- return outstr
- # 检查输出结果
- str1 = city_country('santiago', 'chile')
- str2 = city_country('santiago', 'chile', population=5000000)
- if str1 == 'Santiago, Chile':
- print('第一个结果匹配')
- else:
- print('啊哦,第一个没有匹配')
- if str2 == 'Santiago, Chile -population 5000000':
- print('第二个结果匹配')
- else:
- print('啊哦,第二个没有匹配')
复制代码
函数自己检测结果是通过的,但是用unittest居然说没有做测试,
下面是testcity.py
- import unittest
- from city_func import city_country
- class CityTestCase(unittest.TestCase):
- """测试city_functions.py。"""
- def testcitycountry(self):
- """传入简单的城市和国家是否可行"""
- str1 = city_country('santiago', 'chile')
- self.assertEqual(str1, 'Santiago, Chile')
- def testcitycountry_popu(self):
- """传入的城市和国家、人口是否可行"""
- str2 = city_country('santiago', 'chile', population=5000000)
- self.assertEqual(str2, 'Santiago, Chile -population 5000000')
- unittest.main()
复制代码 |
|