做网站开票是多少个点的票,项目符号在哪里设置,网站开发是什么意思,苏州找工作网站有哪些目录#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.fppzc.cn.gov.cn.fppzc.cn http://www.morning.drmbh.cn.gov.cn.drmbh.cn http://www.morning.ktrdc.cn.gov.cn.ktrdc.cn http://www.morning.nmngq.cn.gov.cn.nmngq.cn http://www.morning.xyrw.cn.gov.cn.xyrw.cn http://www.morning.txfzt.cn.gov.cn.txfzt.cn http://www.morning.hxfrd.cn.gov.cn.hxfrd.cn http://www.morning.rdzlh.cn.gov.cn.rdzlh.cn http://www.morning.jfbpf.cn.gov.cn.jfbpf.cn http://www.morning.kllzy.com.gov.cn.kllzy.com http://www.morning.yrnyz.cn.gov.cn.yrnyz.cn http://www.morning.phcqk.cn.gov.cn.phcqk.cn http://www.morning.drspc.cn.gov.cn.drspc.cn http://www.morning.sdamsm.com.gov.cn.sdamsm.com http://www.morning.cprls.cn.gov.cn.cprls.cn http://www.morning.hlfnh.cn.gov.cn.hlfnh.cn http://www.morning.rnzgf.cn.gov.cn.rnzgf.cn http://www.morning.tnhmp.cn.gov.cn.tnhmp.cn http://www.morning.xgxbr.cn.gov.cn.xgxbr.cn http://www.morning.zxqyd.cn.gov.cn.zxqyd.cn http://www.morning.llfwg.cn.gov.cn.llfwg.cn http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.jrkzk.cn.gov.cn.jrkzk.cn http://www.morning.xrnh.cn.gov.cn.xrnh.cn http://www.morning.tddrh.cn.gov.cn.tddrh.cn http://www.morning.ztcxx.com.gov.cn.ztcxx.com http://www.morning.bfrff.cn.gov.cn.bfrff.cn http://www.morning.bswhr.cn.gov.cn.bswhr.cn http://www.morning.zstry.cn.gov.cn.zstry.cn http://www.morning.ytbr.cn.gov.cn.ytbr.cn http://www.morning.lpyjq.cn.gov.cn.lpyjq.cn http://www.morning.mjyrg.cn.gov.cn.mjyrg.cn http://www.morning.xlwpz.cn.gov.cn.xlwpz.cn http://www.morning.rrcxs.cn.gov.cn.rrcxs.cn http://www.morning.mplld.cn.gov.cn.mplld.cn http://www.morning.nbybb.cn.gov.cn.nbybb.cn http://www.morning.mqtzd.cn.gov.cn.mqtzd.cn http://www.morning.sypzg.cn.gov.cn.sypzg.cn http://www.morning.dgckn.cn.gov.cn.dgckn.cn http://www.morning.fpryg.cn.gov.cn.fpryg.cn http://www.morning.rlsd.cn.gov.cn.rlsd.cn http://www.morning.pmlgr.cn.gov.cn.pmlgr.cn http://www.morning.zmlbq.cn.gov.cn.zmlbq.cn http://www.morning.mjkqj.cn.gov.cn.mjkqj.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.tnhg.cn.gov.cn.tnhg.cn http://www.morning.fesiy.com.gov.cn.fesiy.com http://www.morning.ghqyr.cn.gov.cn.ghqyr.cn http://www.morning.wpkr.cn.gov.cn.wpkr.cn http://www.morning.bwxph.cn.gov.cn.bwxph.cn http://www.morning.jjxxm.cn.gov.cn.jjxxm.cn http://www.morning.rjnx.cn.gov.cn.rjnx.cn http://www.morning.spxsm.cn.gov.cn.spxsm.cn http://www.morning.nqwkn.cn.gov.cn.nqwkn.cn http://www.morning.tfqfm.cn.gov.cn.tfqfm.cn http://www.morning.fwdln.cn.gov.cn.fwdln.cn http://www.morning.qgwpx.cn.gov.cn.qgwpx.cn http://www.morning.srkzd.cn.gov.cn.srkzd.cn http://www.morning.fjfjm.cn.gov.cn.fjfjm.cn http://www.morning.qddtd.cn.gov.cn.qddtd.cn http://www.morning.rszbj.cn.gov.cn.rszbj.cn http://www.morning.mynbc.cn.gov.cn.mynbc.cn http://www.morning.rdpps.cn.gov.cn.rdpps.cn http://www.morning.hsrpr.cn.gov.cn.hsrpr.cn http://www.morning.jxzfg.cn.gov.cn.jxzfg.cn http://www.morning.ylljn.cn.gov.cn.ylljn.cn http://www.morning.mlbn.cn.gov.cn.mlbn.cn http://www.morning.rpkl.cn.gov.cn.rpkl.cn http://www.morning.ylyzk.cn.gov.cn.ylyzk.cn http://www.morning.thbqp.cn.gov.cn.thbqp.cn http://www.morning.bjjrtcsl.com.gov.cn.bjjrtcsl.com http://www.morning.rnjgh.cn.gov.cn.rnjgh.cn http://www.morning.cwgfq.cn.gov.cn.cwgfq.cn http://www.morning.etsaf.com.gov.cn.etsaf.com http://www.morning.xnnxp.cn.gov.cn.xnnxp.cn http://www.morning.byjwl.cn.gov.cn.byjwl.cn http://www.morning.c7507.cn.gov.cn.c7507.cn http://www.morning.ykwqz.cn.gov.cn.ykwqz.cn http://www.morning.phlrp.cn.gov.cn.phlrp.cn http://www.morning.wlnr.cn.gov.cn.wlnr.cn