第一种
1.通过 HTMLTestRunner 模块输出报告
2.下载连接 http://tungwaiyip.info/software/HTMLTestRunner.html
3.将下载好的文件放到python安装目录的lib下面
生成测试报告实例代码
Myunittest.py
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #! user/bin/python 2 #---------------------------------- 3 ''' 4 代码说明: 5 编写日期: 6 设计 者: 7 ''' 8 #---------------------------------- 9 10 import unittest 11 from selenium import webdriver 12 import time 13 class Myunittest(unittest.TestCase): 14 def setUp(self): 15 self.driver = webdriver.Firefox() 16 time.sleep(2) 17 self.driver.get(url)# url自己填写 18 def tearDown(self): 19 self.driver.quit()
login_test.py
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #! user/bin/python 2 #---------------------------------- 3 ''' 4 代码说明: 5 编写日期: 6 设计 者: 7 ''' 8 #---------------------------------- 9 from selenium import webdriver 10 import unittest 11 import time 12 from BeautifulReport import BeautifulReport 13 from Myunittest import Myunittest 14 15 class LoginTest(Myunittest): 16 17 def test_login01(self): 18 self.driver.find_element_by_id('username').send_keys('rmln') 19 self.driver.find_element_by_id('password').send_keys('qwert1234!@#') 20 time.sleep(2) 21 self.driver.find_element_by_id('loginSubmitButton') 22 self.driver.save_screenshot('logon.png') 23 def test_login02(self): 24 self.driver.find_element_by_id('username').send_keys('rmln') 25 self.driver.find_element_by_id('password').send_keys('qwert1234!@#') 26 time.sleep(2) 27 self.driver.find_element_by_id('loginSubmitButton') 28 def test_login03(self): 29 self.driver.find_element_by_id('username').send_keys('rmln') 30 self.driver.find_element_by_id('password').send_keys('qwert1234!@#') 31 time.sleep(2) 32 self.driver.find_element_by_id('loginSubmitButton') 33 34 if __name__ == '__main__': 35 pass
runtest.py
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #! user/bin/python 2 #---------------------------------- 3 ''' 4 代码说明: 5 编写日期: 6 设计 者: 7 ''' 8 #---------------------------------- 9 import unittest 10 import HTMLTestRunner 11 import time 12 if __name__ == '__main__': 13 test_suite = unittest.defaultTestLoader.discover(r'D:\testselenium', pattern='login_test.py') 14 currTime = time.strftime('%Y-%m-%d %H_%M_%S') 15 filename = currTime+'.html' 16 fp = open(filename, 'wb') 17 runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title='Retail sys测试报告', 18 description='处理器:Intel(R) Core(TM) ' 19 'i5-6200U CPU @ 2030GHz 2.40 GHz ' 20 '内存:8G 系统类型: 64位 版本: windows 10 家庭中文版') 21 runner.run(test_suite)
测试报告样例
第二种
1.通过BeautifulReport 模块输出报告
2.下载连接 https://github.com/TesterlifeRaymond/BeautifulReport
3.将下载好的BeautifulReport包放到python安装目录的site-packages下面
生成测试报告实例代码
Myunittest.py
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #! user/bin/python 2 #---------------------------------- 3 ''' 4 代码说明: 5 编写日期: 6 设计 者: 7 ''' 8 #---------------------------------- 9 10 import unittest 11 from selenium import webdriver 12 import time 13 class Myunittest(unittest.TestCase): 14 def setUp(self): 15 self.driver = webdriver.Firefox() 16 time.sleep(2) 17 self.driver.get(url)# url 自己填写 18 def tearDown(self): 19 self.driver.quit()
login_test.py
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #! user/bin/python 2 #---------------------------------- 3 ''' 4 代码说明: 5 编写日期: 6 设计 者: 7 ''' 8 #---------------------------------- 9 import time 10 from BeautifulReport import BeautifulReport 11 from Myunittest import Myunittest 12 13 class LoginTest(Myunittest): 14 15 @BeautifulReport.add_test_img('login.png') 16 def test_login01(self): 17 self.driver.find_element_by_id('username').send_keys('rmln') 18 self.driver.find_element_by_id('password').send_keys('qwert1234!@#') 19 time.sleep(2) 20 self.driver.find_element_by_id('loginSubmitButton') 21 self.driver.save_screenshot('logon.png') 22 def test_login02(self): 23 self.driver.find_element_by_id('username').send_keys('rmln') 24 self.driver.find_element_by_id('password').send_keys('qwert1234!@#') 25 time.sleep(2) 26 self.driver.find_element_by_id('loginSubmitButton') 27 def test_login03(self): 28 self.driver.find_element_by_id('username').send_keys('rmln') 29 self.driver.find_element_by_id('password').send_keys('qwert1234!@#') 30 time.sleep(2) 31 self.driver.find_element_by_id('loginSubmitButton') 32 33 if __name__ == '__main__': 34 pass
runtest.py
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #! user/bin/python 2 #---------------------------------- 3 ''' 4 代码说明: 5 编写日期: 6 设计 者: 7 ''' 8 #---------------------------------- 9 from BeautifulReport import BeautifulReport 10 import unittest 11 import time 12 if __name__ == '__main__': 13 currTime = time.strftime('%Y-%m-%d %H_%M_%S') 14 filename = currTime+'.html' 15 test_suite = unittest.defaultTestLoader.discover(r'D:\testselenium', pattern='login_test.py') 16 result = BeautifulReport(test_suite) 17 result.report(filename=filename, description='测试deafult报告', log_path='.')
测试报告样例
最后一个报告还是挺漂亮的,可以考虑使用,我相信随着时间的推移,会有更多的好看的报告面试(只能把期望交给行业中的大牛了^-^)