读书笔记「Python编程:从入门到实践」_11.测试函数

11.1 测试函数

  要学习测试,得有要测试的代码。下面是一个简单的函数,它接受名和姓并返回整洁的姓名:

def get_formatted_name(first, last):
    """Generate a neatly formatted full name."""
    full_name = first + ' ' + last
    return full_name.title()

  为核实get_formatted_name() 像期望的那样工作,我们来编写一个使用这个函数的程序。

  程序names.py让用户输入名和姓,并显示整洁的全名:

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("\tNeatly formatted name: " + formatted_name + '.')

  我们可以在每次修改get_formatted_name() 后都进行测试:运行程序names.py,并输入像Janis Joplin 这样的姓名,但这太烦琐

  我们可以利用单元测试函数,每次修改完元source以后,直接运行单元测试函数来判断程序是否正确

  11.1.1 单元测试和测试用例  

  Python标准库中的模块unittest 提供了代码测试工具。
  单元测试 用于核实函数的某个方面没有问题;
  测试用例 是一组单元测试,这些单元测试一起核实函数在各种情形下的行为都符合要求。
  良好的测试用例考虑到了函数可能收到的各种输入,包含针对所有这些情形的测试。
  全覆盖式测试 用例包含一整套单元测试,涵盖了各种可能的函数使用方式。

  11.1.2 可通过的测试

  test_name_function.py

import unittest
from name_function import get_formatted_name

#创建了一个名为NamesTestCase 的类,用于包含一系列针对get_formatted_name() 的单元测试。
#最好让它看起来与要测试的函数相关,并包含字样Test
#这个类必须继承unittest.TestCase 类
class NamesTestCase(unittest.TestCase):
    """测试name_function.py"""
    #我们运行testname_function.py时,所有以test 打头的方法都将自动运行
    def test_first_last_name(self):
        """能够正确地处理像Janis Joplin这样的姓名吗?"""
        formatted_name = get_formatted_name('janis', 'joplin')
        #使用了unittest 类最有用的功能之一:一个断言 方法。断言方法用来核实得到的结果是否与期望的结果一致
        self.assertEqual(formatted_name, 'Janis Joplin')

unittest.main()
.
----------------------------------------------------------------------
Ran 1 test in 0.002s

OK

  11.1.3 不能通过的测试

  test_name_function.py

import unittest
from name_function import get_formatted_name

#创建了一个名为NamesTestCase 的类,用于包含一系列针对get_formatted_name() 的单元测试。
#最好让它看起来与要测试的函数相关,并包含字样Test
#这个类必须继承unittest.TestCase 类
class NamesTestCase(unittest.TestCase):
    """测试name_function.py"""
    #我们运行testname_function.py时,所有以test 打头的方法都将自动运行
    def test_first_last_middle_name(self):
        """能够正确地处理像Wolfgang Amadeus Mozart这样的姓名吗?"""
        formatted_name = get_formatted_name('wolfgang', 'mozart', 'amadeus')
        self.assertEqual(formatted_name, 'Wolfgang Amadeus Mozart')

unittest.main()
E
======================================================================
ERROR: test_first_last_middle_name (__main__.NamesTestCase)
能够正确地处理像Wolfgang Amadeus Mozart这样的姓名吗?
----------------------------------------------------------------------
Traceback (most recent call last):
  File "d:\40.勉強資料\python\test_name_function.py", line 11, in test_first_last_middle_name
    formatted_name = get_formatted_name('wolfgang', 'mozart', 'amadeus')
TypeError: get_formatted_name() takes 2 positional arguments but 3 were given

----------------------------------------------------------------------
Ran 1 test in 0.003s

FAILED (errors=1)

  11.1.4 测试未通过时怎么办

  测试未通过时怎么办呢?如果你检查的条件没错,测试通过了意味着函数的行为是对的,
  而测试未通过意味着你编写的新代码有错。因此,测试未通过时,不要修改测试,而应修复导致测试不能通过的代码:
  检查刚对函数所做的修改,找出导致函数行为不符合预期的修改

 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()

  11.1.5 添加新测试

import unittest
from name_function import get_formatted_name

#创建了一个名为NamesTestCase 的类,用于包含一系列针对get_formatted_name() 的单元测试。
#最好让它看起来与要测试的函数相关,并包含字样Test
#这个类必须继承unittest.TestCase 类
class NamesTestCase(unittest.TestCase):
    """测试name_function.py"""
    #我们运行testname_function.py时,所有以test 打头的方法都将自动运行
    def test_first_last_name(self):
        """能够正确地处理像Janis Joplin这样的姓名吗?"""
        formatted_name = get_formatted_name('janis', 'joplin')
        #使用了unittest 类最有用的功能之一:一个断言 方法。断言方法用来核实得到的结果是否与期望的结果一致
        self.assertEqual(formatted_name, 'Janis Joplin')

    def test_first_last_middle_name(self):
        """能够正确地处理像Wolfgang Amadeus Mozart这样的姓名吗?"""
        formatted_name = get_formatted_name('wolfgang', 'mozart', 'amadeus')
        self.assertEqual(formatted_name, 'Wolfgang Amadeus Mozart')
unittest.main()

11.2 测试类

  11.2.1 各种断言方法

  方法用途
  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 中

  11.2.2 一个要测试的类

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)

  11.2.3 测试Anonymous

import unittest
from survey import AnonymousSurvey

class TestAnonmyousSurvey(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_responses(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()

  11.2.4 方法setUp()  

  在前面的test_survey.py中,我们在每个测试方法中都创建了一个AnonymousSurvey 实例,并在每个方法中都创建了答案。

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

  如果你在TestCase 类中包含了方法setUp() ,Python将先运行它,再运行各个以test_打头的方法。这样,在你编写的每个测试方法中都可使用在方法setUp() 中创建的对象了

import unittest
from survey import AnonymousSurvey

class TestAnonymousSurvey(unittest.TestCase):
    """针对AnonymousSurvey类的测试"""
    #可在setUp() 方法中创建一系列实例并设置它们的属性,再在测试方法中直接使用这些实例。
    #相比于在每个测试方法中都创建实例并设置其属性,这要容易得多
#方法setUp() 做了两件事情:创建一个调查对象;创建一个答案列表
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(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.store_response(response) for response in self.responses: self.assertIn(response, self.my_survey.responses) unittest.main()

猜你喜欢

转载自www.cnblogs.com/changxinblog/p/10184736.html