《Python编程从入门到实践(第2版)》读书笔记11章

测试代码

测试函数

原始的name_function.py

def get_formatted_name(first, last):
    """生成整洁的姓名"""
    full_name = f"{first} {last}"
    return full_name.title()

原始的names.py(为核实get_formatted_name()像期望的那样工作,来编写一个使用该函数的程序)

from name_function import get_formatted_name

print("Enter 'q' at any time to quit.")
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(f"\tNeatly formatted name: {formatted_name}.")

Enter 'q' at any time to quit.

Please give me a first name: janis
Please give me a last name: joplin
	Neatly formatted name: Janis Joplin.

Please give me a first name: q

单元测试和测试用例

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

单元测试用于核实函数的某个方面没有问题

测试用例是一组单元测试,他们一道核实函数在各种情况下的行为都符合要求。

全覆盖的测试用例包含一整套单元测试,涵盖了各种可能的函数使用方式。

可通过的测试

要为函数编写测试用例,可先导入模块 unittest 和要测试的函数,在创建一个继承 unittest.TestCase 的类,并编写一系列方法对函数行为的不同方面进行测试。

原始的test_name_function.py 只包含一个方法,它检查函数 get_formatted_name()在给定名和姓时能否正确工作

import unittest
from name_function import get_formatted_name   

class NamesTestCase(unittest.TestCase): #创建一个名为NamesTestCase的类,必须继承unittest.TestCase类
    """Tests for 'name_function.py'."""
    """测试name_function.py"""
    def test_first_last_name(self): #所有以test_打头的方法都将自动运行
        """Do names like 'Janis Joplin' work?"""
        """能够正确地处理像Janis Joplin这样的姓名吗?"""
        formatted_name = get_formatted_name('janis', 'joplin')
        #使用实参'janis'和'joplin'调用get_formatted_name(),并将结果赋值给变量formatted_name
        self.assertEqual(formatted_name, 'Janis Joplin') #断言方法
if __name__ == '__main__':  #这个文件作为主程序执行,变量__name__被设置为'__main__'
    unittest.main()

未通过的测试

第一次修改后的name.function.py

def get_formatted_name(first, middle, last):     # #添加了middle
    """生成整洁的姓名"""
    full_name = f"{first} {middle} {last}"
    return full_name.title()

names.py保持不变,测试用例test_name_function.py保持不变,运行test_name_function.py,测试未通过

TypeError: get_formatted_name() missing 1 required positional argument: 'last'

测试未通过时怎么办

如果测试未通过,不要修改测试,而应修复导致测试不能通过的代码:检查刚刚对函数所做的修改,找出导致函数行为不符合预期的修改。

要将中间名设置为可选的,可在函数定义中将形参middle移到列表末尾,并将其默认值指定为一个空字符串。还需要添加一个if 测试,以便根据是否提供了中间名相应地创建姓名。

第二次修改后的name_function.py

def get_formatted_name(first, last, middle=''):
    """Generate a neatly formatted full name."""
    if middle:
        full_name = f"{first} {middle} {last}"
    else:
        full_name = f"{first} {last}"
    return full_name.title()

names.py保持不变,测试用例test_name_function.py保持不变,运行test_name_function.py,测试通过

Ran 1 test in 0.002s
OK

添加新测试

增加一个测试,用于测试包含中间名的姓名。

可以在TestCase类中使用很长的方法名,而且这些方法名必须是描述性的,这样才能看懂测试未通过时的输出。

import unittest
from name_function import get_formatted_name

class NamesTestCase(unittest.TestCase):
    """Tests for 'name_function.py'."""
    
    def test_first_last_name(self):
        """Do names like 'Janis Joplin' work?"""
        formatted_name = get_formatted_name('janis', 'joplin')
        self.assertEqual(formatted_name, 'Janis Joplin')

    def test_first_last_middle_name(self): #增加一个测试,用于测试包含中间名的姓名
        """Do names like 'Wolfgang Amadeus Mozart' work?"""
        formatted_name = get_formatted_name(
            'wolfgang', 'mozart', 'amadeus')
        self.assertEqual(formatted_name, 'Wolfgang Amadeus Mozart')

if __name__ == '__main__':
    unittest.main()
    
Ran 2 tests in 0.003s

OK

测试类

各种断言方法

unittest模块中的断言方法

方法用途
assertEqual(a, b)核实 a == b
assertNotEqual(a, b)核实 a != b
assertTrue(X)核实 X 为 True
assertFalse(X)核实 X 为 False
assertIn(item, list)核实 item 在 list
assertNotIn(tem, list)核实 item 不在 list
assertIsNone(X)核实 X is None
assertIsNotNone(X)核实 X is not None
assertIn(a, b)核实 a in b
assertNotIn(a, b)核实 a not in b
assertIsInstance(a, b)核实 isinstance(a, b)
assertNotIsInstance(a, b)核实 not isinstance(a, b)

一个要测试的类

类的测试与函数的测试相似,你所做的大部分工作是测试类中方法的行为。

下面编写一个要测试的类(帮助管理匿名调查的类)

class AnonymousSurvey:
    """Collect anonymous answers to a survey question."""
    """收集匿名调查问卷的答案"""
    def __init__(self, question):
        """Store a question, and prepare to store responses."""
        """存储一个问题,并为存储答案做准备"""
        self.question = question
        self.responses = []
        
    def show_question(self):
        """Show the survey question."""
        """显示调查问卷"""
        print(self.question)
        
    def store_response(self, new_response):
        """Store a single response to the survey."""
        """存储单份调查问卷"""
        self.responses.append(new_response)
        
    def show_results(self):
        """Show all the responses that have been given."""
        """显示收集到的所有答卷"""
        print("Survey results:")
        for response in self.responses:
            print(f"- {response}")

为证明AnonymousSurvey类能够正常工作,编写一个使用它的程序:

from survey import AnonymousSurvey

# Define a question, and make a survey.
#定义一个问题,并创建一个调查
question = "What language did you first learn to speak?"
my_survey = AnonymousSurvey(question)

# Show the question, and store responses to the question.
#显示问题并存储答案
my_survey.show_question()
print("Enter 'q' at any time to quit.\n")
while True:
    response = input("Language: ")
    if response == 'q':
        break
    my_survey.store_response(response)

# Show the survey results.
#显示调查结果
print("\nThank you to everyone who participated in the survey!")
my_survey.show_results()


Enter 'q' at any time to quit.

Language: English
Language: Spanish
Language: q

Thank you to everyone who participated in the survey!
Survey results:
- English
- Spanish

测试 AnonymousSurvey 类

验证:如果用户只提供一个答案,这个答案也能被妥善地存储。为此,在这个答案被存储后使用方法 assertIn()来核实它确实在答案列表中。

第一个方法验证:调查问题的单个答案被存储后,会包含在调查结果列表中。对于这个方法,一个不错的描述性名称是 test_store_single_response()。

要测试类的行为,需要创建其实例。使用问题"What language did you first learn to speak?"创建一个名为my_survey的实例。

import unittest                           #导入模块 unittest 和要测试的类 AnonymousSurvey
from survey import AnonymousSurvey        #将测试用例 命名为TestAnonymousSurvey

class TestAnonymousSurvey(unittest.TestCase):
    """针对AnonymousSurvey类的测试"""
    def test_store_single_response(self):
        """测试单个答案会被妥善地存储"""
        question = "What language did you first learn to speak?"
        my_survey = AnonymousSurvey(question)   #使用问题"What...speak?"创建一个名为my_survey的实例,
        my_survey.store_response('English')  #使用方法store_response存储单个答案English
        self.assertIn('English',my_survey.responses)

if __name__ == '__main__':
    unittest.main()
    
 Ran 1 test in 0.002s

OK

只能收集一个答案的调查用途不大。下面来核实当用户提供三个答案时,它们也将被妥善地存储。为此,在TestAnonymousSurvey中再添加一个方法。

import unittest                           #导入模块 unittest 和要测试的类 AnonymousSurvey
from survey import AnonymousSurvey        #将测试用例 命名为TestAnonymousSurvey

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)      #对其中每个答案调用store_response()
        for response in responses: #存储这些答案后,使用一个循环确认每个答案都包含在my_survey.responses中
            self.assertIn(response, my_survey.responses)

if __name__ == '__main__':
    unittest.main()

Ran 2 tests in 0.003s  #运行test_survey.py,两个测试都通过了。

OK

方法 setUp( )

unittest.TestCase类包含的方法setUp()让我们只需创建这些对象一次,就能在每个测试方法中使用。

如果在 TestCase 类中包含了方法 setUp(),Python 将先运行它,再运行各个以 test_打头的方法。

import unittest                          #导入模块 unittest 和要测试的类 AnonymousSurvey
from survey import AnonymousSurvey

class TestAnonymousSurvey(unittest.TestCase):   #将测试用例 命名为TestAnonymousSurvey
    """Tests for the class AnonymousSurvey"""
    """针对TestAnonymousSurvey 类的测试"""
    def setUp(self):
        """
        Create a survey and a set of responses for use in all test methods.
        """
        """创建一个调查对象和一组答案,供使用的测试方法使用"""
        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):
        """Test that a single response is stored properly."""
        """测试单个答案会被妥善地存储"""
        self.my_survey.store_response(self.responses[0])
        self.assertIn(self.responses[0], self.my_survey.responses)

    def test_store_three_responses(self):
        """Test that three individual responses are stored properly."""
        """测试三个答案会被妥善地存储"""
        for response in self.responses:
            self.my_survey.store_response(response)
        for response in self.responses:
            self.assertIn(response, self.my_survey.responses)

if __name__ == '__main__':
    unittest.main()
 
Ran 2 tests in 0.002s

OK
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值