目錄
先給大家分享下學習資源
1. 安裝pytest
2. 編寫用例規則
3. 執行用例
最近在學習pytest的用法 并且用這套框架替換了原來的unittest, 同是測試框架 確實感覺到pytest更加便捷 這邊分享給大家我得學習心得
先給大家分享下學習資源
1 官方文檔
-
pytest 官方文檔?永遠是是最全面的學習資源。
2 書籍 (這個是deepseek給的 我沒看
-
《Python Testing with pytest》:深入講解?
pytest
?的使用和最佳實踐。、
3 B站 (小破站永遠的神!隨便搜個看看都行 都挺好的 關鍵詞:pytest測試框架
1. 安裝pytest
pip install pytest # 在pycharm terminal中直接輸入這個命令去安裝pytest
pytest -v # 驗證是否安裝成功
2. 編寫用例規則
- 測試文件需要以test_開頭,或者以_test結尾,例如 test_example.py 或 example_test.py
- 測試函數需要以test_開頭, 例如test_case()
- 測試以Test開頭,例如TestMethod
- 使用assert斷言,?例如?
assert actually_result == expected_result
附:pytest會自動識別test_開頭或結尾的文件并運行其中test_開頭的用例
def test_api():user info = {"username"":sanmu"""password":"123456"}resp = session.post('https://baidu.com', json=user info)print(resp)assert resp.status_code ==200
3. 執行用例
- 命令行
?pytest test_example.py
- 代碼
import pytestpytest.main() # 啟動pytest測試框架
下一階段入口:
pytest核心功能(進階用法)-CSDN博客
pytest測試框架所需文件以及各自位置-CSDN博客