pytest之缓存测试运行状态

2023-12-13 03:48

本文主要是介绍pytest之缓存测试运行状态,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

缓存

pytest执行后会自动缓存运行失败等各种结果态的用例
--lf, --last-failed 仅重新运行失败的用例
--ff,–failed-first 先运行上次失败的用例,然后再运行剩余的其他用例
--nf,–new-first选项:首先运行新测试,然后再进行其余测试,在这两种情况下,测试还按文件修改时间排序,最新的文件排在第一位。
--cache-show,用来窥视缓存的内容
–cache-clear,用来在开始一轮新的测试前清理所有的缓存。(一般不需要)

1,首次执行50条用例,其中会有两条失败

import pytest@pytest.mark.parametrize("i", range(50))
def test_num(i):if i in (17, 25):pytest.fail("bad luck")

运行结果:如预期当1=17和25时,出现两条失败

PS autotest> pytest -q test_study.py
.................F.......F........................                       [100%]
=============================== FAILURES ==============================
_____________________________ test_num[17] ___________________________
i = 17@pytest.mark.parametrize("i", range(50))def test_num(i):if i in (17, 25):
>           pytest.fail("bad luck")
E           Failed: bad luck
test_study3.py:31: Failed
____________________________ test_num[25] ___________________________
i = 25@pytest.mark.parametrize("i", range(50))def test_num(i):if i in (17, 25):
>           pytest.fail("bad luck")
E           Failed: bad luck
test_study3.py:31: Failed
2 failed, 48 passed in 0.24 seconds

2,然后使用以下命令运行它--lf
自上次运行以来,仅运行了2个失败的测试,而尚未运行48个通过测试(“deselected取消选中”):

PS autotest> pytest --lf -q test_study.py
FF                                                                       [100%]
================================== FAILURES ===================================
________________________________ test_num[17] _________________________________
i = 17@pytest.mark.parametrize("i", range(50))def test_num(i):if i in (17, 25):
>           pytest.fail("bad luck")
E           Failed: bad luck
test_study3.py:31: Failed
________________________________ test_num[25] _________________________________
i = 25@pytest.mark.parametrize("i", range(50))def test_num(i):if i in (17, 25):
>           pytest.fail("bad luck")
E           Failed: bad luck
test_study3.py:31: Failed
2 failed, 48 deselected in 0.15 seconds

3,如果使用该--ff选项运行
将运行所有测试,但是将首先执行之前的失败用例(从FF和点系列可以看出),再执行其他用例:

PS autotest> pytest --ff -q test_study.py
FF................................................                       [100%]
================================== FAILURES ===================================
________________________________ test_num[17] _________________________________
i = 17@pytest.mark.parametrize("i", range(50))def test_num(i):if i in (17, 25):
>           pytest.fail("bad luck")
E           Failed: bad luck
test_study3.py:31: Failed
________________________________ test_num[25] _________________________________
i = 25@pytest.mark.parametrize("i", range(50))def test_num(i):if i in (17, 25):
>           pytest.fail("bad luck")
E           Failed: bad luck
test_study3.py:31: Failed
2 failed, 48 passed in 0.18 seconds

4,使用--nf命令
首先运行新测试,然后再进行其余测试,在这两种情况下,测试还按文件修改时间排序,最新的文件排在第一位。
在之前运行失败后基础上,在测试文件中新加一个测试函数test_new

import pytest@pytest.mark.parametrize("i", range(50))
def test_num(i):if i in (17,25):pytest.fail("bad luck")
def test_new(): #新加的测试函数assert 1!=1

使用--nf命令执行(特意让test_new执行失败),可以看到先执行新增的测试用例test_new,再继续执行之前的用例(从F和.顺序可以看出)

PS autotest> pytest --nf -q test_study3.py
F.................F.......F........................                      [100%]
================================== FAILURES ===================================
__________________________________ test_new ___________________________________def test_new():
>       assert 1!=1
E       assert 1 != 1
test_study3.py:33: AssertionError
________________________________ test_num[17] _________________________________
i = 17@pytest.mark.parametrize("i", range(50))def test_num(i):if i in (17,25):
>           pytest.fail("bad luck")
E           Failed: bad luck
test_study3.py:31: Failed
________________________________ test_num[25] _________________________________
i = 25@pytest.mark.parametrize("i", range(50))def test_num(i):if i in (17,25):
>           pytest.fail("bad luck")
E           Failed: bad luck
test_study3.py:31: Failed
3 failed, 48 passed in 0.24 seconds

5,没有测试失败的数据 --实际验证不管用,无论使用all还是none参数都把用例全部执行了

如果最后一次运行没有测试失败,或者找不到缓存的lastfailed数据,pytest则可以使用–last-failed-no-failures选项将其配置为运行所有测试或不运行测试

pytest --last-failed --last-failed-no-failures all    # run all tests (default behavior)
pytest --last-failed --last-failed-no-failures none   # run no tests and exit

操作结果,以上两个参数都试过,但是都全部执行了:

PS autotest> pytest -q test_study3.py
...................................................                      [100%]
51 passed in 0.14 secondsPS autotest> pytest --last-failed --last-failed-no-failures none test_study.py
============================= test session starts =============================
collected 51 items
test_study3.py ...................................................       [100%]
========================== 51 passed in 0.15 seconds ==========================PS autotest> pytest --last-failed --last-failed-no-failures all test_study.py
============================= test session starts =============================
collected 51 items
test_study3.py ...................................................       [100%]
========================== 51 passed in 0.18 seconds ==========================

6,检查缓存内容--cache-show命令
可以使用--cache-show命令行选项来窥视缓存的内容 :
虽然执行时我指定了test_study3.py文件,但是也会把当前目录下的其他test文件都执行了

PS autotest> pytest --cache-show test_study3.py
============================= test session starts =============================
cachedir: autotest\.pytest_cache
-------------------------------- cache values ---------------------------------
cache\lastfailed contains:{'test_study.py::test_username[1]': True,'test_study.py::test_username[2]': True,'test_study.py::test_username[3]': True,'test_study3.py::test_num[2]': True,'test_study3.py::test_num[6]': True}
cache\nodeids contains:['test_study.py::test_username[1]','test_study.py::test_username[2]','test_study.py::test_username[3]','test_study3.py::test_num[0]','test_study3.py::test_num[1]','test_study3.py::test_num[2]','test_study3.py::test_num[3]','test_study3.py::test_num[4]','test_study3.py::test_num[5]','test_study3.py::test_num[6]','test_study3.py::test_num[7]','test_study3.py::test_num[8]','test_study3.py::test_num[9]','test_some/test_study2.py::test_username1']
cache\stepwise contains:[]

config.cache --待更新

其他模块
逐步调试:?
使用–sw, --stepwise 允许你每次运行的时候都修复一个用例。测试集将运行到第一个失败的用例,然后自行停止,下一次再调用时,测试集将从上次失败的用例继续运行,然后运行到下一次失败的测试用例再停止。你可以使用–stepwise-skip选项来跳过一个失败的用例,在下一个失败的用例处再停止,如果你一直搞不定当前的失败的用例且只想暂时忽略它,那么这个选项非常有用。

这篇关于pytest之缓存测试运行状态的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/487029

相关文章

pytest+allure环境搭建+自动化实践过程

《pytest+allure环境搭建+自动化实践过程》:本文主要介绍pytest+allure环境搭建+自动化实践过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、pytest下载安装1.1、安装pytest1.2、检测是否安装成功二、allure下载安装2.

IIS 7.0 及更高版本中的 FTP 状态代码

《IIS7.0及更高版本中的FTP状态代码》本文介绍IIS7.0中的FTP状态代码,方便大家在使用iis中发现ftp的问题... 简介尝试使用 FTP 访问运行 Internet Information Services (IIS) 7.0 或更高版本的服务器上的内容时,IIS 将返回指示响应状态的数字代

Java实现本地缓存的常用方案介绍

《Java实现本地缓存的常用方案介绍》本地缓存的代表技术主要有HashMap,GuavaCache,Caffeine和Encahche,这篇文章主要来和大家聊聊java利用这些技术分别实现本地缓存的方... 目录本地缓存实现方式HashMapConcurrentHashMapGuava CacheCaffe

如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)

《如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)》:本文主要介绍如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)问题,具有很好的参考价值,希望对大家有所帮助,如有... 目录先在你打算存放的地方建四个文件夹更改这四个路径就可以修改默认虚拟内存分页js文件的位置接下来从高级-

PyCharm如何更改缓存位置

《PyCharm如何更改缓存位置》:本文主要介绍PyCharm如何更改缓存位置的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录PyCharm更改缓存位置1.打开PyCharm的安装编程目录2.将config、sjsystem、plugins和log的路径

JSR-107缓存规范介绍

《JSR-107缓存规范介绍》JSR是JavaSpecificationRequests的缩写,意思是Java规范提案,下面给大家介绍JSR-107缓存规范的相关知识,感兴趣的朋友一起看看吧... 目录1.什么是jsR-1072.应用调用缓存图示3.JSR-107规范使用4.Spring 缓存机制缓存是每一

Spring 缓存在项目中的使用详解

《Spring缓存在项目中的使用详解》Spring缓存机制,Cache接口为缓存的组件规范定义,包扩缓存的各种操作(添加缓存、删除缓存、修改缓存等),本文给大家介绍Spring缓存在项目中的使用... 目录1.Spring 缓存机制介绍2.Spring 缓存用到的概念Ⅰ.两个接口Ⅱ.三个注解(方法层次)Ⅲ.

Spring Boot 整合 Redis 实现数据缓存案例详解

《SpringBoot整合Redis实现数据缓存案例详解》Springboot缓存,默认使用的是ConcurrentMap的方式来实现的,然而我们在项目中并不会这么使用,本文介绍SpringB... 目录1.添加 Maven 依赖2.配置Redis属性3.创建 redisCacheManager4.使用Sp

springboot项目redis缓存异常实战案例详解(提供解决方案)

《springboot项目redis缓存异常实战案例详解(提供解决方案)》redis基本上是高并发场景上会用到的一个高性能的key-value数据库,属于nosql类型,一般用作于缓存,一般是结合数据... 目录缓存异常实践案例缓存穿透问题缓存击穿问题(其中也解决了穿透问题)完整代码缓存异常实践案例Red

Spring三级缓存解决循环依赖的解析过程

《Spring三级缓存解决循环依赖的解析过程》:本文主要介绍Spring三级缓存解决循环依赖的解析过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、循环依赖场景二、三级缓存定义三、解决流程(以ServiceA和ServiceB为例)四、关键机制详解五、设计约