zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    设置Centos7会话超时时间
    Shell浮点运算
    Maven 同一依赖多版本共存
    Java根据模板生成word
    Java条形码生成
    arcgis for js 4.x 悬浮显示popup
    tomcat 跨域配置
    Mysql8.0 版本 timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',报错
    volatile
    synchronized
  • 原文地址:https://www.cnblogs.com/zhangyueba/p/12296007.html
Copyright © 2011-2022 走看看