|
一、數(shù)據(jù)驅(qū)動(dòng) 1.pip install ddt 安裝模塊 ? 2.使用場(chǎng)景 (1)ddt一般是針對(duì)同一個(gè)接口,只是參數(shù)值不同,比如一個(gè)接口需要十組乃至更多組數(shù)據(jù),寫在腳本里顯然是不科學(xué)的,也不便于維護(hù)。 (2)ddt可與表格一起使用,從表格讀取出批量的測(cè)試數(shù)據(jù),作為參數(shù)依次傳入 ? 3.案例 測(cè)試系統(tǒng)登錄功能,使用ddt模塊讀取測(cè)試數(shù)據(jù),測(cè)試數(shù)據(jù)存放在excel維護(hù),實(shí)現(xiàn)登錄方法、測(cè)試登錄腳本以及測(cè)試數(shù)據(jù)分離 ? 二、代碼 ?common中的HTMLReport.py和read_excel.py為測(cè)試報(bào)告封裝和表格讀取數(shù)據(jù) ? 1.common.login_api.py 測(cè)試登錄方法(返回登錄結(jié)果中的msg信息,用于測(cè)試斷言)
# coding:utf-8
import requests
class Login():
def __init__(self):
self.s = requests.session()
self.headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36",
"Origin":"http://192.168.1.9:8080"
}
self.login_url = "http://192.168.1.9:8080/SSOAuth?action=login&code=undefined&gotoURL=http://192.168.1.9:8080/portal/geoindex.do"
def login(self, userAccount, pwd):
data = {"userAccount": userAccount, "pwd": pwd}
body = {
"data":'%s'?ta
}
r1 = self.s.post(self.login_url,headers=self.headers,data=body,verify=False)
return r1.json()["msg"] # 返回msg信息,測(cè)試腳本斷言
if __name__ == '__main__':
l = Login()
l.login("suner", "123456")
? ?2.case.test_login_api.py 測(cè)試登錄腳本
# coding:utf-8
import os
import ddt
import unittest
from common.login_api import Login
from common.read_excel import ExcelUtil
cur_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) # 返回當(dāng)前文件的上上層目錄
excel_path = os.path.join(cur_path , "test_data/login_data.xlsx") # 測(cè)試數(shù)據(jù)login_data.xlsx文件路徑
# 調(diào)用封裝好的讀取表格方法,取出表格中的數(shù)據(jù)
instance_excel = ExcelUtil(excel_path , "Sheet1")
da = instance_excel.dict_data()
# print(da)
@ddt.ddt
class TestLogin(unittest.TestCase):
def setUp(self):
self.lo = Login()
@ddt.data(*da)
def test_login(self, data): # {0}是在最終的測(cè)試報(bào)告中依次打印中測(cè)試數(shù)據(jù)
"""測(cè)試數(shù)據(jù):{0}"""
userAccount = data["userAccount"]
pwd = data["pwd"]
exp = data["exp"]
res = self.lo.login(userAccount, pwd)
self.assertEqual(exp, res)
if __name__ == '__main__':
unittest.main()
3.run_main.py 執(zhí)行測(cè)試
import unittest
import os
import time
from common.HTMLReport import HTMLTestRunner
# 當(dāng)前腳本所在文件真實(shí)路徑
cur_path = os.path.dirname(os.path.realpath(__file__))
def add_case(caseName="case", rule="test_*.py"):
"""第一步:加載所有測(cè)試用例"""
case_path = os.path.join(cur_path, caseName) # 用例文件夾
# 文件夾不存在就創(chuàng)建一個(gè)文件夾
if not os.path.exists(case_path): os.mkdir(case_path)
# 定義discover加載所有測(cè)試用例
# case_path:執(zhí)行用例的目錄;pattern:匹配腳本名稱的規(guī)則;top_level_dir:默認(rèn)為None
discover = unittest.defaultTestLoader.discover(case_path, pattern=rule, top_level_dir=None)
return discover
def run_case(all_case, reportName="report"):
"""第二步:執(zhí)行所有的用例,并把結(jié)果寫入到html測(cè)試報(bào)告中"""
now = time.strftime("%Y_%m_%d_%H_%M_%S")
report_path = os.path.join(cur_path, reportName)
if not os.path.exists(report_path): os.mkdir(report_path)
report_abspath = os.path.join(report_path, now "result.html")
print("report path:%s" % report_abspath)
fp = open(report_abspath, "wb")
runner = HTMLTestRunner(stream=fp, title="自動(dòng)化測(cè)試報(bào)告,測(cè)試結(jié)果如下:",
description="用例執(zhí)行情況")
# 調(diào)用add_case函數(shù)
runner.run(all_case)
fp.close()
def get_report_file(report_path):
"""第三步:獲取最新的測(cè)試報(bào)告"""
lists = os.listdir(report_path)
lists.sort(key=lambda fn: os.path.getmtime(os.path.join(report_path, fn)))
print("最新測(cè)試生成的報(bào)告:" lists[-1])
# 找到生成最新的報(bào)告文件
report_file = os.path.join(report_path, lists[-1])
return report_file
if __name__ == '__main__':
all_case = add_case() # 加載用例
run_case(all_case) # 執(zhí)行用例
report_path = os.path.join(cur_path, "report")
report_file = get_report_file(report_path) # 生成報(bào)告
? 三、結(jié)果 1.控制臺(tái)運(yùn)行結(jié)果 ? 2.測(cè)試報(bào)告 ? 來(lái)源:http://www./content-1-104301.html |
|
|