python中使用 unittest.TestCase单元测试的用例详解

2022-09-03 0 214

单元测试和测试用例

python标准库中的模块unittest提供了代码测试工具。单元测试用于核实函数的莫个方面没有问题;测试用例是一组单元测试,这些单元测试一起核实函数在各种情形下的行为都符合要求。良好的测试用例考虑到了函数可能收到的各种输入,包含针对所有这些情形的测试。全覆盖测试用例包含一整套单元测试,涵盖了各种可能的函数使用方式。对于大型项目,要实现全覆盖可能很难,通常,最初只要针对代码的重要行为编写测试即可,等项目被广泛使用时再考虑全覆盖。

各种断言方法

python 在unittest.TestCase 中提高了很多断言方法。

unittest Module中的断言方法

方法 用途
assertEqual(a,b) 核实a == b
assertNotEqual(a,b) 核实a != b
assertTrue(x) 核实x为True
assertFalse(x) 核实x为False
assertIn(item,list) 核实ietm在list中
assertNotIn(item,list) 核实item不在list中

函数测试

 1.准备测试函数

name_function.py

def get_formatted_name(first, last):
    \'\'\'生成整洁的姓名\'\'\'
    full_name = first + \' \' + last
    return full_name.title()

2.编写一个能使用它的程序

nams.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 + \'.\')

3.对函数进行单元测试

test_name_function.py

import unittest
from unittest import TestCase

from name_function import get_formatted_name


class NamesTestCase(TestCase):
    \'\'\'测试name_function.py\'\'\'

    def test_first_last_name(self):
        \'\'\'能够正确地处理象 Janis Joplin这样的姓名吗?\'\'\'
        formtted_name = get_formatted_name(\'janis\', \'joplin\')
        self.assertEqual(formtted_name, \'Janis Joplin\')


# 执行
unittest.main()
python test_name_function.py

python中使用 unittest.TestCase单元测试的用例详解

第一行的句点 表示测试通过了,接下来的一行指出python运行了一个测试,消耗的时间不到0.001秒,最后的OK表示改测试用例中的所有测试单元都通过了。

类测试

1.准备测试的类

survey.py

class AnonmousSurvey():
    \"\"\"收集匿名调查问卷的答案\"\"\"

    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)

2.编写一个能使用它的程序

language_survey.py

from survey import AnonmousSurvey

# 定义一个问题,并创建一个表示调查的AnonymousSurvey对象
question = \"What language did you first learn to speak?\"
my_survey = AnonmousSurvey(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)

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

3.对类进行单元测试

import unittest

from survey import AnonmousSurvey


class TestAnonmousSurvey(unittest.TestCase):
    \"\"\"针对AnonymousSurvey类的测试\"\"\"

    def test_store_single_response(self):
        \"\"\"测试单个答案会被妥善地存储\"\"\"
        question = \"What language did you first learn to speak?\"
        my_survey = AnonmousSurvey(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 = AnonmousSurvey(question)
        responses = [\"English\", \"Chinses\", \"Japan\"]
        for response in responses:
            my_survey.store_response(response)

        for response in responses:
            self.assertIn(response, my_survey.responses)


unittest.main()

python中使用 unittest.TestCase单元测试的用例详解

可以看到对类的单元测试也是成功的。虽然成功了,但是做法不是很好,测试有些重复了,下面使用unittest的另一项功能来提高它们的效率

方法 setUP()

如果你在TestCase类中包含方法setUP(),python将先运行它,在运行各个以test_开头的方法。

test_survey_setup.py

import unittest

from survey import AnonmousSurvey


class TestAnonmousSurvey(unittest.TestCase):
    \"\"\"针对AnonymousSurvey类的测试\"\"\"

    def setUp(self):
        \"\"\"创建一个调查对象和一组答案,供使用的测试方法使用\"\"\"
        question = \"What language did you first learn to speak?\"
        self.my_survey = AnonmousSurvey(question)
        self.responses = [\"English\", \"Chinses\", \"Japan\"]

    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()让测试方法编写起来更容易:可以在setUP()方法中创建一系列实例并设置它们的属性,再在测试方法中直接使用这些实例。相比于在每个测试方法中都创建实例并设置属性,这要容易的多。

注意

运行测试用例时,每完成一个单元测试,python都打印一个字符: 测试通过时打印一个句点; 测试引发错误时打印一个E; 测试导致断言失败时打印一个F。这就是运行测试用例时,在输出的第一行中看到的句点和字符数量各不相同的原因。如果测试用例包含很多单元测试,需要运行很长时间,就可以通过观察这些结果来获悉有多少个测试通过了。

到此这篇关于python中使用 unittest.TestCase 进行单元测试的文章就介绍到这了,更多相关python单元测试内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

遇见资源网 Python python中使用 unittest.TestCase单元测试的用例详解 http://www.ox520.com/29441.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务