下一个小时,或者,下半个小时,就是你的未来。你要怎么度过?

 一、流程梳理

1、测试代码首先要引入一个叫作“unittest”的包,

2、测试文件的名字一定要让人能一看就知道是测试哪个文件的。

3、在测试文件中,引入要测试的那个函数。

4、创建一个类,类名最好能表示出你测试的是哪个函数,一般是xxxTestCase。

5、创建的这个类必须继承 unittest.TestCase

6、运行测试类时,所有以test_打头的方法都将自动运行。

举例:

 name_function.py

def get_formatted_name(first,last):
	'''生成整洁的改名'''
	full_name=first+' '+last
	return full_name.title()

test_name_functon.py

import unittest
from name_function import get_formatted_name

class NamesTestCase(unittest.TestCase):
	'''测试name_function.py'''
	def test_first_last_name(self):
		formatted_name=get_formatted_name('janis','joplin')
		self.assertEqual(formatted_name,'Janis Joplin')
		
unittest.main()

【重点】arrestEqual(a,b)方法会判断a 与b 是否相等,a 是我们运行结果,b是期待结果。

unittest.main()方法让python运行这个文件中的测试。

结果:

python xgboost 准确率召回率 python 回测代码_测试类

点代表通过测试。

接下来的时间表示消耗的时间。

最后的OK代表所有单元测试都通过了。

 

测试未通过时怎么办?

不要修改测试,而是要修改导致测试不能通过的代码,检查刚对函数所做的修改,找出导致函数行为不符合预期的修改。

下面把文件修改成可以添加有中间名的命名:

name_function.py

def get_formatted_name(first,middle,last):
	'''生成整洁的改名'''
	full_name=first+' '+middle+' '++last
	return full_name.title()

再运行测试:

 

python xgboost 准确率召回率 python 回测代码_运行测试_02

E代表测试用例中有一个单元楼测试导致了错误。

REEOR指出了哪一个单元测试错误了。

下面一堆是解释了一下错误原因。

最后一句指出有多少测试用例错误了。

下面来改正导致错误的原文件:

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()

再次测试,结果是正确的。

 添加新测试,用于测试包含中间名的姓名。

import unittest
from name_function import get_formatted_name

class NamesTestCase(unittest.TestCase):
	'''测试name_function.py'''
	def test_first_last_name(self):
		formatted_name=get_formatted_name('janis','joplin')
		self.assertEqual(formatted_name,'Janis Joplin')
		
	def text_first_middle_last_name(self):
		'''能正确处理像张宸宸这样的名字吗'''
		formatted_name=get_formatted_name('janis','kdkd','joplin')
		self.assertEqual(formatted_name,'Janis Kdkd Joplin')
unittest.main()

 

二、测试类

一共有四种断言方法:

assertEqual(a,b)               核实a==b

assertNotEqual(a,b)         核实a!=b

asserTrue(x)                     核实x为True

assertFalse(x)                   核实x为False

assertIn(item,list)              核实item在List中

assertNotIn(item,list)         核实item不在List中

举例:

创建一个要测试的类:

#创建一个要测试的类。
class AnonymousServey():
    def __init__(self,question):
        self.responses=[]
        self.question=question

    def store_responses(self,new_response):
        self.responses.append(new_response)

    def show_question(self):
        print(self.question)

    def show_responses(self):
        for response in responses:
            print(response)

编写一个使用上类的程序:

from survey import AnonymousServey

question="What is your favorite language?"
my_survey=AnonymousServey(question)

my_survey.show_question()
print("Enter 'q' at any time to quit.\n")

while True:
    response = input("Enter your favorite language: ")
    if response=='q':
        break
    else:
        my_survey.store_responses()

my_survey.show_responses()

创建一个测试类去测试上面的类:

#测试AnonymousServey
import unittest
from survey import AnonymouServey

class AnonymousServeyTest(unittest.TestCase):
    def test_store_single_question(self):
        que="what is your name"
        my_survey=AnonymousServey(que)
        my_survey.store_response('haha')
        self.assertIn('haha',my_survey.responses)
unittest.main()

创建一个具有两个测试功能的测试类:

import unittest
from survey import AnonymousServey
class AnonymousServeyTest(unittest.TestCase):
    def test_store_single_response(self):
        que="what is your name"
        my_survey=AnonymousServey(que)
        my_survey.store_response('haha')
        self.assertIn('haha',my_survey.responses)

    def test_store_three_response(self):
        que="what is your faovrite fruits?"
        my_survey=AnonymousServey(que)
        my_survey.responses=['orange','banana','grape']
        for item in responses:
            my_survey.stroe_response(item)
        for response in responses:
            assertIn(response,my_survey.responses)
unittest.main()

上面测试两个功能的测试类是通过建立了两个实例,能不能用一个实例测试多个功能呢?

答案是使用setUp()就可以。

setUp是TestCase类型包含的一个方法,让我们只需要创建一个对象,并在每个测试方法中使用它。python先运行setUp()方法,再运行test_打头的方法。

下面是上例中代码的重构版:

import unittest
from survey import AnonymousServey
class AnonymousServeyTest(unittest.TestCase):
    def setUp(self):
        que="what is your faovrite fruits?"
        self.my_survey=AnonymousServey(que)
        self.my_survey.responses=['orange','banana','grape']
        
    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 item in self.responses:
            self.my_survey.stroe_response(item)
        for response in self.responses:
            assertIn(response,self.my_survey.responses)
unittest.main()