1. 程式人生 > >python+selenium 輸出2種樣式的測試報告

python+selenium 輸出2種樣式的測試報告

第一種:

  1.通過 HTMLTestRunner 模組輸出報告

  3.將下載好的檔案放到python安裝目錄的lib下面

  4.生成測試報告例項程式碼

Myunittest.py

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

login_test.py

 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('[email protected]#') 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('[email protected]#') 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('[email protected]#') 31 time.sleep(2) 32 self.driver.find_element_by_id('loginSubmitButton') 33 34 if __name__ == '__main__': 35 pass
View Code

runtest.py

 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)
View Code

測試報告樣例:

第二種:

  1.通過BeautifulReport 模組輸出報告

  3.將下載好的BeautifulReport包放到python安裝目錄的site-packages下面

  4.生成測試報告例項程式碼

Myunittest.py

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

login_test.py

 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('[email protected]#')
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('[email protected]#')
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('[email protected]#')
30         time.sleep(2)
31         self.driver.find_element_by_id('loginSubmitButton')
32 
33 if __name__ == '__main__':
34     pass
View Code

runtest.py

 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='.')
View Code

測試報告樣例:

希望此篇文章能幫到初學者

原創文章編寫不易,轉載請註明出處