• Python编程从入门到实践第十一章-测试代码


    11-1

    def get_formatted_name(city,country):
        '''函数返回一个格式为City, Country 的字符串'''
        full_name = city + country
        return full_name.title()
    import unittest
    from city_functions import get_formatted_name
    class CityCountryTestCase(unittest.TestCase):
        def test_city_country(self):
            formatted_name =get_formatted_name('santiago ', 'chile')
            self.assertEqual(formatted_name,'Santiago Chile')
    unittest.main()

    11-2

    def get_formatted_name(city,country,population=''):
        full_name = city + country + '-population' + str(population)
        return full_name.title()
    import unittest
    from city_functions import get_formatted_name
    class CityCountryTestCase(unittest.TestCase):
        def test_city_country(self):
            formatted_name =get_formatted_name('santiago ', 'chile','50000')
            self.assertEqual(formatted_name,'Santiago Chile-Population50000')
    unittest.main()

     11-3

    import  unittest
    class Employee ():
        def __init__(self,first,last,salary):
            self.first = first
            self.last = last
            self.salary = salary
        def give_raise(self,salary=5000):
            self.salary+=salary
    class TesstEmployee (unittest. TestCase) :
        def setUp(self):
            self.emp = Employee('','',1000)
        def test_give_default_raise(self):
            self.emp.give_raise() 
            self.assertEqual(self.emp.salary,6000)
        def test_give_custom_raise(self):
            self.emp.give_raise(6000)
            self.assertEqual(self.emp.salary,7000)
    unittest.main()
    输出:
    ..
    ----------------------------------------------------------------------
    Ran 2 tests in 0.001s
    
    OK
  • 相关阅读:
    协议
    创建属性、属性标签、对象序列化
    JS中generater和箭头函数
    前端forEach在Array、map、set中的使用,weakset,weakmap
    更新最大内码,金蝶开发
    ERP,还需要WEB开发吗
    可读性太低的SQL语句
    事务,视图和索引
    简单子查询
    创建表并添加约束
  • 原文地址:https://www.cnblogs.com/zhangyueba/p/12296007.html
Copyright © 2020-2023  润新知