zoukankan      html  css  js  c++  java
  • Python:从入门到实践--第十一章--测试代码--练习

    #1.城市和国家:编写一个函数,它接受两个形参:一个城市名和一个国家名。
    #这个函数返回一个格式为City,Country的字符串,如Santiago,Chile。将这个函数
    #存储在一个名为city_function.py的模块中
    #创建一个名为test_cities.py的程序,对编写的函数进行测试
    #编写一个名为test_city_country()的方法核实得到的字符串正确
    
    #city_function.py
    
    def city_country(city,country):
        #国家和城市
        country_city = city + " " + country
        return country_city.title()
        
    #test_cities.py
    from city_function import city_country
    
    print("
    Enter q to quit.")
    while True:
        #输入城市和国家
        city = input("
    Enter a city: ")
        if city == 'q':
            break
        country = input("
    Enter country of the city: ")
        if country == 'q':
            break
        
        get_msg = city_country(city,country)
        print("The city and country: " + get_msg.title())
        
        
    #unittest_test_city.py
    import unittest
    from city_function import city_country
    
    class CityTestCase(unittest.TestCase):
        def test_city_country(self):
            get_city_country_name = city_country("beijing","china")
            self.assertEqual(get_city_country_name,"Beijing China")
        
    unittest.main()
    
    #2.人口数量:修改1中的函数,使其包含必不可少的形参population
    #并返回一个格式City,Country - population xxx 的字符串
    
    def city_country(city,country,population=''):
        #国家和城市
        country_city = city + ", " + country + " -- Population " + population
        return country_city.title()
    
    ##测试模块
    import unittest
    from city_function import city_country
    
    class CityTestCase(unittest.TestCase):
        def test_city_country(self):
            get_city_country_name = city_country("beijing","china","5000")
            self.assertEqual(get_city_country_name,"Beijing, China -- Population 5000")
        
    unittest.main()
            
    #3.雇员:编写一个名为Employee的类,其方法__init__()接受名、姓和年薪
    #并将它们存储在属性中。编写一个名为give_raise()的方法,它默认将年薪增加5000
    #但也能够接受其他的年薪增量。编写一个测试用例,其中包含两个测试方法:
    #test_give_default_raise()和test_give_custom_raise()
    #使用setUp方法,以免在每个测试方法中都创建新的雇员实例,运行这个测试用例
    #确认两个测试都通过
    
    #employee.py
    class Employee():
        
        def __init__(self,first_name,last_name,salary):
            self.first_name = first_name
            self.last_name = last_name
            self.salary = salary
            self.raising = 5000
            
        def give_raise(self):
            return self.raising
    
            
    #test_employee.py
    import unittest
    from employee import Employee
    
    class TestEmployee(unittest.TestCase):
        #针对Employee类的测试
        
        def setUp(self):
            self.employee_test = Employee('Ma','Naoke',5000)
            
        def test_give_default_raise(self):
           #测试默认的工资
            raising = self.employee_test.give_raise()
            self.assertEqual(raising,5000)
            
        def test_give_custom_raise(self):
            #测试年薪增量
            self.employee_test.raising = 6000
            raising = self.employee_test.give_raise()
            self.assertEqual(raising,6000)
    
    unittest.main()
  • 相关阅读:
    Linux就该这么学--Shell脚本基本应用
    Linux就该这么学--了解Shell脚本
    Linux就该这么学--命令集合11(配置系统相关信息)
    解決 centos -bash: vim: command not found
    Linux就该这么学--命令集合10(vim编辑器)
    Linux就该这么学--命令集合9(环境变量)
    html5 浏览器端数据库
    加密技术---仿射密码
    数组的运用、排序
    面试题参考
  • 原文地址:https://www.cnblogs.com/geeker-xjl/p/10645133.html
Copyright © 2011-2022 走看看