检测城市、国家的输出情况,添加可灵活判断关键字参数的情况,city_func.py如下:
1 def city_country(city, country, population=0): 2 """返回一个形如'Santiago, Chile' 3 或者'Santiago, Chile -population 5000000'的字符串""" 4 outstr = city.title() + ', ' + country.title() 5 if population: 6 outstr += ' -population ' + str(population) 7 return outstr 8 9 # 检查输出结果 10 str1 = city_country('santiago', 'chile') 11 str2 = city_country('santiago', 'chile', population=5000000) 12 if str1 == 'Santiago, Chile': 13 print('第一个结果匹配') 14 else: 15 print('啊哦,第一个没有匹配') 16 if str2 == 'Santiago, Chile -population 5000000': 17 print('第二个结果匹配') 18 else: 19 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()