python+selenium 输出2种样式的测试报告

时间:2021-12-19 18:08:06

第一种

1.通过 HTMLTestRunner 模块输出报告

2.下载连接 http://tungwaiyip.info/software/HTMLTestRunner.html

3.将下载好的文件放到python安装目录的lib下面

生成测试报告实例代码

Myunittest.py

 #! user/bin/python
#----------------------------------
'''
代码说明:
编写日期:
设计 者:
'''
#---------------------------------- import unittest
from selenium import webdriver
import time
class Myunittest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
time.sleep(2)
self.driver.get(url)# url自己填写
def tearDown(self):
self.driver.quit()

login_test.py

 #! user/bin/python
#----------------------------------
'''
代码说明:
编写日期:
设计 者:
'''
#----------------------------------
from selenium import webdriver
import unittest
import time
from BeautifulReport import BeautifulReport
from Myunittest import Myunittest class LoginTest(Myunittest): def test_login01(self):
self.driver.find_element_by_id('username').send_keys('rmln')
self.driver.find_element_by_id('password').send_keys('qwert1234!@#')
time.sleep(2)
self.driver.find_element_by_id('loginSubmitButton')
self.driver.save_screenshot('logon.png')
def test_login02(self):
self.driver.find_element_by_id('username').send_keys('rmln')
self.driver.find_element_by_id('password').send_keys('qwert1234!@#')
time.sleep(2)
self.driver.find_element_by_id('loginSubmitButton')
def test_login03(self):
self.driver.find_element_by_id('username').send_keys('rmln')
self.driver.find_element_by_id('password').send_keys('qwert1234!@#')
time.sleep(2)
self.driver.find_element_by_id('loginSubmitButton') if __name__ == '__main__':
pass

runtest.py

 #! user/bin/python
#----------------------------------
'''
代码说明:
编写日期:
设计 者:
'''
#----------------------------------
import unittest
import HTMLTestRunner
import time
if __name__ == '__main__':
test_suite = unittest.defaultTestLoader.discover(r'D:\testselenium', pattern='login_test.py')
currTime = time.strftime('%Y-%m-%d %H_%M_%S')
filename = currTime+'.html'
fp = open(filename, 'wb')
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title='Retail sys测试报告',
description='处理器:Intel(R) Core(TM) '
'i5-6200U CPU @ 2030GHz 2.40 GHz '
'内存:8G 系统类型: 64位 版本: windows 10 家庭中文版')
runner.run(test_suite)

测试报告样例

python+selenium 输出2种样式的测试报告

第二种

1.通过BeautifulReport 模块输出报告

2.下载连接 https://github.com/TesterlifeRaymond/BeautifulReport

3.将下载好的BeautifulReport包放到python安装目录的site-packages下面

生成测试报告实例代码

Myunittest.py

 #! user/bin/python
#----------------------------------
'''
代码说明:
编写日期:
设计 者:
'''
#---------------------------------- import unittest
from selenium import webdriver
import time
class Myunittest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
time.sleep(2)
self.driver.get(url)# url 自己填写
def tearDown(self):
self.driver.quit()

login_test.py

 #! user/bin/python
#----------------------------------
'''
代码说明:
编写日期:
设计 者:
'''
#----------------------------------
import time
from BeautifulReport import BeautifulReport
from Myunittest import Myunittest class LoginTest(Myunittest): @BeautifulReport.add_test_img('login.png')
def test_login01(self):
self.driver.find_element_by_id('username').send_keys('rmln')
self.driver.find_element_by_id('password').send_keys('qwert1234!@#')
time.sleep(2)
self.driver.find_element_by_id('loginSubmitButton')
self.driver.save_screenshot('logon.png')
def test_login02(self):
self.driver.find_element_by_id('username').send_keys('rmln')
self.driver.find_element_by_id('password').send_keys('qwert1234!@#')
time.sleep(2)
self.driver.find_element_by_id('loginSubmitButton')
def test_login03(self):
self.driver.find_element_by_id('username').send_keys('rmln')
self.driver.find_element_by_id('password').send_keys('qwert1234!@#')
time.sleep(2)
self.driver.find_element_by_id('loginSubmitButton') if __name__ == '__main__':
pass

runtest.py

 #! user/bin/python
#----------------------------------
'''
代码说明:
编写日期:
设计 者:
'''
#----------------------------------
from BeautifulReport import BeautifulReport
import unittest
import time
if __name__ == '__main__':
currTime = time.strftime('%Y-%m-%d %H_%M_%S')
filename = currTime+'.html'
test_suite = unittest.defaultTestLoader.discover(r'D:\testselenium', pattern='login_test.py')
result = BeautifulReport(test_suite)
result.report(filename=filename, description='测试deafult报告', log_path='.')

测试报告样例

python+selenium 输出2种样式的测试报告

最后一个报告还是挺漂亮的,可以考虑使用,我相信随着时间的推移,会有更多的好看的报告面试(只能把期望交给行业中的大牛了^-^)