日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Requests Python-ddt數(shù)據(jù)驅(qū)動

 印度阿三17 2019-01-24

一、數(shù)據(jù)驅(qū)動

1.pip install ddt 安裝模塊

?

2.使用場景

(1)ddt一般是針對同一個接口,只是參數(shù)值不同,比如一個接口需要十組乃至更多組數(shù)據(jù),寫在腳本里顯然是不科學的,也不便于維護。

(2)ddt可與表格一起使用,從表格讀取出批量的測試數(shù)據(jù),作為參數(shù)依次傳入

?

3.案例

測試系統(tǒng)登錄功能,使用ddt模塊讀取測試數(shù)據(jù),測試數(shù)據(jù)存放在excel維護,實現(xiàn)登錄方法、測試登錄腳本以及測試數(shù)據(jù)分離

?

二、代碼

?common中的HTMLReport.py和read_excel.py為測試報告封裝和表格讀取數(shù)據(jù)

?

1.common.login_api.py

測試登錄方法(返回登錄結(jié)果中的msg信息,用于測試斷言)

# 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信息,測試腳本斷言


if __name__ == '__main__':
    l = Login()
    l.login("suner", "123456")

?

?2.case.test_login_api.py

測試登錄腳本

# 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__)))  # 返回當前文件的上上層目錄
excel_path = os.path.join(cur_path , "test_data/login_data.xlsx")  # 測試數(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}是在最終的測試報告中依次打印中測試數(shù)據(jù)
        """測試數(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í)行測試

import unittest
import os
import time
from common.HTMLReport import HTMLTestRunner

# 當前腳本所在文件真實路徑
cur_path = os.path.dirname(os.path.realpath(__file__))

def add_case(caseName="case", rule="test_*.py"):
    """第一步:加載所有測試用例"""
    case_path = os.path.join(cur_path, caseName)  # 用例文件夾
    # 文件夾不存在就創(chuàng)建一個文件夾
    if not os.path.exists(case_path): os.mkdir(case_path)

    # 定義discover加載所有測試用例
    # case_path:執(zhí)行用例的目錄;pattern:匹配腳本名稱的規(guī)則;top_level_dir:默認為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測試報告中"""
    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="自動化測試報告,測試結(jié)果如下:",
                                                  description="用例執(zhí)行情況")
    # 調(diào)用add_case函數(shù)
    runner.run(all_case)
    fp.close()

def get_report_file(report_path):
    """第三步:獲取最新的測試報告"""
    lists = os.listdir(report_path)
    lists.sort(key=lambda fn: os.path.getmtime(os.path.join(report_path, fn)))
    print("最新測試生成的報告:"   lists[-1])
    # 找到生成最新的報告文件
    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) # 生成報告

?

三、結(jié)果

1.控制臺運行結(jié)果

?

2.測試報告

?

來源:http://www./content-1-104301.html

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多