unittest之二makeSuite\testload\discover及测试报告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://www.baidu.com/')
def test01(self):
'''验证网页title是否正确'''
self.assertTrue(str(self.driver.title).startswith('百度一下'))
def test02(self):
'''验证网址是否正确'''
self.assertEqual(self.driver.current_url, 'https://www.baidu.com/')
@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://www.baidu.com/')
def test01(self):
'''验证网页title是否正确'''
self.assertEqual(self.driver.title,u'百度一下,你就知道')
def test02(self):
'''验证网址是否正确'''
self.assertEqual(self.driver.current_url, 'https://www.baidu.com/')
@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://www.baidu.com/')
def test01(self):
'''验证网页title是否正确'''
self.assertTrue(self.driver.title.startswith('百度一下'))
def test02(self):
'''验证网址是否正确'''
self.assertEqual(self.driver.current_url,'https://www.baidu.com/')
@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,截图如下
注意:每个用例要测的是什么的描述必须要写,要不然测试报告中没有测试用例冒号:后的文字,就不知道哪个用例是测什么功能点的