zoukankan      html  css  js  c++  java
  • 函数检测1

    检测城市、国家的输出情况,添加可灵活判断关键字参数的情况,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()
    View Code
  • 相关阅读:
    HDU1058Humble Numbers(DP)
    HDU1285确定比赛名次(拓扑排序)
    HDU2602Bone Collector(DP,0/1背包)
    HDU1869六度分离(最短路floyd)
    HDU3342Legal or Not(拓扑)
    Dijkstra不能得到含有负权边图的单源最短路径
    HDU1069Monkey and Banana(DP)
    HDU1176免费馅饼(DP)
    DAG上的动态规划
    网络分析中数据包结构的定义
  • 原文地址:https://www.cnblogs.com/gzj137070928/p/13825051.html
Copyright © 2011-2022 走看看