2021-10-23

Python Day4 

    11.2 测试类

survey.py

class AnnoymousSurvey:

    """收集匿名调查问卷的答案"""

    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_result(self):

        """显示收集到的所有答案"""

        print("Survey results:")

        for response in self.responses:

            print(f"- {response}")



test.survey.py

import unittest

from survey import AnnoymousSurvey

class TestAnnoymousSurvey(unittest.TestCase):

    """针对AnnoymousSurvey类的测试"""

    def test_store_single_response(self):

        """测试单个答案会被妥善的存储"""

        question="What language did you first learn to speak?"

        my_survey=AnnoymousSurvey(question)

        my_survey.store_response('English')

        self.assertIn('English',my_survey.responses)


if __name__=='__main__':

    unittest.main()

你可能感兴趣的:(2021-10-23)