python基础课程系列(十四)

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)
=======================================================
from survey import AnonymousSurvey

# 定义一个问题,并创建一个表示调查的 AnonymousSurvey 对象
question = "What language did you first learn to speak?"
my_survey = AnonymousSurvey(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 everyone who participated in the survey!")
my_survey.show_results()
=======================================================
What language did you first learn to speak?
Enter 'q' at any time to quit.

Language: English
Language: Spanish
Language: English
Language: Mandarin
Language: q

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

11.2.3.测试 AnonymousSurvey  类
下面来编写一个测试,对 AnonymousSurvey 类的行为的一个方面进行验证:如果用户面对调查问题时只提供了一个答案,这个答案也能被妥善地存储。
=======================================================
import unittest
from survey import AnonymousSurvey

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)

unittest.main()
=======================================================
.
----------------------------------------------------------------------
Ran 1 test in 0.026s

OK

下面来核实用户提供三个答案时,它们也将被妥善地存储。为此,我们在 TestAnonymousSurvey 中再添加一个方法:
=======================================================
import unittest
from survey import AnonymousSurvey

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_responses(self):
        """测试三个答案会被妥善地存储"""
        question = 'What language did you first learn to speak?'
        my_survey = AnonymousSurvey(question)
        responses = ['English', 'Spanish', 'Mandrin']
        for response in responses:
            my_survey.store_response(response)

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

unittest.main()
=======================================================
..
----------------------------------------------------------------------
Ran 2 tests in 0.005s

OK

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类的测试"""
    def setUp(self):
        """创建一个调查对象和一组答案,供使用的测试方法使用"""
        question = 'What language did you first learn to speak?'
        self.my_survey = AnonymousSurvey(question)
        self.responses = ['English', 'Spanish', 'Mandrin']

    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()
=======================================================
..
----------------------------------------------------------------------
Ran 2 tests in 0.010s

OK

11.3.小结
在本章中,你学习了:如何使用模块 unittest 中的工具来为函数和类编写测试;如何编写继承 unittest.TestCase 的类,以及如何编写测试方法,以核实函数和类的行为
符合预期;如何使用方法 setUp() 来根据类高效地创建实例并设置其属性,以便在类的所有测试方法中都可使用它们。

猜你喜欢

转载自blog.csdn.net/zhaocen_1230/article/details/81667519