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

相关文章

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

hdu1565(状态压缩)

本人第一道ac的状态压缩dp,这题的数据非常水,很容易过 题意:在n*n的矩阵中选数字使得不存在任意两个数字相邻,求最大值 解题思路: 一、因为在1<<20中有很多状态是无效的,所以第一步是选择有效状态,存到cnt[]数组中 二、dp[i][j]表示到第i行的状态cnt[j]所能得到的最大值,状态转移方程dp[i][j] = max(dp[i][j],dp[i-1][k]) ,其中k满足c

缓存雪崩问题

缓存雪崩是缓存中大量key失效后当高并发到来时导致大量请求到数据库,瞬间耗尽数据库资源,导致数据库无法使用。 解决方案: 1、使用锁进行控制 2、对同一类型信息的key设置不同的过期时间 3、缓存预热 1. 什么是缓存雪崩 缓存雪崩是指在短时间内,大量缓存数据同时失效,导致所有请求直接涌向数据库,瞬间增加数据库的负载压力,可能导致数据库性能下降甚至崩溃。这种情况往往发生在缓存中大量 k

状态dp总结

zoj 3631  N 个数中选若干数和(只能选一次)<=M 的最大值 const int Max_N = 38 ;int a[1<<16] , b[1<<16] , x[Max_N] , e[Max_N] ;void GetNum(int g[] , int n , int s[] , int &m){ int i , j , t ;m = 0 ;for(i = 0 ;

hdu3006状态dp

给你n个集合。集合中均为数字且数字的范围在[1,m]内。m<=14。现在问用这些集合能组成多少个集合自己本身也算。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.Inp

从状态管理到性能优化:全面解析 Android Compose

文章目录 引言一、Android Compose基本概念1.1 什么是Android Compose?1.2 Compose的优势1.3 如何在项目中使用Compose 二、Compose中的状态管理2.1 状态管理的重要性2.2 Compose中的状态和数据流2.3 使用State和MutableState处理状态2.4 通过ViewModel进行状态管理 三、Compose中的列表和滚动

实例:如何统计当前主机的连接状态和连接数

统计当前主机的连接状态和连接数 在 Linux 中,可使用 ss 命令来查看主机的网络连接状态。以下是统计当前主机连接状态和连接主机数量的具体操作。 1. 统计当前主机的连接状态 使用 ss 命令结合 grep、cut、sort 和 uniq 命令来统计当前主机的 TCP 连接状态。 ss -nta | grep -v '^State' | cut -d " " -f 1 | sort |

状态模式state

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/state 在一个对象的内部状态变化时改变其行为, 使其看上去就像改变了自身所属的类一样。 在状态模式中,player.getState()获取的是player的当前状态,通常是一个实现了状态接口的对象。 onPlay()是状态模式中定义的一个方法,不同状态下(例如“正在播放”、“暂停

Redis中使用布隆过滤器解决缓存穿透问题

一、缓存穿透(失效)问题 缓存穿透是指查询一个一定不存在的数据,由于缓存中没有命中,会去数据库中查询,而数据库中也没有该数据,并且每次查询都不会命中缓存,从而每次请求都直接打到了数据库上,这会给数据库带来巨大压力。 二、布隆过滤器原理 布隆过滤器(Bloom Filter)是一种空间效率很高的随机数据结构,它利用多个不同的哈希函数将一个元素映射到一个位数组中的多个位置,并将这些位置的值置

防止缓存击穿、缓存穿透和缓存雪崩

使用Redis缓存防止缓存击穿、缓存穿透和缓存雪崩 在高并发系统中,缓存击穿、缓存穿透和缓存雪崩是三种常见的缓存问题。本文将介绍如何使用Redis、分布式锁和布隆过滤器有效解决这些问题,并且会通过Java代码详细说明实现的思路和原因。 1. 背景 缓存穿透:指的是大量请求缓存中不存在且数据库中也不存在的数据,导致大量请求直接打到数据库上,形成数据库压力。 缓存击穿:指的是某个热点数据在