码迷,mamicode.com
首页 > 编程语言 > 详细

Python单元测试unittest

时间:2018-08-09 14:09:17      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:一个   ini   last   zhang   __init__   code   unittest   lse   format   

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

标签:一个   ini   last   zhang   __init__   code   unittest   lse   format   

原文地址:https://www.cnblogs.com/zxylock/p/9448176.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!