unittest之makeSuitetestloaddiscover及测试报告teseReport

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

unittest之makeSuitetestloaddiscover及测试报告teseReport 测试套件suite除了使⽤addTest以外,还有使⽤操作起来更更简便的makeSuite\testload\discover
1、makeSuite,创建测试套件,传的参数是要执⾏的测试⽤例所在的类名,如下代码makeSuite()⾥传⼊的就是⽤例test01\test02所在的类Login,
代码:reload(sys)
sys.setdefaultencoding('utf-8')是将代码中的字符类型都转为UTF-8编码格式
#!/usr/bin/env.python
#-*-coding:utf-8-*-
from selenium import webdriver
import unittest
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
class Login(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Firefox()
cls.driver.maximize_window()
cls.driver.implicitly_wait(30)
cls.driver.get('https:///')
def test01(self):
'''验证⽹页title是否正确'''
self.assertTrue(str(self.driver.title).startswith('百度⼀下'))
def test02(self):
'''验证⽹址是否正确'''
self.assertEqual(self.driver.current_url, 'https:///')
@classmethod
def tearDownClass(cls):
cls.driver.quit()
@staticmethod
def suite():
suite = unittest.makeSuite(Login)
return suite
if __name__ == '__main__':
unittest.TextTestRunner(verbosity=2).run(Login.suite())
2、testload:⼀般只有这个⽅法suite=unittest.TestLoader.loadTestsFromTestCase(Login),传⼊的也是类名
#!/usr/bin/env.python
#-*-coding:utf-8-*-
from selenium import webdriver
import unittest
class Login(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Firefox()
cls.driver.maximize_window()
cls.driver.implicitly_wait(30)
cls.driver.get('https:///')
def test01(self):
'''验证⽹页title是否正确'''
self.assertEqual(self.driver.title,u'百度⼀下,你就知道')
def test02(self):
'''验证⽹址是否正确'''
self.assertEqual(self.driver.current_url, 'https:///')
@classmethod
def tearDownClass(cls):
cls.driver.quit()
@staticmethod
def suite():
suite=unittest.TestLoader.loadTestsFromTestCase(Login)
return suite
if __name__ == '__main__':
unittest.TextTestRunner(verbosity=2).run(Login.suite())
3、discover:在实际测试中,被测试的系统肯定是多个模块,然后⼀个模块中有N条⽤例,discover就是以递归的⽅式找到制定⽬录⾥的⽂件(要被执⾏的⽤例模块⽂件,⼀般习惯都是test01.py.x的命名格式),加载在测试套件中,进⾏运⾏
discover的三个参数:
start_dir=os.path.join(os.path.dirname(__file__)+'/test1'),要被测试的⽂件所在的⽬录,⼀般是⽤os.path拼接
pattern='test*.py',⽤正则表达式匹配测试⽤例的⽂件名,*表⽰⼀个或多个字符,'test*.py'表⽰以test开头的,中间N个字符,以.py结束的⽂件
top_level_dir=None 默认的
具体代码:
#!/usr/bin/env.python
#-*-coding:utf-8-*-
import unittest
import os
def suite():
suite=unittest.defaultTestLoader.discover(
start_dir=os.path.join(os.path.dirname(__file__)+'/test1'),
pattern='test*.py',
top_level_dir=None)
return suite
if __name__=='__main__':
unittest.TextTestRunner(verbosity=2).run(suite())
4、断⾔
assertEqual(a,b):a的值和b的值如果相等-->PASS
assertTrue(a) 表达式a的结果为真,则PASS
#!/usr/bin/env.python
#-*-coding:utf-8-*-
from selenium import webdriver
import unittest
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
class Login(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver=webdriver.Firefox()
cls.driver.maximize_window()
cls.driver.implicitly_wait(30)
cls.driver.get('https:///')
def test01(self):
'''验证⽹页title是否正确'''
self.assertTrue(self.driver.title.startswith('百度⼀下'))
def test02(self):
'''验证⽹址是否正确'''
self.assertEqual(self.driver.current_url,'https:///')
@classmethod
def tearDownClass(cls):
cls.driver.quit()
@staticmethod
def suite():
suite=unittest.makeSuite(Login)
return suite
if __name__=='__main__':
unittest.TextTestRunner(verbosity=2).run(Login.suite())
5、测试报告
5.1、使⽤对象file⽣成测试报告
fp=file(os.path.join(os.path.dirname(__file__)+'/TestReport.html'),'wb')
拆分解释:os.path.join(os.path.dirname(__file__)+'/TestReport.html')测试报告的路径(和名称),
'wb':以⼆进制的⽅式写⼊
5.2、测试执⾏runner写法如下:(测试报告需导⼊import HTMLTestRunner)
runner=HTMLTestRunner.HTMLTestRunner(
stream=fp,
title=u'报告的标题TestReport',
description=u'报告的描述'
)
HTMLTestRunner的三个参数:
stream=fp 指定测试报告是哪个⽂件
title=u'报告的标题TestReport',测试报告的标题
description=u'报告的描述'测试报告的描述
具体代码:
#!/usr/bin/env.python
#-*-coding:utf-8-*-
import os,sys
import unittest
import HTMLTestRunner
reload(sys)
sys.setdefaultencoding('utf-8')
def suite():
suite=unittest.defaultTestLoader.discover(
start_dir=os.path.join(os.path.dirname(__file__)+'/test1'),
pattern='test*.py',
top_level_dir=None)
return suite
if __name__=='__main__':
fp=file(os.path.join(os.path.dirname(__file__)+'/TestReport.html'),'wb')
runner=HTMLTestRunner.HTMLTestRunner(
stream=fp,
title=u'报告的标题TestReport',
description=u'报告的描述'
)
runner.run(suite())
测试报告:
右击在浏览器打开testReport.html,截图如下
注意:每个⽤例要测的是什么的描述必须要写,要不然测试报告中没有测试⽤例冒号:后的⽂字,就不知道哪个⽤例是测什么功能点的。

相关文档
最新文档