上干货 : token认证的接口
接口自动化涉及到的包 :request 框架:pytest
本节不用pymysql数据库进行数据校验了,此小段介绍一下思路
创建common工具存放,sprict脚本层面,api封装层面
1.1 未封装大概思路
import request
class Test_xiangmu:
def test_xiangmu1_ok(self)
url=""
heard={}
body={}
resp=request.post(url=url,heards=heard,json=body)
assert 200==resp.status.code
assert ==resp.json().get("")
def test_xiangmu12_ok(self)
url=""
heard={}
body={}
resp=request.post(url=url,heards=heard,json=body)
assert 200==resp.status.code
assert ==resp.json().get("")
那么,在我们进行接口自动化的时候整个项目的代码沉余度是非常大的,利用封装思想对接口进行优化。
1.2
import request
class API_xiangmu:
@classmethod
def api_xiangmu(cls,body)
url=""
head={}
resp=request.post(url=url,heards=head,json=body)
代码的封装思路:api层面,sprict脚本层,对api封装之后为在脚本层面减少沉余
1.3
class ASSERR_xiangmu:
@classmethod
def assert_xiangmu1(cls,resp,status.code,message)
assert status.code==resp.status.code
assert message==resp.json().age()
1.4
放入common用于接口关联
class TOKEN_xiangmu:
@classmethod
def get_token(cls)
.....
resp=request.post(........)
return resp.json().get("")
1.5
报告生成,allure形式所以这里改掉。
[pytest]
addopts = -s --alluredir report
1.6
import request
import allure
from API import API_xiangmu
from common import ASSERT
from common import TOKEN_xiangu1
class Test_xiangmu:
def upset(self)
self.tn=TOKEN_xiangu1.get_token()
@allure.step(tiele="#test_name")
@allure.severity(allure.severity_level.CRITICAL)#错误级别,报告方便查看
def test_xiangmu1_ok(self)
body={}
resp=API.api_xiangmu(body)
ASSERT.assert1(200,ok)
@allure.step(tiele="#test_name")
@allure.severity(allure.severity_level.MINOR)#错误级别,报告方便查看
def test_update_ok(self)
body={}
resp=API.api_xiangmu(body,self.tn)
ASSERT.assert1(200,resp,ok)
1.6
push到gitee里面结合jenkins持续集合。
这就是代码实现思想,总之接口测试,并非要用python来完成,按照个人习惯也可以使用其它平台来完成。