接口自动化测试框架搭建(9、自动化测试case的编写)--python+HTMLTestRunnerCN+request+unittest+mock+db

 

目录

一、增加配置

二、创建excel用来存放测试用例

三、创建testCategoriesList.py文件


基于之前文章:

1、环境的搭建:https://blog.csdn.net/Mojitoice/article/details/108992622?spm=1001.2014.3001.5501

2、配置文件的创建,封装配置文件的读取方法:https://blog.csdn.net/Mojitoice/article/details/109031074

3、excel读取方法的封装:https://blog.csdn.net/Mojitoice/article/details/109203605?spm=1001.2014.3001.5501

4、url拼接方法的封装:https://blog.csdn.net/Mojitoice/article/details/114400695

5、封装get,post请求方法:https://blog.csdn.net/Mojitoice/article/details/114402767

6、进行mock的方法封装:https://blog.csdn.net/Mojitoice/article/details/114403473

7、操作数据库和日志方法封装:https://blog.csdn.net/Mojitoice/article/details/114404093

8、发送邮件方法的封装+HTMLTestRunnerCN:https://blog.csdn.net/Mojitoice/article/details/114533283

 

背景:

综合之前的文章,已经封装好了所有自动化case需要用到的文件,本次编写测试case.py,生成测试报告并发送。以例子的形式出发,我们再整体回顾下。

例如:https://gank.io/api/v2/categories/Article 接口,get的请求方法

一、增加配置

/Users/dongyue/Documents/framework/testFile/config.ini

[HTTP]
scheme = https    #本次新增配置
baseurl = boe-ms.byted.org
url = homed-inhouse.bytedance.net
productid_url = boe-ms.byted.org
url_test = gank.io   #本次新增配置
port = 8080 
timeout = 10.0

在配置文件中增加该接口徐需要mock的数据,我将返回参数中的status更改为了200


[mock_testCategoriesList]
data = {"data":[{"_id":"5e59ec146d359d60b476e621","coverImageUrl":"http://gank.io/images/b9f867a055134a8fa45ef8a321616210","desc":"Always deliver more than expected.\uff08Larry Page\uff09","title":"Android","type":"Android"},{"_id":"5e59ed0e6e851660b43ec6bb","coverImageUrl":"http://gank.io/images/d435eaad954849a5b28979dd3d2674c7","desc":"Innovation distinguishes between a leader and a follower.\uff08Steve Jobs\uff09","title":"\u82f9\u679c","type":"iOS"},{"_id":"5e5a25346e851660b43ec6bc","coverImageUrl":"http://gank.io/images/c1ce555daf954961a05a69e64892b2cc","desc":"The man who has made up his mind to win will never say \u201c Impossible\u201d\u3002\uff08 Napoleon \uff09","title":"Flutter","type":"Flutter"},{"_id":"5e5a254b6e851660b43ec6bd","coverImageUrl":"http://gank.io/images/4415653ca3b341be8c61fcbe8cd6c950","desc":"Education is a progressive discovery of our own ignorance. \uff08 W. Durant \uff09","title":"\u524d\u7aef","type":"frontend"},{"_id":"5e5a255c6e851660b43ec6be","coverImageUrl":"http://gank.io/images/964552b931d5470ea1506fc2b0f1cba9","desc":"If you do not learn to think when you are young, you may never learn. \uff08 Edison \uff09","title":"\u540e\u7aef","type":"backend"},{"_id":"5e5a25716e851660b43ec6bf","coverImageUrl":"http://gank.io/images/c3c7e64f0c0647e3a6453ccf909e9780","desc":"Do not, for one repulse, forgo the purpose that you resolved to effort. \uff08 Shakespeare \uff09","title":"APP","type":"app"}],"status":200}

 

二、创建excel用来存放测试用例

/Users/dongyue/Documents/framework/excel/CategoriesList.xlsx

caseNameapiPathapiNamemockmethodpurpose
分类列表_list/api/v2/categories/Article分类列表获取接口NGET100
分类列表_list2/api/v2/categories/Article分类列表获取接口YGET200

caseName:测试case的名称

apiPath:接口的path路径

apiName:接口的名称

mock:代表的是该case是否需要mock。Y代表需要,N代表不需要

method:该case的请求方法

purpose:期望返回值。该接口返回中的status为100 ,所以如果返回的是100,则证明该case通过,也就是符合期望值。

由表格可以看出来,该case需要进行mock,那我们就需要在配置文件中配置好mock的数据。

 

三、创建testCategoriesList.py文件

/Users/dongyue/Documents/framework/case/testCategoriesList.py

from testFile.getUrlParams import MakeRealmName
from testFile.readExcel import ReadExcel
import paramunittest
import unittest
from common.configHttp import Run_Main
import json
from common.log import logger
from common.configapiMock import Mock_Test


#读取excel中的测试case   readExcel.py封装的函数,需要传递入参数:excel_name,sheet_name,[1:]的意思是从下标为1往后开始。
readexcel = ReadExcel().getexcel("CategoriesList.xlsx","Sheet1")[1:]


@paramunittest.parametrized(*readexcel)  #参数化
class testCategoriesList(unittest.TestCase):

    def setParameters(self,caseName,apiPath,apiName,mock,method,purpose):  #测试case的属性
        self.caseName = caseName
        self.apiPath = apiPath
        self.apiName = apiName
        self.mock = mock
        self.method = method
        self.purpose = purpose
        self.url = MakeRealmName().make_url("url_test") + self.apiPath #getUrlParams.py中需要传递入参name。指的是在config.ini中配置好的域名


    def setUp(self) -> None:
        logger.info(self.caseName + "......测试开始......")

    def tearDown(self) -> None:
        logger.info(self.caseName + "......测试结束......")

    def description(self):
        self.caseName

    def testcategorieslist(self):
        #如果case需要mock,就掉用configapiMock中封装好的函数
        if self.caseismock():
            resp = Mock_Test().mocktest(mock_method=Run_Main().run_main, method=self.method,url=self.url)
            self.check_result(resp)
        
        #否则,就正常发送请求,判断返回值
        else:
            resp = Run_Main().run_main(self.url,self.method)  #configHttp.py中的函数,需要传url和method等参数
            self.check_result(resp)


    def caseismock(self):
        #判断该case是否需要mock
        if Mock_Test().ismock("mock_testCategoriesList"):     #判断config.ini中是否存在配置好的mock数据
            if self.mock == "Y":                              #判断从excel中获取到的数据是否需要mock
                logger.info(self.caseName + "mock该case")
                return True
            else:
                logger.info(self.caseName + "该case不需要mock")
                return False
        else:
            logger.info(self.caseName + "不存在mock数据")


    def check_result(self,response):
        info = json.loads(response)  #格式化返回值
        #判断返回值中的status是否等于预期中的值
        self.assertEqual(info["status"], int(self.purpose))
        logger.info("status=%s" % int(self.purpose) + "(期望值|实际值)%s" % info["status"])


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

 

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值