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

相关文章

linux进程D状态的解决思路分享

《linux进程D状态的解决思路分享》在Linux系统中,进程在内核模式下等待I/O完成时会进入不间断睡眠状态(D状态),这种状态下,进程无法通过普通方式被杀死,本文通过实验模拟了这种状态,并分析了如... 目录1. 问题描述2. 问题分析3. 实验模拟3.1 使用losetup创建一个卷作为pv的磁盘3.

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

MySQL 缓存机制与架构解析(最新推荐)

《MySQL缓存机制与架构解析(最新推荐)》本文详细介绍了MySQL的缓存机制和整体架构,包括一级缓存(InnoDBBufferPool)和二级缓存(QueryCache),文章还探讨了SQL... 目录一、mysql缓存机制概述二、MySQL整体架构三、SQL查询执行全流程四、MySQL 8.0为何移除查

通过prometheus监控Tomcat运行状态的操作流程

《通过prometheus监控Tomcat运行状态的操作流程》文章介绍了如何安装和配置Tomcat,并使用Prometheus和TomcatExporter来监控Tomcat的运行状态,文章详细讲解了... 目录Tomcat安装配置以及prometheus监控Tomcat一. 安装并配置tomcat1、安装

Linux之进程状态&&进程优先级详解

《Linux之进程状态&&进程优先级详解》文章介绍了操作系统中进程的状态,包括运行状态、阻塞状态和挂起状态,并详细解释了Linux下进程的具体状态及其管理,此外,文章还讨论了进程的优先级、查看和修改进... 目录一、操作系统的进程状态1.1运行状态1.2阻塞状态1.3挂起二、linux下具体的状态三、进程的

Redis缓存问题与缓存更新机制详解

《Redis缓存问题与缓存更新机制详解》本文主要介绍了缓存问题及其解决方案,包括缓存穿透、缓存击穿、缓存雪崩等问题的成因以及相应的预防和解决方法,同时,还详细探讨了缓存更新机制,包括不同情况下的缓存更... 目录一、缓存问题1.1 缓存穿透1.1.1 问题来源1.1.2 解决方案1.2 缓存击穿1.2.1

解决Cron定时任务中Pytest脚本无法发送邮件的问题

《解决Cron定时任务中Pytest脚本无法发送邮件的问题》文章探讨解决在Cron定时任务中运行Pytest脚本时邮件发送失败的问题,先优化环境变量,再检查Pytest邮件配置,接着配置文件确保SMT... 目录引言1. 环境变量优化:确保Cron任务可以正确执行解决方案:1.1. 创建一个脚本1.2. 修

Redis与缓存解读

《Redis与缓存解读》文章介绍了Redis作为缓存层的优势和缺点,并分析了六种缓存更新策略,包括超时剔除、先删缓存再更新数据库、旁路缓存、先更新数据库再删缓存、先更新数据库再更新缓存、读写穿透和异步... 目录缓存缓存优缺点缓存更新策略超时剔除先删缓存再更新数据库旁路缓存(先更新数据库,再删缓存)先更新数

el-select下拉选择缓存的实现

《el-select下拉选择缓存的实现》本文主要介绍了在使用el-select实现下拉选择缓存时遇到的问题及解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录项目场景:问题描述解决方案:项目场景:从左侧列表中选取字段填入右侧下拉多选框,用户可以对右侧

SpringBoot使用注解集成Redis缓存的示例代码

《SpringBoot使用注解集成Redis缓存的示例代码》:本文主要介绍在SpringBoot中使用注解集成Redis缓存的步骤,包括添加依赖、创建相关配置类、需要缓存数据的类(Tes... 目录一、创建 Caching 配置类二、创建需要缓存数据的类三、测试方法Spring Boot 熟悉后,集成一个外