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 seconds

PS 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选项来跳过一个失败的用例,在下一个失败的用例处再停止,如果你一直搞不定当前的失败的用例且只想暂时忽略它,那么这个选项非常有用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值