广东省网站备案查询,wordpress免费模板怎么使用,南京h5 网站建设,网站发展历程目录#xff1a;导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结#xff08;尾部小惊喜#xff09; 前言
在PC端登录公司的… 目录导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结尾部小惊喜 前言
在PC端登录公司的后台管理系统或在手机上登录某个APP时经常会发现登录成功后返回参数中会包含token它的值为一段较长的字符串而后续去请求的请求头中都需要带上这个token作为参数否则就提示需要先登录。
什么是token
token 由服务端产生是客户端用于请求的身份令牌。第一次登录成功时服务端会生成一个包含用户信息的加密字符串token返回给客户端并保存在本地后续客户端只需要带上token进行请求即可无需带上用户名密码。
token原理简单概括如下 用户首次登录成功后服务端会生成一个token值服务端会将它保存保存在数据库中同时也会将它返回给客户端
客户端拿到token值后保存在本地
后续客户端再次发送除登录外的其他请求时会把保存在本地的token值作为参数一起发送给服务端
服务端收到客户端的请求后会拿发送过来的token值与保存在数据库中的token值进行比较 如果两个token值相同 则说明当前用户处于登录状态 如果数据库中没有这个token值或者token值已经生效则需用户重新登录。
token场景处理
公司某管理后台系统登录后返回token接着去请求其他接口时请求头中都需要加上这个token否则提示请先登录。
请求该系统的登录接口如下
import requests
import jsonheaders {Content-Type: application/json;charsetutf8}
url http://127.0.0.1:5000/login
_data {username: 刘德华,password: 123456
}
res requests.post(urlurl, headersheaders, json_data).text
print(res)结果如下
{code: 1000, msg: 登录成功, token: sh34ljjl08s32730djsh34ljjl08s32730djsh34ljjl08s32730djsh34ljjl08s32730djsh34ljjl08s32730djsh34ljjl08s32730dj
}在对项目做接口自动化测试时需要先请求登录接口拿到token再去请求别的接口。每次请求其他接口时先请求一次登录接口这样做虽然可行但这样不仅会降低自动化的执行效率而且每次都请求登录也会对服务器资源造成浪费。
两种处理思路
思路1
在执行用例之前先请求登录接口并将返回的token值存储在文件中(如yaml文件)后续请求需要用到token值则从该文件。
python中yaml文件的读写请参考我之前的文章Python读写yaml文件使用PyYAML库。
1、运行接口自动化测试框架初始化时先请求登录接口获取token值并写入指定的yaml文件中。
import requests
import json
import yamldef get_token():请求登录接口获取token:return:headers {Content-Type: application/json;charsetutf8}url http://127.0.0.1:5000/login_data {username: 刘德华,password: 123456}res requests.post(urlurl, headersheaders, json_data).textres json.loads(res)token res[token]return tokendef write_yaml(token):写入yaml文件:return:t_data {token: token}with open(yaml文件路径, w, encodingutf-8) as f:yaml.dump(datat_data, streamf, allow_unicodeTrue)if __name__ __main__:token get_token() # 获取tokenwrite_yaml(token) # 将token值写入yaml文件2、执行测试用例时先读取yaml文件中token值并将token加入headers中(也有些是将token放在请求参数中视被测试项目具体情况而定)再发送请求。
import requests
import yaml
import pytest
import jsondef read_yaml():读yaml文件:return:with open(yaml文件路径, r, encodingutf-8) as f:result yaml.load(f.read(), Loaderyaml.FullLoader)token result[token]return tokendef test_check_user():查询个人信息需要先登录系统:return:# 先从yaml文件中读取tokentoken read_yaml()# 再将token添加到请求头中headers {Content-Type: application/json;charsetutf8,token: token}url http://127.0.0.1:5000/users/3res requests.get(urlurl, headersheaders).text# 返回结果为json格式转换为字典res json.loads(res)# 断言code是否为1000assert res[code] 1000if __name__ __main__:pytest.main()这里仅仅只是举例说明而在实际的框架中我们需要把这些诸如yaml文件的读写这样的函数单独封装在某个模块中供其他模块调用这样会代码会更加清晰简洁。
思路2
利用pytest中的Fixture函数作用域设置为session并返回token值后续测试方法/函数调用该Fixture函数。
pytest中Fixture的使用请参考我之前的文章pytest(6)-Fixture(固件)。
1、首先在conftest中定义一个作用域为session的Fixture函数用于请求登录接口返回token。
import pytest
import requests
import jsonpytest.fixture(scopesession)
def get_token_fixture():作用域为session的fixture函数返回token:return:headers {Content-Type: application/json;charsetutf8}url http://127.0.0.1:5000/login_data {username: 刘德华,password: 123456}res requests.post(urlurl, headersheaders, json_data).textres json.loads(res)token res[token]return token2、接着测试用例调用该Fixture。
def test_check_user(get_token_fixture):查询个人信息需要先登录系统:return:# 通过Fixture函数g获取et_token_fixture值即token再将token添加到请求头中headers {Content-Type: application/json;charsetutf8,token: get_token_fixture}url http://127.0.0.1:5000/users/3res requests.get(urlurl, headersheaders).textres json.loads(res)print(res)print(headers)assert res[code] 1000if __name__ __main__:pytest.main()执行测试用例结果如下 相对于Session/Cookies来说请求量较大或者涉及第三方接口的系统使用token更适合。
有些项目token是放在请求头中发送的而有一些项目则是放在请求参数里发送的做接口自动化时要明确是哪种方式。
接口自动化处理token时这两种思路可任选一种如果使用pytest框架的话建议尝试思路2。
下面是我整理的2023年最全的软件测试工程师学习知识架构体系图
一、Python编程入门到精通 二、接口自动化项目实战 三、Web自动化项目实战 四、App自动化项目实战 五、一线大厂简历 六、测试开发DevOps体系 七、常用自动化测试工具 八、JMeter性能测试 九、总结尾部小惊喜
如风驰骋势不可挡如光照耀闪耀夺目。奋斗是生命的节奏奋斗是梦想的灵魂。不畏困难不言放弃用行动诠释坚韧与勇敢创造自己的辉煌传世勇往直前奋斗不止
拥抱挑战超越极限放飞心灵创造奇迹。奋斗的路上或许辛酸但信念与毅力将铸就辉煌。勇敢追求梦想扬帆远航你将书写属于自己的壮丽篇章
扬起帆追逐未来的风。奋斗不止于争取成功更是赋予生命无限可能的旅程。相信自己的能力燃烧内心的激情用汗水和努力铸就辉煌人生。踏上征程创造属于自己的壮丽传奇奋斗不息梦想绽放 文章转载自: http://www.morning.ndzhl.cn.gov.cn.ndzhl.cn http://www.morning.xkyst.cn.gov.cn.xkyst.cn http://www.morning.hghhy.cn.gov.cn.hghhy.cn http://www.morning.gqcd.cn.gov.cn.gqcd.cn http://www.morning.nhrkl.cn.gov.cn.nhrkl.cn http://www.morning.dzgmj.cn.gov.cn.dzgmj.cn http://www.morning.vuref.cn.gov.cn.vuref.cn http://www.morning.knlyl.cn.gov.cn.knlyl.cn http://www.morning.qsfys.cn.gov.cn.qsfys.cn http://www.morning.zlzpz.cn.gov.cn.zlzpz.cn http://www.morning.rwmq.cn.gov.cn.rwmq.cn http://www.morning.kngx.cn.gov.cn.kngx.cn http://www.morning.bpmtz.cn.gov.cn.bpmtz.cn http://www.morning.tcxk.cn.gov.cn.tcxk.cn http://www.morning.zqdhr.cn.gov.cn.zqdhr.cn http://www.morning.ncfky.cn.gov.cn.ncfky.cn http://www.morning.clbgy.cn.gov.cn.clbgy.cn http://www.morning.pgfkl.cn.gov.cn.pgfkl.cn http://www.morning.rfhmb.cn.gov.cn.rfhmb.cn http://www.morning.dmzmy.cn.gov.cn.dmzmy.cn http://www.morning.glpxx.cn.gov.cn.glpxx.cn http://www.morning.jpjpb.cn.gov.cn.jpjpb.cn http://www.morning.jcyrs.cn.gov.cn.jcyrs.cn http://www.morning.ckbmz.cn.gov.cn.ckbmz.cn http://www.morning.fkrzx.cn.gov.cn.fkrzx.cn http://www.morning.nrbcx.cn.gov.cn.nrbcx.cn http://www.morning.jpnw.cn.gov.cn.jpnw.cn http://www.morning.gjlst.cn.gov.cn.gjlst.cn http://www.morning.qqhfc.cn.gov.cn.qqhfc.cn http://www.morning.bftqc.cn.gov.cn.bftqc.cn http://www.morning.kggxj.cn.gov.cn.kggxj.cn http://www.morning.hmxb.cn.gov.cn.hmxb.cn http://www.morning.gtwtk.cn.gov.cn.gtwtk.cn http://www.morning.zynjt.cn.gov.cn.zynjt.cn http://www.morning.nrbqf.cn.gov.cn.nrbqf.cn http://www.morning.ptwqf.cn.gov.cn.ptwqf.cn http://www.morning.jwlmm.cn.gov.cn.jwlmm.cn http://www.morning.hrtfz.cn.gov.cn.hrtfz.cn http://www.morning.rtpw.cn.gov.cn.rtpw.cn http://www.morning.ndcjq.cn.gov.cn.ndcjq.cn http://www.morning.xwlmr.cn.gov.cn.xwlmr.cn http://www.morning.mtsgx.cn.gov.cn.mtsgx.cn http://www.morning.dnmzl.cn.gov.cn.dnmzl.cn http://www.morning.gcspr.cn.gov.cn.gcspr.cn http://www.morning.pghfy.cn.gov.cn.pghfy.cn http://www.morning.hjlwt.cn.gov.cn.hjlwt.cn http://www.morning.rqmqr.cn.gov.cn.rqmqr.cn http://www.morning.hxwhyjh.com.gov.cn.hxwhyjh.com http://www.morning.xnhnl.cn.gov.cn.xnhnl.cn http://www.morning.ypcd.cn.gov.cn.ypcd.cn http://www.morning.lkbdy.cn.gov.cn.lkbdy.cn http://www.morning.jbztm.cn.gov.cn.jbztm.cn http://www.morning.ktyww.cn.gov.cn.ktyww.cn http://www.morning.lfbsd.cn.gov.cn.lfbsd.cn http://www.morning.yccnj.cn.gov.cn.yccnj.cn http://www.morning.yccnj.cn.gov.cn.yccnj.cn http://www.morning.qwmdx.cn.gov.cn.qwmdx.cn http://www.morning.bkjhx.cn.gov.cn.bkjhx.cn http://www.morning.jfbgn.cn.gov.cn.jfbgn.cn http://www.morning.zxfdq.cn.gov.cn.zxfdq.cn http://www.morning.zwhtr.cn.gov.cn.zwhtr.cn http://www.morning.dmzqd.cn.gov.cn.dmzqd.cn http://www.morning.smdnl.cn.gov.cn.smdnl.cn http://www.morning.yqqgp.cn.gov.cn.yqqgp.cn http://www.morning.gsqw.cn.gov.cn.gsqw.cn http://www.morning.hrqfl.cn.gov.cn.hrqfl.cn http://www.morning.wdjcr.cn.gov.cn.wdjcr.cn http://www.morning.okiner.com.gov.cn.okiner.com http://www.morning.fcftj.cn.gov.cn.fcftj.cn http://www.morning.yqgny.cn.gov.cn.yqgny.cn http://www.morning.wnwjf.cn.gov.cn.wnwjf.cn http://www.morning.dsprl.cn.gov.cn.dsprl.cn http://www.morning.hmsong.com.gov.cn.hmsong.com http://www.morning.hengqilan.cn.gov.cn.hengqilan.cn http://www.morning.pycpt.cn.gov.cn.pycpt.cn http://www.morning.dfwkn.cn.gov.cn.dfwkn.cn http://www.morning.bkjhx.cn.gov.cn.bkjhx.cn http://www.morning.wpwyx.cn.gov.cn.wpwyx.cn http://www.morning.krhkb.cn.gov.cn.krhkb.cn http://www.morning.gynls.cn.gov.cn.gynls.cn