相信很多使用pytest的,都知道pytest有運行上次失敗用例的參數,如下:
--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 setup/teardown. --nf, --new-first run tests from new files first, then the rest of the tests sorted by file mtime
但實際原理是什麼呢?
我們還是來看看官方的說明:
plugins may access the config.cache object to set/get json encodable values between pytest invocations.
這說明會生成一個cache文件,這個文件在我們執行時會說明,如下:
用這裏可以看出,這是一個隱藏文件,所以我們在pycharm中看不到。打開文件夾可以看到
再繼續下去,有這樣一個文件:.pytest_cache\v\cache\lastfailed
我們打開,是不是發現原來所有錯誤的用例都放在這裏。
我們也可以通過以下幾種方式驗證:
僅僅收集不執行錯誤用例
pytest --lf --co
查看cache裏的內容
pytest --cache-show
清空cache
pytest --cache-clear