11.测试代码
测试代码
测试函数
核实一系列输入是否能得到预期的输出
name_function.py
def get_formatted_name(first, last): """Generate a neatly formatted full name.""" full_name = first + ' ' + last return full_name.title()
name.py
from name_function import get_formatted_name while True: first = input("\nPlease give me a first name: ") if first == 'q': break last = input("Please give me a last name: ") if last == 'q': break formatted_name = get_formatted_name(first, last) print("\tNeatly formated name: " + formatted_name + '.')
test_name_function.py
import unittest from name_function import get_formatted_name class NamesTestCase(unittest.TestCase): """测试name_function.py""" def test_fist_last_name(self): """能够正确地处理像Janis Joplin这样的姓名吗""" formatted_name = get_formatted_name('janis', 'joplin') self.assertEqual(formatted_name, 'Janis Joplin') unittest.main()
输出:
运行时,所有以test打头的方法都会自动运行
输出中,第一个句点表明有一个测试通过了。接下来的一行指出Python运行了一个测试,消耗的时间不到0.001秒。最好OK表明该测试用例中的所有单元测试都通过了
name_function.py
def get_formatted_name(first, middle, last): """生成整洁的姓名""" full_name = first + ' ' + middle + ' ' + last return full_name.title()
运行test_name_function.py输出:
E:指出测试用例中有一个单元测试导致了错误
ERROR:指出哪个单元测试出错
Traceback:指出错误的原因
不要修改测试,而应修复导致测试不能通过的代码
name_function.py
def get_formatted_name(first, last, middle=''): """生成整洁的姓名""" if middle: full_name = first + ' ' + middle + ' ' + last else: full_name = first + ' ' + last return full_name.title()
添加新测试
import unittest from name_function import get_formatted_name class NamesTestCase(unittest.TestCase): """测试name_function.py""" def test_first_last_name(self): """能够正确地处理像Janis Joplin这样的姓名吗""" formatted_name = get_formatted_name('janis', 'joplin') self.assertEqual(formatted_name, 'Janis Joplin') def test_first_last_middle_name(self): """能够正确地处理像Wolfwang Amadeus Mozart这样的姓名吗""" formatted_name = get_formatted_name('wolfwang', 'mozart', 'amadeus') self.assertEqual(formatted_name, 'Wolfwang Amadeus Mozart') unittest.main()
输出:
两个单元测试都通过
各种断言方法
返回值为Ture或False
用途 | |
---|---|
assertEqual(a, b) | 核实a == b |
assertNotEqual(a, b) | 核实a != b |
assertTrue(x) | 核实x为Ture |
assertFalse(x) | 核实x为False |
assertIn(item, list) | 核实item在list中 |
assertNotIn(item, list) |
survey.py
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): """显示收集到的所有答卷""" print("Survey results: ") for response in self.responses: print('- ' + response)
import unittest from survey import AnonymousSurvey class TestAnonymousSurvey(unittest.TestCase): """针对AnonymousSurvey类的测试""" def test_store_single_response(self): """测试单个答案会被妥善地存储""" question = "What language did you first learn to speak?" my_survey = AnonymousSurvey(question) my_survey.store_response('English') self.assertIn('English', my_survey.responses) def test_store_three_response(self): """测试三个答案会被妥善地存储""" question = "What language did you first learn to speak?" my_survey = AnonymousSurvey(question) responses = ['English', 'Spanish', 'Mandarin'] for response in responses: my_survey.store_response(response) for response in responses: self.assertIn(response, my_survey.responses) unittest.main()
方法setUp()
unittest.TestCase类包含方法setUp()
如果TestCase类中包含方法setUp(),Python将先运行它,再运行各个以test_打头地方法
import unittest from survey import AnonymousSurvey class TestAnonymousSurvey(unittest.TestCase): """针对AnonymousSurvey类的测试""" 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('English') 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()
能够提高效率,减少重复