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. 测试函数
name_function.py
def get_formatted_name(first, last, middle):
"""Generate a neatly-formatted full name."""
full_name = first + ' ' + middle + ' ' + last
return full_name.title()
test_name_function.py
# 1. 导入模块 unittest 和要测试的函数
import unittest
from name_function import get_formatted_name
# 2. 创建一个继承 unittest.TestCase 的类
class NamesTestCase(unittest.TestCase):
"""测试name_function.py"""
# 3. 编写一系列针对函数行为不同方面的测试方法
# 所有以 test_ 打头的方法都将自动运行
def test_first_last_name(self):
"""能够正确地处理像Janis Joplin这样的姓名吗?"""
formatted_name = get_formatted_name('janis', 'joplin')
# 测试方法中使用断言
self.assertEqual(formatted_name, 'Janis Joplin')
def test_first_middle_last_name(self):
"""能够正确地处理像Wolfgang Amadeus Mozart这样的姓名吗?"""
formatted_name = get_formatted_name('wolfgang', 'mozart', 'amadeus')
self.assertEqual(formatted_name, 'Wolfgang Amadeus Mozart')
# 4. 让 Python 运行这个文件中的测试
unittest.main()
=>
# 运行了2个测试,消耗时间不到0.004秒
Ran 2 tests in 0.004s
# 测试未通过,因为运行该测试用例时发生了一个错误,若测试通过,此处为“OK”
FAILED (errors=1)
Launching unittests with arguments python -m unittest test_name_function.NamesTestCase in /Users/Sally/ProgramProjects/Python/python_crash_course/chapter5
Error
Traceback (most recent call last):
File "/Users/Sally/ProgramProjects/Python/python_crash_course/chapter5/test_name_function.py", line 10, in test_first_last_name
formatted_name = get_formatted_name('janis', 'joplin')
TypeError: get_formatted_name() missing 1 required positional argument: 'middle'
3. 测试类
survey.py
Class AnonymousSurvey():
"""收集匿名调查问卷的答案"""
def __init__(self, question):
"""存储一个问题,并为存储答案做准备"""
self.question = question
self.responses = []
def show_question(self):
"""显示调查问卷"""
print(question)
def store_response(self, new_response):
"""存储单份调查答案"""
self.responses.append(new_response)
def show_results(self):
"""显示收集到的所有答卷"""
print("Survey results:")
for response in responses:
print('- ' + response)
test_survey.py
# 1. 导入模块 unittest 和要测试的类
import unittest
from survey import AnonymousSurvey
# 2. 将测试用例命名为 TestAnonymousSurvey 并继承 unittest.TestCase
class TestAnonymousSurvey(unittest.TestCase):
"""针对 AnonymousSurvey 类的测试"""
# 3. 编写方法 setUp()
# Python 将先运行它,再运行各个以 test_ 打头的方法
# 个人认为它是存在于测试类中类似于 __init__() 的方法
# setUp() 方法在测试类中并非必须存在,它会使测试方法编写起来更容易,如此处的 setUp() 让我们只需创建待测类的对象一次,并在各测试方法中使用
def setUp(self):
"""
创建一个调查对象和一组答案,供使用的测试方法使用
"""
question = "What language did you first learn to speak?"
# 要测试类的行为,需要创建其实例
# 变量名包含前缀 self(即存储在属性中),因此可在这个类的任何地方使用
# 是将待测类的对象和测试数据设置为属性,因为测试方法就需要这俩
self.my_survey = AnonymousSurvey(question)
self.responses = ['English', 'Spanish', 'Mandarin']
# 4. 编写测试方法
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_responses(self):
"""测试三个答案会被妥善地存储"""
for response in self.responses:
self.my_survey.strore_response(response)
for response in self.responses:
self.assertIn(response, self.my_survey.responses)
# 5. 运行测试
unittest.main()