python接口自动化11-pytest入门
前言
pytest是一个非常成熟的全功能的Python测试框架,适合从简单的单元到复杂的功能测试,主要特点有以下几点:
- 简单灵活,容易上手;
- 支持参数化;
- 能够支持简单的单元测试;
- 标记测试功能与属性
- 复杂的功能测试,比如可以做selenium等自动化测试、接口自动化测试(pytest+requests);
- pytest具有很多第三方插件,并且可以自定义扩展,比较好用的如pytest-selenium(集成selenium)、pytest-html(完美html测试报告生成)等;
- Skip和xfail:处理不成功的测试用例;
- 可以很好的和jenkins集成;
- 通过xdist插件分发测试到多个CPU
一、简介
1、环境搭建推荐版本匹配:pip install pytest==3.6.3
- Python3.6.x + pytest 3.6.3
- Python3.7.x + pytest 4.0.2
2、查看版本:pytest --version
C:\Users\Administrator>pytest --version
This is pytest version 3、6、3, imported from d:\path_python\lib\site-packages\pytest、py
3、pytest 命名规则:
- 文件名以test_*、py 或 *_test、py
- 类已 Test* 开头
- 函数/方法以 test_* 开头
4、pytest 直接写用例,写完 cmd 运行,不需要导入其他模块。
G:\python_study\study\pytest_demo\study>pytest -s test_demo1.py
================================================= test session starts =================================================
platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0
rootdir: G:\python_study\study\pytest_demo\study, inifile:
collected 2 items
test_demo1.py
我是用例:a
.
我是用例:b
.
============================================== 2 passed in 0.02 seconds ===============================================
二、pytest 命令行参数介绍
1、运行规则:pytest py文件路径
C:\Users\Administrator>pytest G:\python_study\study\pytest_demo\study\test_demo.py
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0
rootdir: C:\Users\Administrator, inifile:
collected 4 items test_demo.py .... [100%] ========================== 4 passed in 0.03 seconds ===========================
2、显示打印信息(不然不会看到打印内容):pytest -s xxx
G:\python_study\study\pytest_demo\study>pytest -s test_demo1.py
================================================= test session starts =================================================
platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0
rootdir: G:\python_study\study\pytest_demo\study, inifile:
collected 2 items test_demo1.py
我是用例:a
.
我是用例:b
. ============================================== 2 passed in 0.02 seconds ===============================================
3、显示详细信息:pytest -v xxx
G:\python_study\study\pytest_demo\study>pytest test_demo1.py -v
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0 -- d:\path_python\python.exe
cachedir: .pytest_cache
rootdir: G:\python_study\study\pytest_demo\study, inifile:
collected 2 items test_demo1.py::Test_api::test_a PASSED [ 50%]
test_demo1.py::Test_api::test_b PASSED [100%] ========================== 2 passed in 0.02 seconds ===========================
4、简洁显示信息:pytest -q xxx
G:\python_study\study\pytest_demo\study>pytest test_demo1.py -q
.. [100%]
2 passed in 0.02 seconds
5、运行指定用例:pytest -k case_name (case_name可类可函数,模糊匹配关键字),如下匹配 demo
G:\python_study\study\pytest_demo\study>pytest -k demo -v
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0 -- d:\path_python\python.exe
cachedir: .pytest_cache
rootdir: G:\python_study\study\pytest_demo\study, inifile:
collected 11 items / 5 deselected test_demo.py::test_ab PASSED [ 16%]
test_demo.py::test_aba PASSED [ 33%]
test_demo.py::Test_api::test_aa PASSED [ 50%]
test_demo.py::Test_api::test_b PASSED [ 66%]
test_demo1.py::Test_api::test_a PASSED [ 83%]
test_demo1.py::Test_api::test_b PASSED [100%] =================== 6 passed, 5 deselected in 0.06 seconds ====================
6、命令行参数不分顺序,还有其他命令行参数,不一一细说:
- 运行类用例且不运行类某个用例:pytest -v -k "Test_api1 and not test_a"
- 失败停止测试:pytest -x
- 指定个数失败后停止测试:pytest --maxfail=2
- 运行上一次失败用例(或没失败的):pytest --last-failed
- 等等
7、pycharm 设置 pytest 运行用例:
更多请查看 pytest -h 或者找度娘,我们一般用以上的参数够日常使用了。欢迎来QQ交流群:482713805
python接口自动化11-pytest入门的更多相关文章
- python接口自动化之pytest环境准备与入门(五)
安装的pytest版本应该与安装的python版本对应,不然会有问题 (我的环境是python3.6与pytest4.5.0) 1.安装pytest pip install pytest==4.5.0 ...
- 2020年第二期《python接口自动化+测试开发》课程,已开学!
2020年第二期<python接口自动化+python测试开发>课程,12月15号开学! 主讲老师:上海-悠悠 上课方式:QQ群视频在线教学,方便交流 本期上课时间:12月15号-3月29 ...
- Python接口自动化——soap协议传参的类型是ns0类型的要创建工厂方法纪要
1:在Python接口自动化中,对于soap协议的xml的请求我们可以使用Suds Client来实现,其soap协议传参的类型基本上是有2种: 第一种是传参,不需要再创建啥, 第二种就是ns0类型的 ...
- python接口自动化(十)--post请求四种传送正文方式(详解)
简介 post请求我在python接口自动化(八)--发送post请求的接口(详解)已经讲过一部分了,主要是发送一些较长的数据,还有就是数据比较安全等.我们要知道post请求四种传送正文方式首先需要先 ...
- python接口自动化-Cookie_绕过验证码登录
前言 有些登录的接口会有验证码,例如:短信验证码,图形验证码等,这种登录的验证码参数可以从后台获取(或者最直接的可查数据库) 获取不到也没关系,可以通过添加Cookie的方式绕过验证码 前面在“pyt ...
- python接口自动化28-requests-html爬虫框架
前言 requests库的好,只有用过的人才知道,最近这个库的作者又出了一个好用的爬虫框架requests-html.之前解析html页面用过了lxml和bs4, requests-html集成了一些 ...
- python接口自动化-参数化
原文地址https://www.cnblogs.com/yoyoketang/p/6891710.html python接口自动化 -参数关联(一)https://www.cnblogs.com/11 ...
- python接口自动化 -参数关联(一)
原文地址https://www.cnblogs.com/yoyoketang/p/6886610.html 原文地址https://www.cnblogs.com/yoyoketang/ 原文地址ht ...
- python接口自动化20-requests获取响应时间(elapsed)与超时(timeout)
前言 requests发请求时,接口的响应时间,也是我们需要关注的一个点,如果响应时间太长,也是不合理的. 如果服务端没及时响应,也不能一直等着,可以设置一个timeout超时的时间 关于reques ...
- python接口自动化24-有token的接口项目使用unittest框架设计
获取token 在做接口自动化的时候,经常会遇到多个用例需要用同一个参数token,并且这些测试用例跨.py脚本了. 一般token只需要获取一次就行了,然后其它使用unittest框架的测试用例全部 ...
随机推荐
- 使用 docker 部署常用的开发环境
使用 docker 部署常用的开发环境 Intro 使用 docker,很多环境可以借助 docker 去部署,没必要所有的环境都在本地安装,十分方便. 前段时间电脑之前返厂修了,回来之后所有的软件都 ...
- FAQ: Oracle Flex ASM 12c / 12.1 (Doc ID 1573137.1)
FAQ: Oracle Flex ASM 12c / 12.1 (Doc ID 1573137.1) APPLIES TO: Oracle Database - Enterprise Edition ...
- ML.NET Model Builder 更新
ML.NET是面向.NET开发人员的跨平台机器学习框架,而Model Builder是Visual Studio中的UI工具,它使用自动机器学习(AutoML)轻松地允许您训练和使用自定义ML.NET ...
- libwebrtc & libmediasoupclient编译
本文简单介绍在Ubuntu下libwebrtc的编译过程. 由于网速限制,实际编译过程是在远程vps上编译滴. 系统环境 Ubuntu 18.04系统的虚拟主机. root@vultr:~# pwd ...
- table-layout:fixed
table-layout: fixed; 在table上设置上面属性后,如果不设置td的宽度,那么所有td的宽度平分总table宽度.如果设置了td的宽度,则以设置的宽度为准. table-layou ...
- .NET Core 实现 腾讯云云解析简单客户端
一.说明 腾讯云的.NET SDK虽然非常强大,但是对他的产品支持不是很完全,域名的云解析就没有SDK,所以自己写了一个,初衷是用来做动态DNS的,也准备接入多个云厂商,但是我自己本身仅仅只有腾讯云这 ...
- 这十个Python常用库,学习Python的你必须要知道!
想知道Python取得如此巨大成功的原因吗?只要看看Python提供的大量库就知道了 包括原生库和第三方库.不过,有这么多Python库,有些库得不到应有的关注也就不足为奇了.此外,只在一个领域里的工 ...
- flutter 下拉加载+下拉加载
功能: 1.下拉加载 2.上拉加载 3.显示加载图标 4.更新列表数据,隐藏加载图标 flutter库: flutter_spinkit: ^3.1.0 加载图标 其他:加载列表需要列表,基于上一节的 ...
- Git如何把本地代码推送到远程仓库
Git如何把本地代码推送到远程仓库 1. 初始化版本库 $ git init 2. 添加文件到版本库(只是添加到缓存区),.代表添加文件夹下所有文件 $ git add . 3. 把添加的文件提交到版 ...
- 基于C# 调用百度AI 人脸识别
一.设置 登录百度云控制台,添加应用-添加人脸识别,查找,对比等. 记住API Key和Secret Key 二.创建Demo程序 1.使用Nuget安装 Baidu.AI 和 Newtonsoft. ...