unittest 管理接口用例(數據分離-讀取excel)


1、公共模塊

---> login.xls

"""
common (package)
    ---> ReadFile.py
"""

import xlrd

class ReadExcel():
    def __init__(self,file_path,sheetx):
        self.book = xlrd.open_workbook(file_path)
        self.sheet = self.book.sheet_by_index(sheetx)

    def getValue(self,row_index,col_index):
        """
        獲取excel單元格數據
        """
        return self.sheet.cell_value(row_index,col_index)

    def getCols(self):
        """
        獲取有效行數
        """
        return self.sheet.ncols

    def getRows(self):
        """
        獲取有效列數
        """
        return self.sheet.nrows
"""
common (package)
    ---> WriteFile.py
"""

import xlrd

from xlutils.copy import copy

def WriteExcel(filepath,sheetx,rowx,colx,value):
    """
    excel寫入操作
    """
    book = xlrd.open_workbook(filepath)
    newbook = copy(book)
    sheet = newbook.get_sheet(sheetx)
    sheet.write(rowx,colx,value)
    newbook.save(filepath)
"""
common (package)
    ---> var.py
"""
"""
excel用例模塊等變量
"""
url = 2
Method = 3
header = 4
body = 5
test_response = 7
result = 8
ok = "成功"
fail = "失敗"

 

2、輸出testcase如下

import requests
import unittest
import HTMLTestRunner
import json
from API_test.common.ReadFile import ReadExcel
from API_test.common import var
from API_test.common.WriteFile import WriteExcel

class TestLogin(unittest.TestCase):

    def setUp(self):
        self.url = ReadExcel("D:\work_doc\CodeFile\API_test\common\login.xls", 0).getValue(1, var.url)
        self.headers = json.loads(ReadExcel("D:\work_doc\CodeFile\API_test\common\login.xls", 0).getValue(1, var.header))

    def test01(self):
        """
        正向用例
        """
        payload = json.loads(ReadExcel("D:\work_doc\CodeFile\API_test\common\login.xls",0).getValue(1,var.body))
        response = requests.request("POST", self.url, headers=self.headers, json = payload)
        # 調用 WriteExcel 公共方法,將返回的報文實際結果寫入到 excel 中
        WriteExcel("D:\work_doc\CodeFile\API_test\common\login.xls",0,1,var.test_response,response.text)
        # 斷言
        try:
            self.assertEqual(response.json()["msg"],"成功",msg="test01 error")
        except:
            # 調用 WriteExcel 公共方法,將結論寫入到 excel 中
            WriteExcel("D:\work_doc\CodeFile\API_test\common\login.xls",0,1,var.result,var.fail)
            # 再次斷言生成測試報告,避免 try 異常處理將異常用例 pass 掉
            self.assertEqual(response.json()["msg"], "成功", msg="test01 error")
        else:
            WriteExcel("D:\work_doc\CodeFile\API_test\common\login.xls",0,1,var.result,var.ok)

    def test02(self):
        """
        賬號錯誤
        """
        payload = json.loads(ReadExcel("D:\work_doc\CodeFile\API_test\common\login.xls",0).getValue(2,var.body))
        response = requests.request("POST", self.url, headers=self.headers, json = payload)
        WriteExcel("D:\work_doc\CodeFile\API_test\common\login.xls",0,2,var.test_response,response.text)
        try:
            self.assertEqual(response.json()["msg"],"用戶不存在",msg="test02 error")
        except:
            WriteExcel("D:\work_doc\CodeFile\API_test\common\login.xls", 0, 2, var.result, var.fail)
            self.assertEqual(response.json()["msg"],"用戶不存在",msg="test02 error")
        else:
            WriteExcel("D:\work_doc\CodeFile\API_test\common\login.xls",0,2,var.result,var.ok)

    def test03(self):
        """
        密碼錯誤
        """
        payload = json.loads(ReadExcel("D:\work_doc\CodeFile\API_test\common\login.xls",0).getValue(3,var.body))
        response = requests.request("POST", self.url, headers=self.headers, json = payload)
        WriteExcel("D:\work_doc\CodeFile\API_test\common\login.xls",0,3,var.test_response, response.text)
        try:
            self.assertEqual(response.json()["msg"],"解密密碼錯誤",msg="test03 error")
        except:
            WriteExcel("D:\work_doc\CodeFile\API_test\common\login.xls", 0, 3, var.result, var.fail)
            self.assertEqual(response.json()["msg"],"解密密碼錯誤",msg="test03 error")
        else:
            WriteExcel("D:\work_doc\CodeFile\API_test\common\login.xls",0,3,var.result,var.ok)

if __name__ == '__main__':
    suite = unittest.TestSuite()
    testcase = [TestLogin("test01"),TestLogin("test02"),TestLogin("test03")]
    suite.addTests(testcase)
    reportfile = open("D:\work_doc\CodeFile\API_test\\report\\testreport.html", "wb")
    runner = HTMLTestRunner.HTMLTestRunner(stream=reportfile,title="TestReport",description="測試結果")
    runner.run(suite)
    reportfile.close()

 

3、循環讀取excel文件內的參數

from python_API.common.ReadExcel import ReadExcel
import requests
import json
import unittest

class Test(unittest.TestCase):

    def setUp(self):
        self.url = ReadExcel("d:\\dym.xls","Sheet1").getValue(1,1)
        self.Method = ReadExcel("d:\\dym.xls","Sheet1").getValue(1,2)
        self.header = json.loads(ReadExcel("d:\\dym.xls","Sheet1").getValue(1,3))

    def test01(self):
        
        #調用讀取excel類中的獲取行數方法getRows(),獲取有效行數
        for row in range(ReadExcel("d:\\dym.xls","Sheet1").getRows()):
            
            #因為第一行為標題,所以row為0時不能用來取值
            if row >=1:
                body = json.loads(ReadExcel("d:\\dym.xls","Sheet1").getValue(row,4))
                response = requests.request(self.Method,self.url,headers=self.header,params=body)
                if row == 1:
                    if response.json()["executeStatus"] == 0:
                        pass
                    else:
                        print ("case01 error!")
                else:
                    if response.json()["executeStatus"] == 1:
                        pass
                    else:
                        print ("case02 error!")

if __name__ == '__main__':
    unittest.main()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM