编写的代码需要测试是否有Bug

1.函数测试

city_functions.py

def city_country(city, country):return str(city) + ',' + str(country)

city_country_unittest.py

import unittest  #导入测试模块
from city_functions import city_country as cc
class citytest(unittest.TestCase):def test_city_country(self):returnValue = cc("Santiago", "Chile")self.assertEqual(returnValue, "Santiago,Chile") #运行结果是否和期待值相等unittest.main() #运行测试


city_functions.py中函数添加一个参数,测试程序不变

def city_country(city, country, population):return str(city) + ',' + str(country) + " - " + "population " + str(population)


再对上面2个文件进行修改

def city_country(city, country, population = ''):if not population:return str(city) + ',' + str(country)else:return str(city) + ',' + str(country) + " - " + "population " + str(population)
import unittest
from city_functions import city_country as cc
class citytest(unittest.TestCase):def test_city_country(self):returnValue = cc("Santiago", "Chile")self.assertEqual(returnValue, "Santiago,Chile") #运行结果是否和期待值相等def test_city_country_population(self):returnValue = cc("Santiago", "Chile", 5000000)self.assertEqual(returnValue, "Santiago,Chile - population 5000000")
unittest.main() #运行测试

2.类测试

survey.py

class AnonymousSurvey():def __init__(self, question):self.question = questionself.responses = []def show_question(self):print(self.question)def store_response(self, new_response):self.responses.append(new_response)def show_results(self):print("Survey results:")for response in self.responses:print('- ' + response)

test_language_survey.py

import unittest
from survey import AnonymousSurveyclass testLanguageSurvey(unittest.TestCase):def setUp(self):    #创建的变量供后面测试函数用question = "What language did you first learn to speak?"self.my_survey = AnonymousSurvey(question)self.responses = ['English', 'Spanish', 'Mandarin']def test_store_single_response(self):self.my_survey.store_response(self.responses[0])self.assertIn(self.responses[0], self.my_survey.responses)def test_store_three_response(self):for response in self.responses:self.my_survey.store_response(response)for response in self.responses:self.assertIn(response, self.my_survey.responses)
unittest.main()

3.课后作业

employee.py

class Employee():def __init__(self, firstname, lastname, salary):self.firstname = firstnameself.lastname = lastnameself.salary = salarydef give_raise(self, raisedmoney = 5000):self.salary += raisedmoney

test_employee.py

from employee import Employee
import unittestclass test_employee(unittest.TestCase):def setUp(self) -> None:self.employee_default = Employee('michael', 'ming', 5000)self.employee_custom = Employee('kobe', 'bryant', 5000)def test_dafault(self):self.employee_default.give_raise()self.assertEqual(10000, self.employee_default.salary)def test_custom(self):self.employee_custom.give_raise(1000)self.assertEqual(6000, self.employee_custom.salary)unittest.main()

python--从入门到实践--chapter 11 代码测试unittest相关推荐

  1. Python从入门到实践习题答案(第九章 类)

    在学习<Python从入门到实践>,代码是自己编写的噢(虽然有些代码有参考其他大佬),点个赞再走8~ 9-1 餐馆:创建一个名为 Restaurant 的类,其方法__init__()设置 ...

  2. 改写《python数据挖掘入门与实践》第九章Gutenberg书籍下载代码

    @数据挖掘 改写<python数据挖掘入门与实践>第九章Gutenberg书籍下载代码 可能是gutenberg网站改版的缘故,随书附带的getdata.py代码执行会报错. 个人将其进行 ...

  3. Python从入门到实践

    Python从入门到实践 文章目录 Python从入门到实践 第2章 变量和简单的数据类型 2.2 变量的命名规则 2.3 字符串 2.4 数字 1.整数 2.浮点数 3.使用str( )避免类型错误 ...

  4. 董老师又双叒叕送书啦,10本《Python程序设计入门与实践》

    活动详情: 在本文文末留言,留言获得点赞. 自本文推送之时活动立即生效,当天2月20日晚上21:00结束,获赞最多的前10条留言,每人获赠一本书. 2月20日晚上21:01在本文文末置顶留言公布获奖名 ...

  5. 新书推荐--《Python程序设计入门与实践》

    过完春节快递小哥们上班以后,公众号会连续送几波书,敬请留意! 书名:Python程序设计入门与实践 ISBN:978-7-5606-5960-2 作者:董付国 页数:319页 例题数量:73例 演示代 ...

  6. 《python数据挖掘入门与实践》决策树预测nba数据集

    前言: 学到决策树预测球队输赢时,按照书中网址去下载数据集,无奈怎么也没下载成功.即使下载了excel文件也是破损的.咱可是学了python的银,那好吧,我就把它爬取下来.(资源在下面) 代码: '' ...

  7. python从入门到实践和从入门到精通-Python从入门到实践之列表|第1天

    User:你好我是森林 Date:2018-02-14 Mark:<Python从入门到实践> 列表 本章主要从列表的概念入手,逐步深入到对列表的操作. 列表 概念 列表由一系列按特定顺序 ...

  8. 《Python程序设计入门与实践》219道课后习题答案

    适用教材:Python程序设计入门与实践 I S B N:978-7-5606-5960-2 作    者:董付国 页    数:319页 例题数量:73个 演示代码:200段(不含例题代码) 习题数 ...

  9. 《Python从入门到实践》读书笔记——第五章 if语句

    <Python从入门到实践>读书笔记--第五章 if语句 1. 一个简单示例 cars = ['audi', 'bwm', 'subaru', 'toyota']for car in ca ...

最新文章

  1. tableau可视化数据分析60讲(十四)-tableau可视化视图(交叉表项目符号图)
  2. WEBSERVICE 之WSDL
  3. c++的thread类(c++线程简单用法)
  4. mysql slave同步_Slave_SQL_Running: No mysql同步故障解决方法
  5. conceptd什么时候上市_阳山水蜜桃多少钱一斤?什么时候成熟上市?
  6. [VB]数据库导入到 CSV 格式文件
  7. C#中 构造函数的执行
  8. vtkImageData处理之阈值分割
  9. mysql安装方法及使用
  10. PC蓝牙加串口调试助手调试蓝牙设备
  11. 物流系统服务开发设计专业方案
  12. c语言串口通信实验报告,单片机实验报告-串口实验
  13. 无线通信算法工程师知识地图
  14. 对嵌入式开发方向的一些思考:在物联网方向
  15. OpenGL (太阳,地球,月亮 +太阳系八大行星)
  16. 报错:error: not found: value spark val ratings = spark.sparkContext.(解决方案)
  17. python爬虫基础及实例---代码经过实测
  18. 文本 去除重复行(sublime Text3 ,正则表达式)
  19. 用户画像分析与场景应用
  20. 原画师一般用什么软件画画?原画师需要用到什么工具?

热门文章

  1. linux sql语句传参数,Linux/Unixshell参数传递到SQL脚本
  2. C语言程序设计二期末考试,C语言程序设计期末考试试卷2.doc
  3. 最近重构公司消息服务的架构设计
  4. plsql中文乱码 显示问号
  5. SpringCloud学习--微服务架构
  6. Linux 下用C语言连接 sqlite
  7. 【海淘域名】GoDaddy账户被锁定后的解决方法
  8. [转]python 中的字符串连接
  9. [Java]java反射随笔
  10. 为什么大家都只谈薪资,却不谈梦想?