python自动化测试框架,封装方法方式
第一种:静态方法封装,接口调用入参定义一个(默认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