前言

pytest 运行完用例之后会生成一个 .pytest_cache 的缓存文件夹,用于记录用例的ids和上一次失败的用例。

方便我们在运行用例的时候加上--lf 和 --ff 参数,快速运行上一次失败的用例。

--lf, --last-failed 只重新运行上次运行失败的用例(或如果没有失败的话会全部跑)

--ff, --failed-first 运行所有测试,但首先运行上次运行失败的测试(这可能会重新测试,从而导致重复的fixture setup/teardown)

--lf 和 --ff 相关介绍查看之前的这篇https://www.cnblogs.com/yoyoketang/p/9769559.html

cache

pytest -h 查看命令行参数,关于 cache 参数的使用方式

>pytest -h

--lf, --last-failed   rerun only the tests that failed at the last run (or
all if none failed)
--ff, --failed-first run all tests but run the last failures first. This
may re-order tests and thus lead to repeated fixture
--nf, --new-first run tests from new files first, then the rest of the
tests sorted by file mtime
--cache-show=[CACHESHOW]
show cache contents, don't perform collection or
tests. Optional argument: glob (default: '*').
--cache-clear remove all cache contents at start of test run.

参数说明:

  • --lf 也可以使用 --last-failed 仅运行上一次失败的用例
  • --ff 也可以使用 --failed-first 运行全部的用例,但是上一次失败的用例先运行
  • --nf 也可以使用 --new-first 根据文件插件的时间,新的测试用例会先运行
  • --cache-show=[CACHESHOW] 显示.pytest_cache文件内容,不会收集用例也不会测试用例,选项参数: glob (默认: '*')
  • --cache-clear 测试之前先清空.pytest_cache文件

--cache-show

测试案例代码test_x.py

# test_x.py
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/ def test_01():
a = "hello"
b = "hello"
assert a == b def test_02():
a = "hello"
b = "hello world"
assert a == b def test_03():
a = "hello"
b = "hello world"
assert a in b def test_04():
a = "hello"
b = "hello world"
assert a not in b

命令行输入 运行完成后,会有2个用例失败,2个用例成功

>pytest test_x.py --tb=no
============================= test session starts =============================
collected 4 items test_x.py .F.F [100%] ===================== 2 failed, 2 passed in 0.11 seconds ======================

运行完成后,会在当前的目录生成一个 .pytest_cache 的缓存文件夹,层级结构如下

lastfailed 文件记录上一次运行失败的用例

{
"test_x.py::test_02": true,
"test_x.py::test_04": true
}

nodeids 文件记录所有用例的节点

[
"test_x.py::test_01",
"test_x.py::test_02",
"test_x.py::test_03",
"test_x.py::test_04"
]

于是可以通过 pytest --cache-show 命令查看cache目录

D:\soft\kecheng202004\xuexi>pytest --cache-show
============================= test session starts =============================
cachedir: \.pytest_cache
---------------------------- cache values for '*' -----------------------------
cache\lastfailed contains:
{'test_x.py::test_02': True, 'test_x.py::test_04': True}
cache\nodeids contains:
['test_x.py::test_01',
'test_x.py::test_02',
'test_x.py::test_03',
'test_x.py::test_04']
cache\stepwise contains:
[] ======================== no tests ran in 0.02 seconds =========================

--cache-clear

--cache-clear 用于在测试用例开始之前清空cache的内容

pytest --cache-clear

查看pytest关于cache的更多文档 https://docs.pytest.org/en/latest/cache.html

pytest文档51-内置fixture之cache使用的更多相关文章

  1. pytest文档42-fixture参数化params

    前言 参数化是自动化测试里面必须掌握的一个知识点,用过 unittest 框架的小伙伴都知道使用 ddt 来实现测试用例的参数化. pytest 测试用例里面对应的参数可以用 parametrize ...

  2. pytest文档7-pytest-html生成html报告

    前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...

  3. pytest文档3-pycharm运行pytest

    前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...

  4. pytest文档52-命令行参数--setup-show查看fixture的执行过程

    前言 使用命令行运行 pytest 用例的时候,看不到 fixture 的执行过程. 如果我们想知道fixture的执行过程和先后顺序,可以加上 --setup-show 命令行参数,帮助查看 fix ...

  5. pytest文档30-功能用例与自动化用例完美对接(allure)

    前言 做自动化做久了,经常会思考一个问题,到底别人是怎么做的自动化,跟自己的有啥不一样,看过不少书和资料,都是停留在demo的层面. 真正把自动化做的好的大牛又不屑于分享自己的劳动成果,所以大部分情况 ...

  6. pytest文档56-插件打包上传到 pypi 库

    前言 pytest 的插件完成之后,可以上传到 github,方便其他小伙伴通过 pip 源码安装.如果我们想通过 pip install packages 这种方式安装的话,需上传到 pypi 仓库 ...

  7. pytest文档43-元数据使用(pytest-metadata)

    前言 什么是元数据?元数据是关于数据的描述,存储着关于数据的信息,为人们更方便地检索信息提供了帮助. pytest 框架里面的元数据可以使用 pytest-metadata 插件实现.文档地址http ...

  8. pytest文档19-doctest测试框架

    前言 doctest从字面意思上看,那就是文档测试.doctest是python里面自带的一个模块,它实际上是单元测试的一种. 官方解释:doctest 模块会搜索那些看起来像交互式会话的 Pytho ...

  9. pytest文档9-参数化parametrize

    前言 pytest.mark.parametrize装饰器可以实现测试用例参数化. parametrizing 1.这里是一个实现检查一定的输入和期望输出测试功能的典型例子 # content of ...

随机推荐

  1. SpringMVC-数据提交

    数据提交 目录 数据提交 1. 前端的参数与controller中的参数名一致 2. 前端的参数与controller中的参数名不一致 3. 前端接收的是一个对象 4. 总结 5. 数据显示到前端 1 ...

  2. sql注入 报错注入常用的三种函数

    1.floor()函数 报错原因是 报错的原因是因为rand()函数在查询的时候会执行一次,插入的时候还会执行一次.这就是整个语句报错的关键 前面说过floor(rand(0)*2) 前六位是0110 ...

  3. PHP之道(PHP The Right Way)

    原文地址:http://laravel-china.github.io/php-the-right-way/

  4. 第1课 - make和makefile

    第1课 - make 和 makefile 1. make make 是一个应用程序,位于 /usr/bin/make 目录下,make 有如下的功能: (1)解析源程序之间的依赖关系 (2)根据依赖 ...

  5. [补题]匹配%#,%#之间的字符串重复%前的num遍

    题目 匹配%#,%#之间的字符串重复%前的num遍. 样例1: 3%acm#2%acm# 输出: acmacmacmacmacm 样例2: 3%2%acm## 输出: acmacmacmacmacm ...

  6. (专题四)06 matlab绘图选项卡

    绘图选项卡 例子1--选择已有变量,绘制图形 都是按照选中的先后顺序依次确定坐标, 如果要修改绘制图形 法一,利用绘图工具和停靠图形按钮 法二,命令行窗口中输入命令 >>plottools ...

  7. windows下搭建ElasticSearch

      1.官网下载ElasticSearch,需要java环境支持       地址:https://www.elastic.co/products/elasticsearch   2.下载后解压到目录 ...

  8. HTML+CSS使用swiper快速生成最简单、最快捷、最易看懂的轮播图

    1.  在网页顶部输入swiper.com.con,进入swiper官网 2.   点击" API文档",获取轮播图代码的地方 3.   点击左侧"swiper初始化&q ...

  9. JVM学习目录

    JVM学习目录 JVM的整体结构 1.类加载子系统 类加载子系统 2.运行时数据区 运行时数据区总览 堆.栈.方法区的详细图解 2.1.程序计数器 程序计数器 2.2.本地方法栈 本地方法栈 2.3. ...

  10. Oracle学习(十四)分表分区

    一.前言 大数据量的查询,不仅查询速度非常慢,而且还会导致数据库经常宕机,在尝试添加索引及查询方式修改后,还有没有更有效的解决方案呢? 分库.分表.分区这些概念咱就应该了解一下. 二.分表 假如一个大 ...