此框架於2018年10月24日重新編輯
大更新,項目名稱"HwUnittestFrameworkPy2"
地址:https://github.com/hongweifuture/HwUnittestFrameworkPy2
基礎
特點
- python2語言,基於unittest
- 由ini改爲yaml文件參數化配置,實現配置與代碼分離
- 支持xlsx、csv、xml等格式數據驅動,實現數據與代碼分離
- log日誌、report報告記錄、生成和存儲
- 測試用例模板化,易編寫
- 自定義郵件發送測試結果
- 支持Selenium和Request套用框架
目錄結構
文件/文件夾 | 說明 |
---|---|
startup.py | 啓動程序 |
cases | 測試用例 |
config | 配置文件 |
data | 數據驅動 |
driver | 驅動文件 |
log | 日誌存放 |
report | 報告留存 |
result | 測試結果 |
screenshot | 過程截圖 |
source | 封裝的庫類 |
使用方法
安裝python2
安裝PyYaml模塊
pip install PyYaml
編寫用例
模板
框架調用測試用例,無論是手動添加suite還是discover自動識別測試用例,main都不會被調用,只是在脫離框子單獨測試用例的時候會用到,模板使用爲手動
#!/usr/bin/python
# coding=utf-8
import unittest
class TestModule(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_Photo(self):
pass
if __name__ == "__main__":
suite = unittest.TestSuite()
suite.addTest(TestModule("test_Photo"))
unittest.TextTestRunner().run(suite)
範例
#!/usr/bin/python
# coding=utf-8
# Created by zhaohongwei on 2018-10-22
# Blog: http://blog.csdn.net/z_johnny
import unittest
class WebTest(unittest.TestCase):
'''Web Test'''
def setUp(self):
print 'one'
def tearDown(self):
'''If setUp() succeeded, the tearDown() method will be run whether runTest() succeeded or not.'''
print 'two'
def test_Case1(self):
self.assertEqual(1, 1, "Error")
def test_Case2(self):
self.assertNotEquals(2, 2, "Equals")
class APITest(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_Case1(self):
print u'\n 1 不等於 2'
self.assertEqual(1,2,"1 != 2")
def test_Case2(self):
self.assertEqual(3,3,"This's ok")
def test_Case3(self):
print 'APITest.test_Case3'
self.assertEqual(5,6,'ERROR')
def test_Case4(self):
print "This's ok"
self.assertEqual(3,3,"This's ok")
if __name__ == "__main__":
suite = unittest.TestSuite()
suite.addTest(WebTest("test_Case1"))
suite.addTest(APITest("test_Case2"))
unittest.TextTestRunner().run(suite)
技巧1 用例添加log,引入HwLogging
HwLogging模塊打包了logging的功能,配置參數化,輸出格式化,使用方法同logging
#from HwLogging import HwLogging
from source.HwLogging import HwLogging
self.logging = HwLogging().outputLog()
self.logging.debug()
self.logging.info()
self.logging.warning()
self.logging.error()
self.logging.critical()
格式化輸出
2018-10-24 10:37:11,440 - HwUnittest.py : INFO -
2018-10-24 10:37:11,440 - HwUnittest.py : INFO The testcase number is : 3
2018-10-24 10:37:11,440 - HwUnittest.py : INFO The testcase name is : ANDROID.py
2018-10-24 10:37:11,440 - HwUnittest.py : INFO The testcase name is : Jenkins.py
2018-10-24 10:37:11,440 - HwUnittest.py : INFO The testcase name is : Web_API.py
2018-10-24 10:37:11,457 - ANDROID.py : WARNING 123456789
2018-10-24 10:37:11,457 - ANDROID.py : INFO Test Down
2018-10-24 10:37:11,459 - ANDROID.py : WARNING 123456789
2018-10-24 10:37:11,459 - ANDROID.py : ERROR This is error message
2018-10-24 10:37:11,459 - ANDROID.py : INFO Test Down
2018-10-24 10:37:11,460 - ANDROID.py : WARNING 123456789
2018-10-24 10:37:11,460 - ANDROID.py : INFO Test Down
2018-10-24 10:37:16,562 - HwCutfile.py : INFO Has been cut the file is 目前最新版本2018_10_24_Wednesday_10_37_11.html
技巧2 輸出報告文件 可選中文或者英文
使用unittest,生成html報告,由於使用在線Bootstrap,需要聯網,離線模式排版不美觀
suite = unittest.TestSuite()
# HwStartup(suite, 'CN') , you must choose
# you must choose CN or EN,it's report language
HwStartup(suite, 'CN').startup()
#HwStartup(suite, 'EN').startup()
技巧3 發送郵件
HwSendEmail打包smtp郵件,默認發送result文件夾附件,只需傳送郵件標題和內容即可
# send email , you can choose
ISOTIMEFORMAT='_%Y-%m-%d_%A'
current_time =str(time.strftime(ISOTIMEFORMAT))
email_tiltle = 'johnny test'+'%s'%current_time # as johnny test_2016-06-20_Monday
email_content = u'本輪自動化測試完成,詳情請查看附件,本郵件是程序自動發送,請勿回覆!'
HwSendEmail(email_tiltle,email_content).sendemail()
技巧4 測試版本留存
可配置source和destination文件夾,對文件進行移動並留存
# HwFileMove() move result to report , you can choose
HwFileMove().moveFile()
技巧5 刪除pyc
調試腳本過程中會出現pyc,可選進行刪除
# delde pyc , you can choose
HwCleanPyc().cleanPyc()
bug
沒有打印信息的測試用例,html報告無法收起
extend
- 加入數據驅動,如excel
- 加入截圖功能
- 加入統計圖
相關模塊
Python smtplib發送郵件 包含文本、附件、圖片等
本框架已經替換yaml配置文件,不過ConfigParser也是很好的
python ConfigParser 參數化配置 學習筆記()