Python3:测试代码

测试函数

单元测试和测试用例

  • Python标准库中的模块unittest提供了代码测试工具
  • 单元测试用于核实函数的某个方面没有问题,测试用例是一组单元测试;
  • 全覆盖式测试用例包含一整套单元测试

测试函数

  • 首先需要准备要测试的函数
# name_func.py
def get_formatted_name(first, last):
    full_name = first + ' ' + last
    return full_name.title()
  • 编写测试用例
#test_name_func.py
import unittest
from name_func import get_formatted_name

class NamesTestCase(unittest.TestCase):
    """测试name_func.py"""
    def test_first_last_name(self):
        """能够正确地处理像Janis Joplin这样的姓名吗? """
        formatted_name = get_formatted_name('janis', 'joplin')
        self.assertEqual(formatted_name, 'Janis Joplin')

unittest.main()
  • 首先,导入模块unittest和要测试的函数get_formatted_name()
  • 然后创建一个继承unittest.TestCase的类NamesTestCase,用于包含一系列针对测试函数的单元测试
  • NamesTestCase包含一个方法test_first_last_name(),当我们运行test_name_func.py时,所有以test_开头的方法都将自动运行。
  • 接下来调用unittest的断言方法assertEqual(),用以比较函数结果和正确结果,如果相等,则万事大吉,如果不相等就和我说一声。
  • 测试成功,将输出如下
.
----------------------------------------------------------------------
Ran 1 tests in 0.000s
OK
  • 运行测试用例时,每完成一个单元测试, Python都打印一个字符:测试通过时打印一个句点;测试引发错误时打印一个E;测试导致断言失败时打印一个F

添加新测试

  • 如下
import unittest
from name_func import get_formatted_name

class NamesTestCase(unittest.TestCase):
    """测试name_func.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):
        """能够正确地处理像Wolfgang Amadeus Mozart这样的姓名吗? """
        formatted_name = get_formatted_name('wolfgang', 'mozart', 'amadeus')
        self.assertEqual(formatted_name, 'Wolfgang Amadeus Mozart')

unittest.main()
  • 同样,新添加的方法必须以test_打头,这个方法测试函数的另一个行为,即处理带中间名的名字。
  • 测试成功,将输出如下
..
----------------------------------------------------------------------
Ran  2 tests in 0.000s
OK

测试类

各种断言方法

  • Python在unittest.TestCase类中提供了很多断言方法,断言方法检查你认为应该满足的条件是否满足。
  • 下表描述了6个常用的断言方法
方法 用途
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中

准备要测试的类

  • 类的测试与函数的测试相似,因为你所做的大部分工作都是测试类中方法的行为,但也有一些不同之处。
  • 以下是一个要被测试的类
# 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)

测试AnonymousSurvey类

  • 下面编写一个测试,验证:如果用户面对调查问题时只提供了一个答案,这个答案也能被妥善地存储。为此我们用方法assertIn()来核实它包含在答案列表中:
#test_survey.py
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)

unittest.main()

方法setUp()

  • 示例代码如下
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(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()
  • 方法setUp()做了两件事:创建一个调查对象;创建一个答案列表 。存储这两样东西的变量名包含前缀self,因此可在这个类的任何地方使用。
  • 方法 test_store_three_response() 核 实 self.responses 中 的 第 一 个 答 案 self.responses[0] 被妥善地存储 。
  • 方 法 test_store_three_response() 核实 self.responses 中的全部三个答案都被妥善地存储。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 编写函数或类时,还可为其编写测试。通过测试,可确定代码面对各种输入都能够按要求的那样工作。测试让你信心满满,深信即...
    阿耀王子阅读 1,468评论 1 2
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,292评论 19 139
  • 编写函数或类时,还可为其编写测试。通过测试,可确定代码面对各种输入都能够按要求的那样工作。在程序中添加新代码时,你...
    Darren_Lin阅读 5,289评论 1 5
  • 接口测试自动化的优点: 1,web自动化说起来很多人都会直接想到UI自动化这个设计,很少有人直接第一个概念是接口自...
    路边看雪的小男孩阅读 4,963评论 1 26
  • 有人说,婚姻,若非天堂,即是地狱。 常见男人们处心积虑经营各种人际关系,每隔一段时间就吃饭、喝酒、打牌交流感情,或...
    英小熊阅读 677评论 3 5