四. Common文件夾
Common 文件夾下存放著一些自定義的通用模塊,將一些可以復(fù)用的代碼進(jìn)行了簡單封裝,為框架提供工具支持,如下所示:
Common/
├── getPath.py
├── log.py
└── sendEmail.py
1. getPath.py
getPath.py 的功能是為框架獲取一些關(guān)鍵性路徑,如配置文件 config.ini 的存放路徑,最終測試報告的存放目錄等。內(nèi)容如下:
1 import os
2
3 class GetPath():
4 def __init__(self):
5 # 獲取當(dāng)前文件所在目錄
6 self.current_dir = os.path.split(os.path.realpath(__file__))[0]
7 # 獲取App文件夾路徑
8 self.app_dir = os.path.abspath(os.path.dirname(self.current_dir))
9 # 獲取項(xiàng)目的根目錄路徑
10 self.project_dir = os.path.abspath(os.path.dirname(self.app_dir))
11
12 def get_conf_path(self):
13 """
14 返回config.ini文件路徑
15 """
16 path = os.path.join(self.project_dir, 'config.ini')
17 return path
18
19 def get_log_path(self):
20 """
21 返回logs.txt文件路徑
22 """
23 path = os.path.join(self.project_dir, 'logs.txt')
24 return path
25
26
27 def get_case_dir(self):
28 """
29 返回測試用例文件存放目錄
30 """
31 dir = os.path.join(self.project_dir, 'TestCase')
32 return dir
33
34 def get_report_dir(self):
35 """
36 返回測試報告存放目錄
37 """
38 dir = os.path.join(self.project_dir, 'Report')
39 return dir
40
41
42 if __name__ == '__main__':
43 print(GetPath().get_conf_path())
44 print(GetPath().get_case_dir())
45 print(GetPath().get_report_dir())
46 print(GetPath().get_log_path())
運(yùn)行一下,命令行輸入:python getPath.py,結(jié)果如下:
>>> python getPath.py
/home/user1/ProgramFile/IntTestDemo/config.ini
/home/user1/ProgramFile/IntTestDemo/TestCase
/home/user1/ProgramFile/IntTestDemo/Report
/home/user1/ProgramFile/IntTestDemo/logs.txt
2. log.py
logging 是python的一個內(nèi)置模塊,該模塊定義的函數(shù)和類為應(yīng)用程序和庫的開發(fā)實(shí)現(xiàn)了一個靈活的事件日志系統(tǒng)。
log.py 用于配置日志的輸出格式以及日志文件的寫入,往后的使用中直接引入就可以了,例如:
import os
# 從Common包引入Log
from Common.log import Log
log = Log(__name__).getlog()
# 判斷用例文件是否存在
if not os.path.isfile(case_file_path):
log.error("測試用例文件不存在!")
log.py 文件內(nèi)容如下:
1 import os
2 import logging
3 from .getPath import GetPath
4
5
6 class Log(object):
7 def __init__(self, logger=None):
8 """
9 設(shè)置log并指定日志文件
10 """
11 # 創(chuàng)建一個logger
12 self.logger = logging.getLogger(logger)
13 self.logger.setLevel(logging.INFO)
14 # 指定日志路徑
15 self.log_path = GetPath().get_log_path()
16 # 創(chuàng)建一個handler,用于寫入日志
17 fh = logging.FileHandler(self.log_path, mode='a')
18 fh.setLevel(logging.DEBUG) # 輸出到日志的log等級為DEBUG
19 # 創(chuàng)建一個handler,用于輸出到控制臺
20 ch = logging.StreamHandler()
21 ch.setLevel(logging.WARNING)
22 # 定義兩個handler的輸出格式
23 formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
24 fh.setFormatter(formatter)
25 ch.setFormatter(formatter)
26 # 給logger添加hanler
27 self.logger.addHandler(fh)
28 self.logger.addHandler(ch)
29
30 def getlog(self):
31 return self.logger
32
33 if __name__ == '__main__':
34 log = Log(__name__).getlog()
35 log.error("測試用例文件不存在!")
回到上層目錄 IntTestDemo/App 下試運(yùn)行:
>>> python -m Common.log
2019-05-16 18:42:52,661 - log.py[line:35] - ERROR: 測試用例文件不存在!
程序會自動生成了日志文件 logs.txt,打開后可以看到已經(jīng)寫入了一條日志。
3. sendEmail.py
該模塊的作用是在測試完成后將測試報告發(fā)送給特定的用戶,我們留到后面再做敘述,暫時不必理會。
4. 別忘記推送代碼
>>> cd ../ # 回到根目錄IntTestDemo
>>> git add . # 將項(xiàng)目改動放入暫存區(qū)
>>> git commit -m # 將暫存區(qū)的的修改提交到當(dāng)前分支,m參數(shù)表示添加注釋
>>> git push origin master # 推送到遠(yuǎn)程服務(wù)器(github)