一条龙建设网站,惠州网站制作公司,discuz做资讯网站,捕鱼网站开发pytest测试框架
单元测试框架定义#xff1a;针对软件开发最小的单元#xff08;函数#xff0c;方法#xff09;进行正确性位置测试
单元测试框架#xff1a;java#xff08;junit#xff0c;testing#xff09;python#xff08;unittest#xff0c;pytest#…pytest测试框架
单元测试框架定义针对软件开发最小的单元函数方法进行正确性位置测试
单元测试框架javajunittestingpythonunittestpytest
单元测试框架的作用
测试发现从多个文件中找到测试用例测试执行按照一定的顺序和规则去执行并且生成结果测试判断通过断言判断预期结果和实际结果的差异测试报告统计测试的结果耗时通过率等生成美观的测试报告
自动化框架的内容单元测试框架pom设计模式数据驱动关键字驱动全局配置文件的封装日志监控selenium和request二次封装断言报告邮件更多…
pytest简介和安装
pytest比unittest更灵活容易上手pytest可以和seleniumrequesappnium结合实现web自动化接口自动化app自动化pytest可以实现测试用例的跳过和reruns失败用例重试pytest可以和allure生成测试报告pytest可以和jenkins持续集成pytest提供强大的插件
如下建立requirements.txt 内容如下
pytest 本身
pytest-html 生成html格式化报告
pytest-xdist 测试用例分布式进行多CPU分发
pytest-ordering 用于改变测试用例的执行顺序
pytest-returnfailures 用例失败后重跑
allure-pytest 用于生成美观的测试报告终端上输入安装 pip install -r requirements.txt检验
pytest --version模块命名
以test_开头或者 _test结尾测试类必须以Test开头并且不再有ini方法测试方法必须以test开头
测试用例的运行方式
1.主函数运行方式
1运行所有pytest.main
class TestLogin:def test_01_login(self):print(测试登录)if __name__ __main__:pytest.main()2运行指定模块
pytest.main([-vs,test_login.py])(3)指定目录
pytest.main([-vs,./test_other.py])4通过nodeid指定用例运行nodeid由模块名分隔符类名函数名组成
pytest.main([’-vs‘’./interface_testcase/test_interface.py::test_04_func‘])2.命令行模式
1运行所有
pytest2指定模块
pytest -vs test_login.py3指定目录
pytest -vs ./test_other.py(4)指定目录
pytest -vs ./interface_testcase/test_interface.py::test_04_func3.参数详解
-v 输出更详细的信息 -s 输出调试信息
-vs这两个参数一起用
-n:支持多线程或者分布式运行测试用例
如pytest -vs ./test_login.py -n 2
–returns NUM:失败用例重跑
-x表示只要要一个用例报错那么测试就停止
–maxfall2 出现两个用例失败就停止
-k根据测试用例的部分字符串指定测试用例
如pytest -vs/testcase -kao
class TestLogin:def test_01_login(self):print(测试登录)if __name__ __main__:pytest.main([-s])案例创建一个all.py文件运行指定的test用例和控制输出的信息
import pytestif __name__ __main__:pytest.main([-vs,test_login.py])相当于终端输入
pytest -vs test_login.py3.结合pytest.ini全局配置文件执行
pytest为整个pytest的核心配置文件 位置一般放在项目的根目录 编码必须是ANSI编码格式可以使用notepad修改编码格式 作用改变pytest默认行为 运行规则不管是主函数的模式运行命令行模式运行都会去读取ini文件 1 [pytest]
2 #命令行参数
3 #常见--html./reports/report.html --reruns 2
4 addopts -vs -m user_manager or smoke
5 #配置执行的用例位置
6 testpaths ./testcases
7 #配置修改默认的模块规则
8 python_files test_*.py
9 #配置修改默认的类规则
10 python_classes Test*
11 #配置修改默认的用例规则
12 python_functions test_*
13 #配置基础路径
14 base_url http://www.baidu.com
15 #标记
16 markers
17 smoke:冒烟测试用例
18 product_manage:商品管理
19 user_manager:用户管理pytest 测试用例的顺序
区分
unittestascII的大小来绝对的执行顺序
pytest默认从上到下
改变默认的执行顺序使用mark标记
添加注释,其中1是执行的顺序
pytest.mark.run(order1)如何分组执行冒烟、分模块执行、分接口和web执行
smoke冒烟用例分布在各个模块内
添加冒烟标记在.py文件
pytest.mark.smoke注意要在pytest.ini文件打一下标记 #标记markers
smoke:冒烟测试用例
product_manage:商品管理
user_manager:用户管理在终端输入
pytest -vs -m smoke想执行其他模块
pytest -vs -m smoke and 其他模块的名称跳过执行的用例 无条件跳过 pytest.mark,skipif(reason’跳过原因‘)有条件跳过 pytest.mark,skipif(条件reason’跳过原因‘)例如
pytest.mark.skipif(age18,reason已成年)pytest框架的一些前后置固件夹具处理常用的三种
前后置setup_class、setup、teardown、teardown_class
class TestPosition:def setUp_class(self):print(\n 在每个类执行前的准备工作例如创建日志对象、数据库链接、接口请求对象)def setup(self):print(\n 执行用例之前的开始动作比如打开浏览器)def test_01(self):print(\n 测试用例)def teardown(self):print(\n 执行用例之前的扫尾动作比如关闭浏览器)def teardown_class(self):print(\n 在每个类执行前的准备工作例如销毁日志对象、数据库链接、接口请求对象)
使用pytest.fixture()装饰器实现部分用例的前后置
装饰器 pytest.fixtrue(scope“作用范围”,autouse“自动执行”,params“参数化”,ids“参数别 名”,name“固件别名”)
案例
pytest.fixture(scope,params, autouse,ids,name)
def my_fixture():print(这是前置的方法可以实现部分以及全部用例的前置)yieldprint(这是后置方法可以实现部分以及全部用例的后置)def test_02(self,my_fixture):print(\n 执行fixture)用法
scope表示的是被pytest.fixture标记的方法的作用域。function默认classmoudlepackage/sessionparams:参数化支持列表[]元组字典列表[{},{},{}],字典元组{}{}{}
import pytestpytest.fixture(scopefunction,params[参数1,参数2,参数3])
def my_fixture(request):print(前置)yield request.param # 注意yield和return都表示返回的以上但是return后面不能跟代码yield可以print(后置)def test_02(self,my_fixture):print(\n 执行fixture)注意params和yield不能一起用但是可以和前置一起用。 autouseTrue自动化使用默认False ids当使用params参数化时给每一个值设置一个变量名意义不大 name给表示的是被pytest.fixture标记的方法取一个别名 当取了别名之后那么原来的名就用不了了
通过conftest.py和pytest.fixture()结合使用实现全局的前置应用如项目的全局登录模块的全局处理 conftest.py文件是单独存放的一个夹具的配置文件名称不能更改 用处可以在不同的py文件中使用同一个fixture函数 原则上conftest.py需要和运行的用例放在统一层并且不需要做任何的import导入的操作 案例 全局拥有全局前置各个子模块下面有一个前置对于用例编写为 def test_01_testconftest(self,all_fixtuture,user_fixture)print(test用例)print(user_fixture)print(all_fixture)这样全局前置包含了子模块的前置
setup/teardown,setup_class/teardown_class 它是作用于所有用例或者所有的类
pytest.fixture() 它的作用是局部的也是全局的
conftest.py和pytest_fixture()结合使用作用于全局的前后置
断言
assert
pytest结合allurepytest插件生成allure测试报告
以前是使用的是pytest_html生成现在可以使用allurepytest结合生成美观的报告
1.下载、解压、配置path路径
https://github.com/allure-framework/allure2/releases
配置环境变量如
D:\allure-2.30.0\bin
验证
allure --version
2.加入命令到pytest.ini文件生成json格式的临时报告
addopts -vs --alluredir ./temp3.生成allure报告
allure generate 命令固定的
./temp 临时的json格式的报告
-o 输出output
./report 生成allure报告路径
.clear 清空./report路径原来的报告
if __name__ __main__:time.sleep(3)# pytest.main([-vs,test_login.py])pytest.main()# allure generate 命令固定的./temp 临时的json格式的报告 -o 输出output ./report 生成allure报告路径 .clear 清空./report路径原来的报告os.system(allure generate ./temp -o ./report --clear)yaml接口自动化实战
1断言的封装
2allure报告的定制
3关键字驱动和数据驱动结合实现接口自动化测试
4python的反射
正常先初始化对象再调方法
反射通过对象得到类对象然后通过类对象调用方法
5jenkins的持续集成和allure报告的集成并且根据项目的自动化的报告错误率发送电子邮件
pytest接口自动化
接口测试
发展背景
目前市面上主流的接口测试工具适用于中小型项目
postmannewmangitjenkinsjmeterantgitjenkins
目前主流的接口自动化测试技术适用于大小型的项目
requests库
安装
requests用来发送http请求以及接受http响应的python第三方库主要用于接口自动化测试
pip install requestsrequests库常用的方法
requests.get() url是接口的地址param用于传参
requests.post() url是接口地址data用于传参json也适用传参
data和json传参的区别主要是通过请求头Content-Type来区分
Content-Type作用是服务器要求传入的报文的内容类型
请求请求方式请求路径请求头请求正文
requests.pull()
requests.delete()
requests.request() 可以发送所有类型的请求get,post,put,delete
拓展postman的四种传参的方式对应的Content-Type的值如下
from-dataContent-Typemultipart/form-data、boundary
x-www-from-urlencoded:Content-Type:applic.ation/x-www-form-urlencoded
raw:
text:Content-Type:text/plain
javascript:Content-Type:application/javascript
json:Content-Type:application/json
html:Content-Type:application/xml
binary:Content-Type:application/binary
data和json传参以及Content-Type的关系如下
1.data传参
data传参报文时dict类型那么默认Content-Type application/x-www-form-urlencoded
data传参报文是str类型那么默认Content-Type text/plain
2.json传参
报文可以是dict类型那么默认Content-Type application/json
注意 json是字典类型的字符串显示。 json.loads() 把json字符串转化为dict格式json.dumps() 把dict格式转换为json字符串 不管是getpostputdelete都是调用的requests.request方法。而requests.reques方法调用的是session.reques方法 method 请求方式 url 请求路径 paramsNone get方式传参 dataNone post方式传参 jsonNone post方式传参 headersNone 请求头 cookieNone 请求cookie filesNone 文件上传
requests模块返回的response对象详解
res.json() 获取返回的字典格式的数据res.text 获得返回的字符串格式的数据res.content 获得返回的bytes字节类型的数据res_status_code 返回状态码res.reason 返回状态信息res.cookies 返回cookie信息res.headers 返回响应头res.request.xxx 返回需求的数据如需求头请求参数
请求必须带有请求头的接口以及需要cookie鉴权和session鉴权的接口
input typehidden namecsrf_token valuee7202bf558a7eb34/90%以上的基于web的接口都有cookie鉴权
两种解决方式 使用cookie关联 案例注释接口关联变量在第一个接口取值在第二个接口传值第三个接口文件上传访问首页的接口解决鉴权 import requestsclass TestRequest:#全局变量类变量通过类名调用access_tokencsrf_tokenphp_cookiedef test_get_token(self):url:https://api.weixin.qq.com/cgi-bin/tokendata:{grant_type:client_credential,appid:wx74a8627810cfa300,secret:e40a02f9cf9d79df497e6aaf93ab80}resrequests.request(methodget,urlurl,paramsdata)print(res.json())TestRequest access_token res.json()[access_token]# post请求编辑标签接口def test_edit_flag(self):url:https://api.weixin.qq.com/cgi-bin/tags/updataccess tokenTestRequest.access_tokendata[tag:{id:134,name:广东人}]str_datajson.dumps(data)resrequests.request(methodpost,urlurl,datadata)print(res.json())# 文件上传
def test_file_upload(self):url:https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_tokenTestRequest.access_tokenTestRequest.access_tokendata{media:open(rE:\shu.png,rb)}resrequests.request(methodpost,urlurl,filesdata)print(res.json())# 访问首页的接口
def test_start(self):url:https://47.107.116.139/phpwindresrequests.request(methodget,urlurl)print(res.text)
# 正则提取token
objre.search(namecsrf_token value(.*?),res.text)
# print(obj.group(1))
TestRequest.csrf_tokenobj.group(1)
# 解决鉴权的方式一提取cookie
TestRequest.php_cookieres.cookies# 登录接口
def test_login(self):url:https://47.107.116.139/phpwind/index.php?mucloginadorundata{username:msxy,password:msxy,csrf_token:TestRequest.csrf_token,backurl:http://47.107.116.139/phpwind/,invite:}# 请求头headers{Accept:application/json, text/javascript, /; q0.01,X-Requested-with:XMLHttpRequest}resrequests.request(methodpost,urlurl,datadata,headersheaders,cookiesTestRequest.php_cookie)print(res.json())
# 解决鉴权的方式一提取cookie
TestRequest.php_cookieres.cookiesif __name__ __main__:TestRequest().test_get_token()TestRequest().test_edit_flag()TestRequest().test_file_upload()TestRequest().test_start()TestRequest().test_login()使用session关联 import requestsclass TestRequest:#全局变量类变量通过类名调用access_tokencsrf_token# php_cookiesessrequests.session()def test_get_token(self):url:https://api.weixin.qq.com/cgi-bin/tokendata:{grant_type:client_credential,appid:wx74a8627810cfa300,secret:e40a02f9cf9d79df497e6aaf93ab80}resrequests.request(methodget,urlurl,paramsdata)print(res.json())TestRequest access_token res.json()[access_token]# post请求编辑标签接口def test_edit_flag(self):url:https://api.weixin.qq.com/cgi-bin/tags/updataccess tokenTestRequest.access_tokendata[tag:{id:134,name:广东人}]str_datajson.dumps(data)resrequests.request(methodpost,urlurl,datadata)print(res.json())# 文件上传
def test_file_upload(self):url:https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_tokenTestRequest.access_tokenTestRequest.access_tokendata{media:open(rE:\shu.png,rb)}resrequests.request(methodpost,urlurl,filesdata)print(res.json())# 访问首页的接口
def test_start(self):url:https://47.107.116.139/phpwindresTestRequest.sess.request(methodget,urlurl)# print(res.text)
# 正则提取token
objre.search(namecsrf_token value(.*?),res.text)
# print(obj.group(1))
TestRequest.csrf_tokenobj.group(1)# 登录接口
def test_login(self):url:https://47.107.116.139/phpwind/index.php?mucloginadorundata{username:msxy,password:msxy,csrf_token:TestRequest.csrf_token,backurl:http://47.107.116.139/phpwind/,invite:}# 请求头headers{Accept:application/json, text/javascript, /; q0.01,X-Requested-with:XMLHttpRequest}resTestRequest.sess.request(methodpost,urlurl,datadata,headersheaders)print(res.json())if __name__ __main__:TestRequest().test_get_token()TestRequest().test_edit_flag()TestRequest().test_file_upload()TestRequest().test_start()TestRequest().test_login()