python自動化測試框架,封裝方法方式

happyyangyanghappy發表於2024-06-05

第一種:靜態方法封裝,介面呼叫入參定義一個(預設json),直接執行介面請求

介面封裝程式碼如下:

class OrderTransactionService:

    @staticmethod
    def getComboProductList(body):
        url = http_host + '/service?serialize=7'
        headers = {'Content-Type': 'application/json'}
        request_list = []
        request_list.append('com.ymm.insurance.request.ProductListRequestDirect')
        request_list.append(body)
        parm_json = CommonUtils.be_post_json(methodName='getComboProductList',
                                             url='http://service.ymm.com/insure-service-apply/orderTransactionService_1.0.0',
                                             request_list=request_list)
        response = httpUtil.Post(url, headers, parm_json)
        logger.info("=======getComboProductList的POST方式的入參是=====\n" + str(request_list))
        logger.info("=======getComboProductList的POST方式的返回值是=====\n" + response)
        response = json.loads(response)
        return response

測試用例方法呼叫如下:

# encoding:utf-8
import pytest
from settings import env
from base.insure.insure_service import OrderTransactionService
from settings import mysql
orderTransactionService = OrderTransactionService()
database = mysql.Database("fis")

@pytest.mark.doubleRead
class Test_getComboProductList:
    # 從資料庫隨機取一條資料
    json_dev =  {
        "cargoId": 10171239743346,
        "driverYmmUid": 965006065498560609,
        "driverHcbUid": 900345103,
        "shipperYmmUid": 965006065498913346,
        "shipperHcbUid": 1100297317,
        "appType": 1,
        "source": 1,
        "cargoSource": 1,
        "client": 1,
        "version": "10990000",
        "pluginVersion": "10.99.1.1",
        "payOrderPluginVersion": "10.99.1.284",
        "securityTran": 7,
        "startCode": 310101,
        "endCode": 270213,
        "firstCategoryCode": 10,
        "firstCategoryName": "食品飲料",
        "secondCategoryCode": 91,
        "secondCategoryName": "醬油"
    }
    json_qa = {
    "cargoId": 123659599419839,
    "driverYmmUid": 967933837394139781,
    "driverHcbUid": 215996505,
    "shipperYmmUid": 967933837002049839,
    "shipperHcbUid": 215801452,
    "appType": 1,
    "source": 1,
    "cargoSource": 1,
    "client": 1,
    "version": "8600700",
    "pluginVersion": "7.25.1.1",
    "payOrderPluginVersion": "8.58.141.380",
    "securityTran": 8,
    "startCode": 330382,
    "endCode": 320114,
    "firstCategoryCode": 3,
    "firstCategoryName": "服飾 紡織 皮革",
    "secondCategoryCode": 47,
    "secondCategoryName": "服裝",
    "userFlag": None,
    "platFormScene": None
  }
    if env.env == 'qa':
        json =json_qa
    else:
        json =json_dev
    def test_01(self):
        result = orderTransactionService.getComboProductList(self.json)

第二種:不是靜態方法,透過不定長字典引數(**kwargs),封裝介面

class freightCompensationClaimService:
    def autoReport(self, **kwargs):
        url = http_host + '/service?serialize=7'
        headers = {'Content-Type': 'application/json'}
        request_list = []
        request_list.append('com.ymm.insurance.dto.claim.crm.request.AutoReportClaimRequestDto')
        request_list.append(kwargs)
        parm_json = CommonUtils.be_post_json(methodName='autoReport',
                                             url='http://service.ymm.com/insure-service-apply/freightCompensationClaimService_1.0.0',
                                             request_list=request_list)
        response = httpUtil.Post(url, headers, parm_json)
        logger.info("=======autoReport的POST方式的入參是=====\n" + str(kwargs))
        print("=======autoReport的POST方式的返回值是=====\n" + response)
        response_json = json.loads(response)
        return response_json

測試用例方法呼叫如下:

from base.insure.insure_service import freightCompensationClaimService
from settings.mysql import Database

freightCompensationClaimService=freightCompensationClaimService()
dataBase = Database("fis")
dataBase_calim = Database("fis_claim")
poliNo = 0

# 太平洋運費損失險理賠
class Test_autoReport:
    def setup_class(self):
        self.poliNo = 'AGUZGDS59724EGGF73E4'
        self.claimAmount = 6000.01
        self.conferenceFreightRate = 8000
        self.receivedFreightRate = 1000
        self.truckLength = '6.0'
        self.driverLicenses =  ["https://dev-image56-conf-oss.ymm56.com/ymmfile/insure-service/1e15310d-f7b7-444d-a5ef-a11708bbe48a?Expires=1654166938&OSSAccessKeyId=LTAIq0WRi8jPwg5y&Signature=7PV0SGlFsqSuMvTPHQlYt2jw3eU%3D"]
        self.drivingLicenses = ["https://dev-image56-conf-oss.ymm56.com/ymmfile/insure-service/1e15310d-f7b7-444d-a5ef-a11708bbe48a?Expires=1654166938&OSSAccessKeyId=LTAIq0WRi8jPwg5y&Signature=7PV0SGlFsqSuMvTPHQlYt2jw3eU%3D"]
        self.invoices = ["https://dev-image56-conf-oss.ymm56.com/ymmfile/insure-service/1e15310d-f7b7-444d-a5ef-a11708bbe48a?Expires=1654166938&OSSAccessKeyId=LTAIq0WRi8jPwg5y&Signature=7PV0SGlFsqSuMvTPHQlYt2jw3eU%3D"]



    def test_01(self):
        response = freightCompensationClaimService.autoReport(policyNo= self.poliNo,claimAmount=self.claimAmount,conferenceFreightRate=self.conferenceFreightRate ,receivedFreightRate=self.receivedFreightRate ,truckLength=self.truckLength ,
            driverLicenses=self.driverLicenses,drivingLicenses=self.drivingLicenses,invoices=self.invoices )
        print(response["response"])
        assert response["response"]["success"] == True
        assert response["response"]["data"] != None

相關文章