第十一章 测试代码
#name_function
def get_formatted_name(first, last):
full_name = first + ' ' + last
return full_name.title()#返回值
#name
from name_function import get_formatted_name#从name_function中调用get_formatted_name函数
print("Enter a 'q' to quit.")#提示用户
while True:
first = input("Please input you first name : ")
if first == 'q':
break
last = input("Please input you last name : ")
if last == 'q':
break
formatted_name = get_formatted_name(first, last)#调用,存储返回值
print(formatted_name)
①测试函数
Python标准库中的模块unit test提供了测试代码工具。
1.可通过的测试
先导入unittest以及要测试的函数,再创建一个继承unittest.TestCase的类。
import unittest
from name_function import get_formatted_name
class NamesTestCase(unittest.TestCase):
def test_first_last_name(self):
formatted_name = get_formatted_name('jsaad', 'adaf')
self.assertEqual(formatted_name, 'Jsaad Adaf')#断言方法
unittest.main()
#######
.
----------------------------------------------------------------------
Ran 1 tests in 0.000s
OK
2.不能通过的测试
#name_function
def get_formatted_name(first, middle, last):
full_name = first + ' ' + middle +' ' + last
return full_name.title()
#test
import unittest
from name_function import get_formatted_name
class NamesTestCase(unittest.TestCase):
def test_first_last_name(self):
formatted_name = get_formatted_name('jsaad', 'adaf')
self.assertEqual(formatted_name, 'Jsaad Adaf')
unittest.main()
这个代码则显示测试未通过。书上对错误的描述十分详细,不赘述了。
3.测试未通过时怎么办
应对参与测试的函数进行修改
def get_formatted_name(first, middle = '', last):#控制参数的个数
if middle:
full_name = first + ' ' + middle +' ' + last
else:
full_name = first + ' ' + last
return full_name.title()
4.添加新测试
import unittest
from name_function import get_formatted_name
class NamesTestCase(unittest.TestCase):
def test_first_middle_last_name(self):
formatted_name = get_formatted_name('qwe', 'rty', 'qwe')
self.assertEqual(formatted_name,'Qwe Qwe Rty')
def test_first_last_name(self):
formatted_name = get_formatted_name('jsaad', 'adaf')
self.assertEqual(formatted_name, 'Jsaad Adaf')
unittest.main()
#
..
----------------------------------------------------------------------
Ran 2 ests in 0.000s
OK
②测试类
1.各种断言方式
assertEqual(a, b) 核实 a == b
assertNotEqual(a, b) 核实 a != b
assertTure(a, b) 核实 x为True
assertFalse(a, b) 核实x为Flase
assertNotIn(item, list) 核实item在list中
assertNotIn(item, list) 核实item不在list中
2.一个要测试的类
#survey
class AnonymousSurvey:
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 x in **self.**responses:
print(x)
##加星号的是书上漏的,不加会报错
#language_survey
from survey import AnonymousSurvey
question = "What is your favorite language?"
my_survey = AnonymousSurvey(question)
my_survey.show_question()
print("Enter 'q' at any time to quit.")
while True:
response = input("language: ")
if response == 'q':
break
my_survey.store_response(response)
my_survey.show_results()
####输出
language: C
language: Java
language: q
C
Java
3.测试AnonymousSurvey类
#test_survey
import unittest
from survey import AnonymousSurvey
class TestAnonymousSurvey(unittest.TestCase):
def test_store_single_response(self):
question = 'What language do you like.'
my_survey = AnonymousSurvey(question)
my_survey.store_response('English')
self.assertIn('English', my_survey.responses)
unittest.main()
####
.
----------------------------------------------------------------------
Ran 1 tests in 0.000s
OK
同样,在AnonymousSurvey添加新的方法时也会被检验。
4.方法setup()
使用setup()来创建一个调查对象和一组答案。
import unittest
from survey import AnonymousSurvey
class TestAnonymousSurvey(unittest.TestCase):
def setUp(self):
question = 'What language do you like.'
self.my_survey = AnonymousSurvey(question)
self.response = ['English', 'Spanish','C']
def test_store_single_response(self):
self.my_survey.store_response(self.response[0])
self.assertIn(self.response[0], self.my_survey.responses)
def test_store_three_response(self):
for x in self.response:
self.my_survey.store_response(x)
for x in self.response:
self.assertIn(x, self.my_survey)
unittest.main()
说实话,类我理解的不是很透彻。不过到此为止,基础部分已经结束了,准备把习题代码敲完,复习一下。然后开始项目的实战,做个游戏,对一些数据进行可视化,创建一个Web应用程序。继续加油吧!!!!