pytest运行_python缓存机制

pytest运行_python缓存机制前言pytest运行完用例之后会生成一个.pytest_cache的缓存文件夹,用于记录用例的ids和上一次失败的用例。方便我们在运行用例的时候加上–lf和–ff参数,快速运行上一

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

前言

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 的缓存文件夹,层级结构如下

pytest运行_python缓存机制

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
------------------------------------------------------------------------------------------------- cache values for '*' -------------------------------------------------------------------------------------------------
cache/lastfailed contains:
  {'case/test_1.py::test_02': True, 'case/test_1.py::test_04': True}
cache/nodeids contains:
  ['case/test_1.py::test_01',
   'case/test_1.py::test_02',
   'case/test_1.py::test_03',
   'case/test_1.py::test_04']
cache/stepwise contains:
  []
Results (0.02s):

 

–cache-clear

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/165682.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月30日 上午7:36
下一篇 2022年7月30日 上午7:46


相关推荐

  • 利用FormData对象实现AJAX文件上传功能及后端实现「建议收藏」

    利用FormData对象实现AJAX文件上传功能及后端实现

    2022年2月11日
    50
  • Java的Volatile关键字的作用

    Java的Volatile关键字的作用VolatileVolatile概念是Java特有的。如果你理解它解决的问题,会更容易理解它。如果一个变量,例如一个计数器正在被一个线程使用,有可能计数器的一个副本放在CPU高速缓存里,并且每次修改时,仅修改高速缓存的内容,而不是写到主存。JVM会决定什么时候更新主存。甚至当其他线程从主存里读取计数器的值时,有可能读到过期的值。如果一个变量用volatile修饰,不论一个线程读还是写此变量,总是从主存里读写。作为进一步的保证,当一个写线程写一个volatile修饰的变量时,写线程能看到的所有变

    2022年5月16日
    37
  • rdesktop教程_rdesktop 退出全屏

    rdesktop教程_rdesktop 退出全屏我的配置:1.准备工作:ubuntu端:Windows端:1.计算机属性远程设置远程,勾选:允许远程连接到此计算机;2.远程桌面–允许ubuntu端,执行命令:2.命令解释

    2022年8月4日
    9
  • Nemotron Speech ASR低延迟英文实时转写的语音识别服务;GLM-Image开源混合自回归与扩散解码架构的图像生成模型

    Nemotron Speech ASR低延迟英文实时转写的语音识别服务;GLM-Image开源混合自回归与扩散解码架构的图像生成模型

    2026年3月12日
    2
  • 【TCP/IP】IP地址的划分及其分类

    【TCP/IP】IP地址的划分及其分类了解Internet中使用的网络层地址,又称IP地址。每个设备都至少需要一个IP地址,其可以作为我们设备的标识,就跟我们的电话号码一样,知道了电话号码就能找到我们,所以每个IP地址都是唯一的,所以在给每台设备分配IP时,会根据一套编号方案进行。IP作用于OSI参考模型中的网络层,在终端通信中作为唯一标识,便于确定数据的传递目标。IP地址分为:IPv4、IPv6大多数用户熟悉并且流行的IP地址是IPv4,其是用点分四组十进制的表示方法展示的,例如165.195.130.107

    2022年6月14日
    32
  • redis 6379端口不通解决方法「建议收藏」

    redis 6379端口不通解决方法「建议收藏」1.reids服务器的6379端口telnet不通2. 查看reids进程和端口,都是存在的。只是ip地址是127.0.0.1而不是0.0.0.0,只是本机能使用3.查找redis的配置文件redis.conf 使用命令:find/-nameredis.conf 4.查找bind127.0.0.1所在的行数  使用命令:cat/usr/local/re…

    2022年5月9日
    162

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号