#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("\nEnter q to quit.")
while True:
#输入城市和国家
city = input("\nEnter a city: ")
if city == 'q':
break
country = input("\nEnter 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","")
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()

Python:从入门到实践--第十一章--测试代码--练习的更多相关文章

  1. Python 从入门到实践 试一试 参考代码

    这两天学习Python 看了python从入门到实践的书籍,里面有课后题“试一试” 然后就跟着写了,代码在以下地址,如果需要自取 https://files.cnblogs.com/files/fud ...

  2. Python:从入门到实践--第四章--列表操作--练习

    #1.想出至少三种你喜欢的水果,将其名称存储在一个列表中,再使用for循环将每种水果的名称都打印出来. #要求:(1)修改这个for循环,使其打印包含名称的句子,而不是仅仅是水果的名称.对于每种水果, ...

  3. Python:从入门到实践--第三章--列表简介--练习

    #1.将一些朋友的姓名存储在一个列表中,并将其命名为friends.依次访问该列表中的每个元素,从而将每个朋友的姓名都打印出来. #2.继续使用1中的列表,为每人打印一条消息,每条消息包含相同的问候语 ...

  4. Python:从入门到实践--第六章--字典--练习

    #1.人:使用一个字典来存储一个熟人的信息;包括姓,名,年龄和居住的城市.将字典中的每项信息都打印出来 friend = { 'last_name':'马', 'first_name':'脑壳', ' ...

  5. python 从入门到实践 第三章

    在第3章,你将学习如何在被称为列表的变量中存储信息集,以及如何通过遍历列表来操作其中的信息 写注释 # 代码越长 标识好代码的重要性 越来越重要要求习惯:在代码中编写清晰,简洁的注释开始研究更复杂的主 ...

  6. Python:从入门到实践--第五章--if语句--练习

    #1.编写一系列条件测试:将每个测试以及结果打印出来 car = '宝马' if car == "宝马": print("预测正确") print(car) e ...

  7. Python:从入门到实践--第七章--用户输入和while循环-练习

    #1.编写一个程序,询问用户要租赁什么样的汽车,并打印. car = input("What's kind of cars dou you want to rent?,sir:") ...

  8. MyBatis从入门到精通:第一章测试代码

    package tk.mybatis.simple.mapper; import org.apache.ibatis.io.Resources; import org.apache.ibatis.se ...

  9. #Python编程从入门到实践#第四章笔记

    #Python编程从入门到实践#第四章笔记   操作列表 ​​​1.遍历列表 使用for循环,遍历values列表 for value in values: print(value) 2.数字列表 使 ...

随机推荐

  1. docker 镜像运行问题

  2. html5 的存储

    html5提供了很多存储的功能,诸如localStorage,sessionStorage,indexedDB,还有离线缓存等,本次主要介绍离线缓存跟本地存储. 离线缓存  使用离线存储可以缓存部分文 ...

  3. Python_Mix*函数名的使用以及第一类对象,闭包,迭代器,for循环的内部机制

    一:函数名的应用(第一类对象) 函数名的命名规范和变量是一样的,函数名其实就是变量名, 0)函数名可以赋值给其他变量 def func(): #定义一个名为func的函数 print('my ange ...

  4. vue页面操作技巧

    // this.$router.push({ path: "https://www.baidu.com/"}); // POST请求的时候 // this.$router.push ...

  5. java入门day03

    控制流程 一:   if else   /    switch 1. 随机数的产生:导入import java.lang.Math; num=Math.random()  -->[0,1); [ ...

  6. Centos7搭建docker仓库

    一:安装启动registry 1.1:环境准备 yum install -y python-devel libevent-devel python-pip gcc xz-devel pip insta ...

  7. c#分布式ID生成器

    c#分布式ID生成器   简介 这个是根据twitter的snowflake来写的.这里有中文的介绍. 如上图所示,一个64位ID,除了最左边的符号位不用(固定为0,以保证生成的ID都是正数),还剩余 ...

  8. (转) java 通过 jdbc 链接 ms sql server 中出现 "no suitable driver for ..."

    原文连接 : http://blog.csdn.net/stewen_001/article/details/19553173/ 前面是 基本操作步骤,按照原博主的方式进行操作即可...() 这里是需 ...

  9. asp.net core 实战项目(一)——ef core的使用

    数据库设计 数据结构图如下:   此次实例比较简单,暂时只设计到上述3张表 SMUser:用于存储用户信息. Role:用于存储角色信息. SMUser_Role:用建立用户和角色关系的一直关联表. ...

  10. DAY6:文件读取

      文件读取: f = open("文件名","r",encoding="utf-8").read()#文件读取 print(f) 写入文件 ...