前言

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入门的更多相关文章

  1. python接口自动化之pytest环境准备与入门(五)

    安装的pytest版本应该与安装的python版本对应,不然会有问题 (我的环境是python3.6与pytest4.5.0) 1.安装pytest pip install pytest==4.5.0 ...

  2. 2020年第二期《python接口自动化+测试开发》课程,已开学!

    2020年第二期<python接口自动化+python测试开发>课程,12月15号开学! 主讲老师:上海-悠悠 上课方式:QQ群视频在线教学,方便交流 本期上课时间:12月15号-3月29 ...

  3. Python接口自动化——soap协议传参的类型是ns0类型的要创建工厂方法纪要

    1:在Python接口自动化中,对于soap协议的xml的请求我们可以使用Suds Client来实现,其soap协议传参的类型基本上是有2种: 第一种是传参,不需要再创建啥, 第二种就是ns0类型的 ...

  4. python接口自动化(十)--post请求四种传送正文方式(详解)

    简介 post请求我在python接口自动化(八)--发送post请求的接口(详解)已经讲过一部分了,主要是发送一些较长的数据,还有就是数据比较安全等.我们要知道post请求四种传送正文方式首先需要先 ...

  5. python接口自动化-Cookie_绕过验证码登录

    前言 有些登录的接口会有验证码,例如:短信验证码,图形验证码等,这种登录的验证码参数可以从后台获取(或者最直接的可查数据库) 获取不到也没关系,可以通过添加Cookie的方式绕过验证码 前面在“pyt ...

  6. python接口自动化28-requests-html爬虫框架

    前言 requests库的好,只有用过的人才知道,最近这个库的作者又出了一个好用的爬虫框架requests-html.之前解析html页面用过了lxml和bs4, requests-html集成了一些 ...

  7. python接口自动化-参数化

    原文地址https://www.cnblogs.com/yoyoketang/p/6891710.html python接口自动化 -参数关联(一)https://www.cnblogs.com/11 ...

  8. python接口自动化 -参数关联(一)

    原文地址https://www.cnblogs.com/yoyoketang/p/6886610.html 原文地址https://www.cnblogs.com/yoyoketang/ 原文地址ht ...

  9. python接口自动化20-requests获取响应时间(elapsed)与超时(timeout)

    前言 requests发请求时,接口的响应时间,也是我们需要关注的一个点,如果响应时间太长,也是不合理的. 如果服务端没及时响应,也不能一直等着,可以设置一个timeout超时的时间 关于reques ...

  10. python接口自动化24-有token的接口项目使用unittest框架设计

    获取token 在做接口自动化的时候,经常会遇到多个用例需要用同一个参数token,并且这些测试用例跨.py脚本了. 一般token只需要获取一次就行了,然后其它使用unittest框架的测试用例全部 ...

随机推荐

  1. 推荐一个好用的行内可编辑的table组件 vxe-table

    项目中有一个需要用户点击table单元格可编辑的需求,由于博主用的是elementUI,element组件内实现可编辑,用过的小伙伴都知道,非常麻烦,后来博主在浏览组件的时候发现了 一款非常好用的ta ...

  2. TICK技术栈(五)Kapacitor安装及使用

    1.什么是Kapacitor? Kapacitor是InfluxData开源的数据处理引擎.它可以处理来自InfluxDB的流数据和批处理数据,并且用户可以用tickScript脚本来处理,监视和警报 ...

  3. 11G-使用跨平台增量备份减少可移动表空间的停机时间 XTTS (Doc ID 1389592.1)

    11G - Reduce Transportable Tablespace Downtime using Cross Platform Incremental Backup (Doc ID 13895 ...

  4. CF#603 Div2

    差不多半年没打cf,还是一样的菜:不过也没什么,当时是激情,现在已是兴趣了,开心就好. A Sweet Problem 思维,公式推一下过了 B PIN Codes 队友字符串取余过了,结果今天早上一 ...

  5. 20.never give up

  6. Hive参数调优

    调优 Hive提供三种可以改变环境变量的方法,分别是: (1)修改${HIVE_HOME}/conf/hive-site.xml配置文件: 所有的默认配置都在${HIVE_HOME}/conf/hiv ...

  7. python-将一个列表切分成多个小列表

    list是python中较为常见的数据类型,它是一个可迭代对象,迭代是什么?简单的可以理解成:一个可以被for循环遍历的对象 今天拿到一个类似这样的list list_info = ['name zh ...

  8. Linux 指令学习

    查询java安装地址 which java ls -lrt /bin/java ls -lrt /etc/alternatives/java # 如果已经配好,则echo $JAVA_HOME 更改环 ...

  9. Metasploit从文件中读取目标地址

    本文简单介绍如何使用Metasploit从文件中读取目标地址,来执行检测. 以检测MS17-010漏洞为例,在设定RHOSTS参数时,可设定目标地址范围和CIDR地址块,设定单个IP的目标也是可以的. ...

  10. H5移动端开发遇见的东西

    常见的有viewport.强制浏览器全屏.IOS的Web APP模式.可点击元素出现阴影 本文主要讲一些其他的或者实用的优化手段. 1. 弹出数字键盘 <!-- 有"#" & ...