pytest文檔51-內置fixture之cache使用


前言

pytest 運行完用例之后會生成一個 .pytest_cache 的緩存文件夾,用於記錄用例的ids和上一次失敗的用例。
方便我們在運行用例的時候加上--lf 和 --ff 參數,快速運行上一次失敗的用例。
--lf, --last-failed 只重新運行上次運行失敗的用例(或如果沒有失敗的話會全部跑)
--ff, --failed-first 運行所有測試,但首先運行上次運行失敗的測試(這可能會重新測試,從而導致重復的fixture setup/teardown)
--lf 和 --ff 相關介紹查看之前的這篇https://www.cnblogs.com/yoyoketang/p/9769559.html

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
--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

測試案例代碼test_x.py

# test_x.py
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/


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個用例成功

>pytest test_x.py --tb=no
============================= test session starts =============================
collected 4 items

test_x.py .F.F                                                           [100%]

===================== 2 failed, 2 passed in 0.11 seconds ======================

運行完成后,會在當前的目錄生成一個 .pytest_cache 的緩存文件夾,層級結構如下

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目錄

D:\soft\kecheng202004\xuexi>pytest --cache-show
============================= test session starts =============================
cachedir: \.pytest_cache
---------------------------- cache values for '*' -----------------------------
cache\lastfailed contains:
  {'test_x.py::test_02': True, 'test_x.py::test_04': True}
cache\nodeids contains:
  ['test_x.py::test_01',
   'test_x.py::test_02',
   'test_x.py::test_03',
   'test_x.py::test_04']
cache\stepwise contains:
  []

======================== no tests ran in 0.02 seconds =========================

--cache-clear

--cache-clear 用於在測試用例開始之前清空cache的內容

pytest --cache-clear

查看pytest關於cache的更多文檔 https://docs.pytest.org/en/latest/cache.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM