Pytest 配置檔案ini檔案詳解(二)
阿新 • • 發佈:2022-04-09
Pytest ini配置檔案介紹
pytest.ini配置檔案介紹:
pytest.ini檔案他是單元測試框架pytest單元測試框架的核心配置檔案,Pytest.ini配置檔案可以改變pytest一些預設的執行方式,
通過設定該配置檔案,可以按照使用者行為習慣進行改進執行過程,如:用例收集規則,標籤,命令列引數等等。
1)pytest.ini檔案存放的位置:一般ini檔案存放在專案的根目錄
2)pytest.ini檔案編碼格式必須為ANSI檔案格式可以通過相關工具進行修改格式
3)Pyest.ini執行規則:直接介紹了pytest的執行方式,主要分為主函式執行,以及命令列模式執行,這兩種的執行模式,在執行前都會
去讀取Pytest.ini這個配置檔案
Pytest主要的配置引數:
具體配置如下,我們每一個配置進行演示
[pytest] addopts = "-vs" testpaths = "./case_api" python_files = "api*.py" python_classes = "Aaa" python_functions = "cc" markers = smoke:冒煙測試用例 usermanage:使用者管理模組測試用例
addopts:主要用來配置執行用例時,檢視cesa的一些配置引數,我這裡設定的是 "vs",那麼
在執行時,就會預設加上-vs引數
示例: 這裡我在執行時沒有加上vs引數,他讀取ini配置檔案之後預設加上了vs引數,
C:\Users\admin\PycharmProjects\wuhan12>pytest #執行用例
結果:
collected 6 items case_api/api_ddt_demo03.py::Test::test01 hello word(1) PASSED case_api/api_ddt_demo03.py::Test::test02 SKIPPED (unconditional skip) case_api/api_ddt_demo03.py::Test::test03 hello word(3) PASSED case_api/api_ddt_demo03.py::Test::test04 hello word(4) PASSED case_api/api_ddt_demo03.py::Test::test05 hello word(5) PASSED case_api/api_ddt_demo03.py::Test::test06 FAILED ================================= FAILURES ================================= _______________________________ Test.test06 ________________________________ self = <case_api.api_ddt_demo03.Test testMethod=test06> def test06(self): """ assertFalse: 當我返回的結果為False 則斷言通過""" self.g = "123456" > self.assertFalse(self.g.startswith("1"), msg="斷言失敗") E AssertionError: True is not false : 斷言失敗 case_api\api_ddt_demo03.py:211: AssertionError ========================= short test summary info ========================== FAILED case_api/api_ddt_demo03.py::Test::test06 - AssertionError: True is ... ================== 1 failed, 4 passed, 1 skipped in 0.60s ==================
testpaths:配置預設讀取執行的專案路徑-->這裡可以看到pytest預設執行的用例的目錄為./case_api目錄
[pytest] testpaths = "./case_api" #執行結果: C:\Users\admin\PycharmProjects\>pytest paths: ./case_api plugins: allure-pytest-2.9.45, forked-1.4.0, html-3.1.1, metadata-2.0.1, orde ring-0.6, rerunfailures-10.2, xdist-2.5.0 collected 6 items
python_files :模組明的命名規則,pytest預設查詢的模組是以test進行開頭的這裡我修改了以api開頭的
模組,那麼pytest就預設查詢api開頭的模組進行執行用例
case_api/api_ddt_demo03.py::Test::test01 hello word(1)
python_classes :修改pytest框架預設查詢類的開頭預設是查詢class開頭的類容,這裡我改成了Aaa開頭那麼會
自動載入Aaa開頭的類
python_functions :修改pytest框架預設查詢類的開頭預設是查詢函式方法開頭的用例,這裡我改成了cc開頭
那麼會自動載入cc開頭的用例
markers:是分組管理的功能,比如我們執行的用例會包含冒煙測試用例,那麼我們想要執行冒煙測試用例怎麼
辦呢?這個時候就可以使用markes進行分組執行用例
示例: 我設定ini 檔案smoke是冒煙測試用例,我的case1 case2都進行標記進行執行可以看到只執行了冒煙測試用例
執行語法pytest main([-m smoke])
#配置詳情: [pytest] markers = smoke:冒煙測試用例 #進行用例標記 import pytest class Test_Case: @pytest.mark.smoke @pytest.mark.run(order=5) def test01(self): print(" case 01") @pytest.mark.smoke @pytest.mark.run(order=4) def test02(self): print(" case 02") @pytest.mark.run(order=3) def test03(self): print(" case 03") @pytest.mark.run(order=2) def test05(self): print(" case 04") @pytest.mark.run(order=1) def test06(self): print(" case 05") #輸出結果 case_api/test_case01.py::Test_Case::test02 case 02 PASSED case_api/test_case01.py::Test_Case::test01 case 01 PASSED ======================= 2 passed, 5 deselected in 0.57s