鱼C论坛

 找回密码
 立即注册
查看: 2138|回复: 0

[技术交流] 函数检测1

[复制链接]
发表于 2020-10-16 10:27:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
检测城市、国家的输出情况,添加可灵活判断关键字参数的情况,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. str1 = city_country('santiago', 'chile')
  10. str2 = city_country('santiago', 'chile', population=5000000)
  11. if str1 == 'Santiago, Chile':
  12.     print('第一个结果匹配')
  13. else:
  14.     print('啊哦,第一个没有匹配')
  15. if str2 == 'Santiago, Chile -population 5000000':
  16.     print('第二个结果匹配')
  17. else:
  18.     print('啊哦,第二个没有匹配')
复制代码


函数自己检测结果是通过的,但是用unittest居然说没有做测试,

下面是testcity.py
  1. import unittest
  2. from city_func import city_country


  3. class CityTestCase(unittest.TestCase):
  4.     """测试city_functions.py。"""
  5.     def testcitycountry(self):
  6.         """传入简单的城市和国家是否可行"""
  7.         str1 = city_country('santiago', 'chile')
  8.         self.assertEqual(str1, 'Santiago, Chile')

  9.     def testcitycountry_popu(self):
  10.         """传入的城市和国家、人口是否可行"""
  11.         str2 = city_country('santiago', 'chile', population=5000000)
  12.         self.assertEqual(str2, 'Santiago, Chile -population 5000000')


  13. unittest.main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-28 10:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表