日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

测试必备:单元测试测试类,以帮助匿名管理调查的类为例详解--白盒测试

發(fā)布時間:2025/3/15 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 测试必备:单元测试测试类,以帮助匿名管理调查的类为例详解--白盒测试 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

類的測試與函數(shù)的測試相似
測試函數(shù)的案例:https://blog.csdn.net/hanhanwanghaha/article/details/107487037

https://blog.csdn.net/hanhanwanghaha寶藏女孩 歡迎您的關(guān)注!
歡迎關(guān)注微信公眾號:寶藏女孩的成長日記
讓這個可愛的寶藏女孩在努力的道路上與你一起同行!
如有轉(zhuǎn)載,請注明出處(如不注明,盜者必究)

廢話不多說上代碼

survey.py

class AnonymousSurvey():"""收集匿名調(diào)查問卷的答案"""def __init__(self, question):"""儲存一個問題,并為儲存答案做準(zhǔn)備"""self.question = questionself.responses = []def show_question(self):"""顯示調(diào)查問卷"""print(self.question)def store_response(self, new_response):"""儲存單份調(diào)查答卷"""self.responses.append(new_response)def show_results(self):"""顯示收到的所有答卷"""print("Servey results: ")for response in self.responses:print('favorite sport: ' + response)

為證明AnonymousSurvey能夠正確地工作,編寫一個使用他的程序

sports_survey

from survey import AnonymousSurvey# 定義一個問題,并創(chuàng)建dac一個表示調(diào)查的AnonymousSurvey對象 question = "What's your favorite sport ?" my_survey = AnonymousSurvey(question)# 顯示問題并儲存答案 my_survey.show_question() print("Enter 'q' at any time to quit.\n") while True:response = input("Sports:")if response == 'q':breakmy_survey.store_response(response)# 顯示調(diào)查結(jié)果 print("\nThank you for your participation in this survey!") my_survey.show_results()

創(chuàng)建測試的時候有個小技巧
1.點擊New,再點擊Python File之后

2.直接點擊Python unit test,隨后點擊Enter會幫我們自動生成一個單元測試框架

測試AnonymousSurvey類:這里用到了Setup方法創(chuàng)建一個調(diào)查對象和一組答案,供方法test_store_single_response()和test_store_three_responses()使用

import unittest from survey import AnonymousSurveyclass TestAnonymousSurvey(unittest.TestCase):"""針對AnonymousSurvey類的測試"""def setUp(self):"""創(chuàng)建一個調(diào)查對象和一組答案,供使用的測試方法使用"""question = "What's your favorite sport ?"self.my_survey = AnonymousSurvey(question)self.responses = ['sleep', 'reading', 'running']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):"""測試3個答案是否會被妥善儲存"""for response in self.responses:self.my_survey.store_response(response)for response in self.responses:self.assertIn(response, self.my_survey.responses)if __name__ == '__main__':unittest.main()

如果在TestCase類中包含方法setUp(),python將先運行它,再運行各個以test_開頭的方法。

https://blog.csdn.net/hanhanwanghaha寶藏女孩 歡迎您的關(guān)注!
歡迎關(guān)注微信公眾號:寶藏女孩的成長日記
讓這個可愛的寶藏女孩在努力的道路上與你一起同行!
如有轉(zhuǎn)載,請注明出處(如不注明,盜者必究)

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的测试必备:单元测试测试类,以帮助匿名管理调查的类为例详解--白盒测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。