《Python编程从入门到实践》第十一章 测试代码 学习笔记1、测试函数

1、Python标准库中模块unittest提供了代码测试工具

单元测试:核实函数的某个方面没有问题
测试用例:一组单元测试,核实函数在各种情形下的行为都符合要求
全覆盖测试:一整套单元测试,涵盖了各种可能的函数使用方式

2、unittest.main()让Python运行当前文件的中的测试,运行测试文件时,所有以test_打头的方法都自动运行。
3、编写测试用例:先导入模块unittest以及要测试的函数,再创建一个继承unittest.TestCase的类,并编写一系列方法对函数行为的不同方面进行测试

练习1
def address(city,country):
	site=city+','+country
	return site
print(address('Santiago','Chile'))

在这里插入图片描述

练习2

在这里插入图片描述
在这里插入图片描述

def address(city,country):
	site=city+','+country
	return site
import unittest
from city_functions import address


class CityTest(unittest.TestCase):
	"""测试city_functions.py"""
	def test_city_functions(self):
		site=address('Santiago','Chile')
		self.assertEqual(site,'Santiago,Chile')
unittest.main()

在这里插入图片描述

练习3

在这里插入图片描述
在这里插入图片描述

def address(city,country,population):
	message=city+','+country+"-population "+population
	return message

import unittest
from city_functions import address


class CityTest(unittest.TestCase):
	"""测试city_functions.py"""
	def test_city_functions(self):
		site=address('Santiago','Chile',"5000000")
		self.assertEqual(site,'Santiago,Chile-population 5000000')
unittest.main()

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值