一、测试函数
我们可以在每次修改函数时测试函数的功能,但是太麻烦了,这里介绍python提供的一种自动测试函数输出的高效方式。
1.单元测试和测试用例
python标准库中的 unittest 提供了代码测试工具
单元测试: 用来核实函数的某个方面没有问题
测试用例: 是一组单元测试,用来核实函数在各种情形下的性胃都符合要求。
2.可通过的测试
要为函数编写测试用例,要先导入模块 unittest 以及要测试的函数,再创建一个继承 unittest.TestCase 的类,并编写一系列方法对函数行为的不同方面进行测试。
要测试的函数(f_name.py):
def get_formatted_name(first,last):
full_name=first+' '+last
return full_name.title()
测试:
import unittest #导入模块
from f_name import get_formatted_name # 导入要测试的函数
class NameTest(unittest.TestCase):
def test_full_name(self):
formatted_name=get_formatted_name('janis','joplin')
self.assertAlmostEqual(formatted_name,'Janis Joplin')#判断
unittest.main()
结果:
. #表示测试通过了
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK #表示该测试用例中的所有单元测试都通过了
以上代码使用了unittest类的一个断言方法 assertAlmostEqual( ),用来核实得到的结果是否与期望的结果一致,并返回结果。
3.不能通过的测试
现在修改一下get_formatted_name函数再进行测试:
def get_formatted_name(first,middle,last):
full_name=first+' '+middle+' '+last
return full_name.title()
import unittest
from f_name import get_formatted_name
class NameTest(unittest.TestCase):
def test_full_name(self):
formatted_name=get_formatted_name('janis','joplin')
self.assertAlmostEqual(formatted_name,'Janis Joplin')
unittest.main()
用之前的测试程序再进行测试,结果为:
E #表示测试用例中有一个单元测试导致了错误
======================================================================
ERROR: test_full_name (__main__.NameTest) #未通过的测试
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\pycharmdata\pypractice\test1.py", line 7, in test_full_name
formatted_name=get_formatted_name('janis','joplin')
TypeError: get_formatted_name() missing 1 required positional argument: 'last' # 错误原因
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (errors=1) # 测试未通过的个数
4.测试未通过时怎么办
先检查测试程序,再检查测试函数
修改函数为:
def get_formatted_name(first,last,middle=''):
if middle:
full_name=first+' '+middle+' '+last
else:
full_name = first+' '+last
return full_name.title()
测试成功
5.添加新测试
测试加上中间名的全名是否正确
测试函数为:
import unittest
from f_name import get_formatted_name
class NameTest(unittest.TestCase):
def test_full_name(self):
formatted_name=get_formatted_name('janis','joplin')
self.assertAlmostEqual(formatted_name,'Janis Joplin')
def test_full_name2(self):
formatted_name=get_formatted_name('py','on','th')
self.assertAlmostEqual(formatted_name,'Py Th On')
unittest.main()
结果:
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s #通过两个测试
OK
二、测试类
1.常用断言方法
要继承 unittest.TestCase类才能使用
| 方法 | 用途 |
|---|---|
| assertEqual(a,b) | 核实a==b |
| assertNotEqual(a,b) | 核实a!=b |
| assertTrue(x) | 核实x为True |
| assertFalse(x) | 核实x为False |
| assertIn(item,list) | 核实item在list中 |
| assertNotIn(item,list) | 核实item不在list中 |
2.编写一个要测试的类
class Survey():
def __init__(self,question):
self.question=question
self.responses=[]
def show_question(self): #查看问题
print(self.question)
def store_response(self,new_response):#增加答案
self.responses.append(new_response)
def show_results(self):# 查看答案
for response in self.responses:
print('-'+response)
调用查看是否能正确运行:
from survey import Survey
newSurvey=Survey('你中午吃的什么?')
newSurvey.show_question()
while True:
food=input('你中午吃的什么')
if food=='q':
break
else:
newSurvey.store_response(food)
print('调查结果为:')
newSurvey.show_results()
答案:
你中午吃的什么?
你中午吃的什么西北风
你中午吃的什么东北风
你中午吃的什么南北风
你中午吃的什么北北风
你中午吃的什么q
调查结果为:
-西北风
-东北风
-南北风
-北北风
3.使用方法SetUp( )测试
测试类的方法和测试函数的方法相同但需在每个测试方法中都创建测试实例,使用unittest.TestCase类中的方法setUp( )让我们只需创建对象一次就可以在各个测试方法中使用。
import unittest
from survey import Survey
class TestSurvey(unittest.TestCase):
def setUp(self):
question='你今天吃的啥?'
self.my_survey=Survey(question)
self.responses=['西北风','东北风','南北风','北北风']
def test_single_response(self): #一个回答
self.my_survey.store_response(self.responses[0])
self.assertIn(self.responses[0],self.my_survey.responses)
def test_four_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()
结果:
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
本文介绍Python中的单元测试方法,包括如何使用unittest库进行测试函数和测试类,详细讲解了通过及未通过测试的情况处理,以及如何添加新测试。
51万+

被折叠的 条评论
为什么被折叠?



