1、pytest 是 python 的第三方單元測試框架,比自帶 unittest 更簡潔和高效
2、安裝 pytest
pip install pytest
3、驗證 pytest 是否安裝成功
pip show pytest
4、使用 pytest 執行測試需要遵行的規則
-
搜索根目錄:默認從當前目錄中搜集測試用例,即在哪個目錄下運行pytest命令,則從哪個目錄當中搜索
-
符合命名規則 test_*.py 或者 *_test.py 的文件
-
文件中識別用例的規則:
-
以test_開頭的函數名
-
以Test開頭的測試類(沒有__init__函數)當中,以test_開頭的函數
-
-
斷言必須使用 assert,pytest 中沒有自帶的斷言方法
5、pytest 執行方式
- 指定文件/模塊
- pytest –v filename(最高級別信息 — verbose)
- pytest -s filename(輸出打印)
- pytest -v -s filename
- pytest -q filename(靜默輸出,不會打印用例輸出)
- 指定一個或多個目錄
- pytest dir01 # dir01 為用例目錄
- pytest dir01 dir02
- 指定運行py文件中的某一個類
- pytest test_demo1.py::Test02
- test_demo1.py 為文件名稱
- Test02 為類名稱
- pytest test_demo1.py::Test02
- 指定運行某個類中的某一個方法
- pytest test_demo1.py::Test01::test_01
- test_demo1.py 為文件名稱
- Test01 為類名稱
- test_01 為方法名稱
- pytest test_demo1.py::Test01::test_01
6、實例介紹一
- dos 窗口中執行用例
# test_demo1.py def test_01(): print("深圳多測師") def test_02(): print("廣州多測師") def test_03(): print("上海多測師")
-
- 執行方法一:pytest -q test_demo1.py
-
- 執行方法二:pytest -s test_demo1.py
-
- 執行方法三:pytest -v test_demo1.py
-
- 執行方法四:pytest -v -s test_demo1.py
7、實例介紹二
- demo1
- demo1 發現結果中沒有用例的執行打印結果
# test_demo.py import pytest def test_01(): print("深圳多測師") def test_02(): print("廣州多測師") def test_03(): print("上海多測師") if __name__ == '__main__': pytest.main() # 結果如下 ============================= test session starts ============================= platform win32 -- Python 3.6.0, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 rootdir: D:\work_doc\CodeFile\practice\pytest_demo plugins: html-2.1.1, metadata-1.9.0 collected 3 items test_demo1.py ... [100%] ============================== 3 passed in 0.05s ============================== Process finished with exit code 0
- demo2
- demo2 中在 main() 中加入 ["-s"],結果中就可以展示打印結果
import pytest def test_01(): print("深圳多測師") def test_02(): print("廣州多測師") def test_03(): print("上海多測師") if __name__ == '__main__': pytest.main(["-s"]) # 在 main() 中加入 ["-s"] # 結果如下 ============================= test session starts ============================= platform win32 -- Python 3.6.0, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 rootdir: D:\work_doc\CodeFile\practice\pytest_demo plugins: html-2.1.1, metadata-1.9.0 collected 3 items test_demo1.py 深圳多測師 .廣州多測師 .上海多測師 . ============================== 3 passed in 0.02s ============================== Process finished with exit code 0
- demo3
- 運行指定的用例文件
# test_demo1.py def test_01(): print("深圳多測師") def test_02(): print("廣州多測師") def test_03(): print("上海多測師")
# run_all.py import pytest if __name__ == '__main__': pytest.main(["D:/test_demo1.py"]) # 結果如下 ============================= test session starts ============================= platform win32 -- Python 3.6.0, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 rootdir: D:\ plugins: html-2.1.1, metadata-1.9.0 collected 3 items test_demo1.py ... [100%] ============================== 3 passed in 0.01s ============================== Process finished with exit code 0
- demo4
- 運行指定目錄下的用例文件
# demo1/test_demo1.py demo1 目錄下的 test_demo1.py 文件 def test_01(): print("深圳多測師") def test_02(): print("廣州多測師") def test_03(): print("上海多測師")
# demo2/test_demo2.py demo2 目錄下的 test_demo2.py 文件 def test_04(): print("杭州多測師") def test_05(): print("北京多測師") def test_06(): print("重慶多測師")
import pytest if __name__ == '__main__': pytest.main(["D:/demo1","D:/demo2"]) # 指定 D:/demo1 和 D:/demo2 目錄 # 結果如下 ============================= test session starts ============================= platform win32 -- Python 3.6.0, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 rootdir: D: plugins: html-2.1.1, metadata-1.9.0 collected 6 items demo1\test_demo1.py ... [ 50%] demo2\test_demo2.py ... [100%] ============================== 6 passed in 0.04s ============================== Process finished with exit code 0
8、pytest-html 生成測試報告
- 安裝 pytest-html
pip install pytest-html
- cmd 下執行用例且生成報告
- 代碼中生成測試報告
import pytest def test_01(): print("深圳多測師") def test_02(): print("廣州多測師") def test_03(): print("上海多測師") if __name__ == '__main__': pytest.main(["-q","test_demo1.py","--html=report.html"]) # --html=report.html 生成測試報告,也可以自定義報告路徑如:--html=d://report.html