Python单元测试unittest
Posted 我的软件测试求学之路
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python单元测试unittest相关的知识,希望对你有一定的参考价值。
2018年8月9日发布,后续补上注释等文字说明
————————————
首先需要测试代码
name_function.py是一个简单的函数,获取前后两个名字,然后组合起来
def get_formatted_name(first,last,middle=‘‘): if middle: full_name = first + ‘ ‘ + middle + ‘ ‘ + last else: full_name = first + ‘ ‘ + last return full_name.title()
names.py让用户输入名字,并输出
from name_function import get_formatted_name print("enter") while True: firstname = input("enter first: ") if firstname == ‘q‘: print("exit") break lastname = input("enter second: ") if lastname == ‘q‘: print("exit") break fullname = get_formatted_name(firstname,lastname) print(‘full name: ‘,fullname)
运行结果
1 enter 2 enter first: zhang 3 enter second: san 4 full name: Zhang San 5 enter first: q 6 exit
如果想要在name_function.py函数的first和last添加一个中间名,为了验证之前的函数是否能够正常工作,可以在每次修改后都去验证get_formatted_name(first,last)进行测试,未免太麻烦了。
Python提供了这样一种自动测试函数输出的方法,unittest。
import unittest from name_function import get_formatted_name class NameTestCase(unittest.TestCase): def test_first_last_name(self): formatted_name = get_formatted_name(‘janis‘,‘joplin‘) self.assertEqual(formatted_name,‘Janis Joplin‘) def test_first_last_middle_name(self): formatted_name = get_formatted_name(‘wolfgang‘,‘mozart‘,‘amadeus‘) self.assertEqual(formatted_name,‘Wolfgang Amadeus Mozart‘) unittest.main()
以上代码,导入模块以及要被测试的函数,
通过调用函数,断言与返回的formatted_name判断结果,最后unittest.main()执行以上结果。
运行结果
.. ---------------------------------------------------------------------- Ran 2 tests in 0.000s OK
再写一段代码
survey.py
class AnoymousSurvey(): 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)
language_servey.py
from survey import AnoymousSurvey question = "what language you first learn" my_survey = AnoymousSurvey(question) my_survey.show_question() print("enter q to quit") while True: response = input("language: ") if response == ‘q‘: break my_survey.store_response(response) print("--") print("thank your input") my_survey.show_results()
通过用户输入信息,然后存入列表,接着打印出来
接着使用unittest,test_survey.py
from survey import AnoymousSurvey import unittest class TestAnonymousSurvey(unittest.TestCase): def test_store_single_response(self): question = "what language you first learn1" my_survey = AnoymousSurvey(question) my_survey.show_question() my_survey.store_response(‘English‘) self.assertIn(‘English‘,my_survey.responses) def test_store_three_response(self): question = "what language you first learn2" my_survey = AnoymousSurvey(question) my_survey.show_question() responses = [‘English‘,‘Spanish‘,‘Chinese‘] for response in responses: my_survey.store_response(response) self.assertIn(response,my_survey.responses) if __name__ == ‘__main__‘: unittest.main()
或者使用setUp方法
import unittest from survey import AnoymousSurvey class TestAnonymousSurvey(unittest.TestCase): def SetUp(self): question = "what language you first learn" self.my_survey = AnoymousSurvey(question) self.responses = [‘English‘,‘Spanish‘,‘Chinese‘] 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_response(self): for response in self.responses: self.my_survey.store_response(response) for response in self.responses: self.assertIn(response,self.my_survey.response) if __name__ == ‘__main__‘: unittest.main()
以上是关于Python单元测试unittest的主要内容,如果未能解决你的问题,请参考以下文章