Pytest(12)pytest缓存

前言

pytest 运行完用例之后会生成一个 .pytest_cache 的缓存文件夹,用于记录用例的ids和上一次失败的用例。
方便我们在运行用例的时候加上–lf 和 –ff 参数,快速运行上一次失败的用例。
–lf, –last-failed 只重新运行上次运行失败的用例(或如果没有失败的话会全部跑)
–ff, –failed-first 运行所有测试,但首先运行上次运行失败的测试(这可能会重新测试,从而导致重复的fixture setup/teardown)

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                                                                             setup/teardown.

--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

案例

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个用例成功

collecting ...

 case/test_1.py ✓                                                                                                                                                                                         25% ██▌

 case/test_1.py ⨯✓                                                                                                                                                                                        75% ███████▌

 case/test_1.py ⨯                                                                                                                                                                                        100% ██████████

运行完成后,会在当前的目录生成一个 .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目录

`
(pytest_env) ➜ apiAutomatic pytest –cache-show
Test session starts (platform: darwin, Python 3.7.6, pytest 6.2.1, pytest-sugar 0.9.4)
rootdir: /Users/jkc/PycharmProjects/apiAutomatic, configfile: pytest.ini
plugins: assume-2.4.2, sugar-0.9.4, rerunfailures-9.1.1, base-url-1.4.2, html-3.1.1, metadata-1.11.0, ordering-0.6, cov-2.10.1, repeat-0.9.1, xdist-2.2.0, forked-1.3.0, allure-pytest-2.8.29
cachedir: /Users/jkc/PycharmProjects/apiAutomatic/.pytest_cache

Original: https://blog.csdn.net/weixin_43880991/article/details/116221366
Author: Silent丿丶黑羽
Title: Pytest(12)pytest缓存

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/775770/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • adjustText解决matplotlib plt.text()文字重叠问题

    texts:List型,每个元素都是表示单个文字标签对应的 matplotlib.text.Text对象 ax:绘制文字标签的目标axe对象,默认为最近一次的axe对象 lim:i…

    Python 2023年9月6日
    066
  • pyinstaller64位windows电脑打包32位exe 5500字超详细教程

    壹、方法简介 贰、安装32位python 一、下载安装包 二、安装python 叁、安装第三方库 一、检查安装 二、安装第三方库与pyinstaller 三、pyinstall 肆…

    Python 2023年9月19日
    050
  • Django + Pytest搭建在线自动化测试平台

    一、测试平台:解决分散用例执行方式,提供统一测试用例执行过程、用例管理、测试报告主要是基于:fastapi+vue.jsdjango+vue.jsdjango 二、搭建过程 2.1…

    Python 2023年9月9日
    039
  • 质数、质因数个数和约数个数的求解

    质数也称素数,是指只能被其自身和1整数的正整数。 那么如何判断一个数是否为质数呢?可以用所有小于该数的正整数去试着除该数,若能整数,则不是质数;若这些数都不能整除它,则该数是质数。…

    Python 2023年6月12日
    040
  • [MySQL]事务ACID详解

    目录 1. 事务的概念 2. 事务的特性 3.事务控制语法 4. 事务并发异常 5. 事务隔离级别 1. 事务的概念 事务指逻辑上的一组操作, 组成这组操作的各个单元, 除非全部正…

    Python 2023年9月27日
    031
  • python+requests+pytest 接口自动化框架(六)

    目录 一、规范YAML测试用例 1、一级关键字必须要包含:name,request,validate 2、在request下必需包含:method,url 3、传参方式: 二、接口…

    Python 2023年9月11日
    052
  • Python-apply()、applymap()、map()的使用和区别

    将一个自定义的函数应用到Pandas的数据结构中可以使用map(), apply()或者applymap(),它们的区别在于应用的对象不同。 1.map() 是一个Series的方…

    Python 2023年8月17日
    060
  • windowsServer环境下的flask+nginx部署方案

    一、部署环境 • winserver2012 • python3.8.5 • nginx1.19.10 • tornado • IDE: PyCharm 二、下载安装nginx 下…

    Python 2023年8月15日
    063
  • Python 字典dict详解(超详细)

    字典是一个 无序、 可变和 有索引的集合。在 Python 中,字典用花括号编写,拥有 键和 值。 1、创建字典 在Python中,我们可以使用 {} 或者 dict() 函数来创…

    Python 2023年8月1日
    054
  • pandas筛选与替换

    需求: 在表格中,按照相应的条件,把数据筛选出来,然后把筛选出来的数据进行赋值后,再放回原来的表格。 一、excel表格筛选 要实现这样的功能,在excel2016之前是不容易的,…

    Python 2023年8月7日
    036
  • 零配置python日志,安装即用

    写了很多年的python日志都是使用内置的logging模块来完成的,使用时都需要经过繁琐的配置。有时候,还可能出现丢失日志的情况,直到loguru的出现。 【阅读全文】 logu…

    Python 2023年11月2日
    058
  • ROS中的时间

    ros::Time t1 = ros::Time::now(); double t_cur = t1.toSec(); printf("The time is: %16f…

    Python 2023年8月8日
    092
  • 发现几个好玩的游戏编程平台,与君共勉!

    俗话说:兴趣是最好的老师,编程也是。如果你一开始就接触到了枯燥的编码,有些人难免会放弃,而最好的方法就是感兴趣,一步一步地变得更好。 [En] As the saying goes…

    Python 2023年5月24日
    059
  • Python中的range函数

    大家好,我是翔宇!今天我们聊聊Python中很重要的range对象!(本文章基于Python3环境,Python2环境下的range会有所不同,但并不影响我们使) range对象 …

    Python 2023年8月1日
    084
  • 滑动窗口分位数

    滑动窗口分位数 分位数计算公式 分位数的计算公式有 PERCETILE.INC和 PERCENTILE.EXC两种,两个公式的计算逻辑是完全一样的,仅仅取数的范围大小不一样,这里我…

    Python 2023年10月14日
    053
  • Python操作Redis详解

    介绍 Redis是一个开源的基于内存也可持久化的Key-Value数据库,采用ANSI C语言编写。它拥有丰富的数据结构,拥有事务功能,保证命令的原子性。由于是内存数据库,读写非常…

    Python 2023年8月2日
    045
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球