7.配置
7.1 pytest配置文件概念
pytest常見的非測試文件,如下所示:
- 1.pytest.ini:pytest的主配置文件,可以改變pytest的默認行爲,其中有很多可配置的選項
- 2.conftest.py:本地的插件庫,其中的hook函數和fixture將作用於該文件所在的目錄及其子目錄
- 3.__init__:每個測試子目錄可能都包含該文件,代表一個包,常用於解決命名衝突
pytest.ini示例如下所示:
[pytest]
addopts = -rsxX -L --tb=short --strict
xfail_stric = true
...
7.1.1 使用pytest --help查看ini文件選項
>>> pytest --help
[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:
markers (linelist): markers for test functions
empty_parameter_set_mark (string): default marker for empty parametersets
norecursedirs (args): directory patterns to avoid for recursion
testpaths (args): directories to search for tests when no files or directories are given in
usefixtures (args): list of default fixtures to be used with this project
python_files (args): glob-style file patterns for Python test module discovery
python_classes (args): prefixes or glob names for Python test class discovery
python_functions (args): prefixes or glob names for Python test function and method discovery
7.1.2 插件可以添加ini文件選項
除前面列出的選項,利用插件和conftest.py文件還可以添加新的選項。新增的選項也可以使用pytest --help查看。
7.2 更改默認命令行選項
如果某一些選項經常,又不想每次重複輸入,這時則可以修改pytest.ini文件的addopts設置,如下所示:
[pytest]
addopts = -rsxX -L --tb=short --strict
7.3 註冊標記來防止拼寫錯誤
自定義標記可以簡化測試工作,讓我們可以使用指定的標記運行某個子集,但標記很容易拼錯。默認情況下,並不會引起程序錯誤,pytest會以爲是創建的另外一個新的標記。爲避免這種情況,可以在pytest.ini中註冊標記。如下所示:
[pytest]
markers =
smoke: Run the smoke test function
get: Run the get test function
標記做好之後,可以通過pytest --markers來查看,如下所示:
>>> pytest --markers
@pytest.mark.smoke: Run the smoke test function
@pytest.mark.get: Run the get test function
沒有註冊的標記不會出現在--markers列表中。如果使用--strict選項,遇到拼寫錯誤的標記或未標記的標記就會報錯。
7.4 指定pytest的最低版本號
minversion選項可以指定運行測試用例的pytest的最低版本。示例如下所示:
[pytest]
minversion = 5.2
7.5 指定pytest忽略某些目錄
pytest在默認情況,會搜索指定目錄及其子目錄。如果想跳過某些目錄,可以使用norecursedirs選項。
norecursedirs的默認設置是.* build dist CVS _darcs {arch}和*.egg
示例如下所示:
[pytest]
norecursedirs = .* build dist CVS _darcs \{arch\} *.egg
7.6 指定測試目錄
norecursedirs指定哪些目錄不用訪問,而 testpaths 則指定了pytest去哪裏搜索運行測試, testpaths是一系列相對於根目錄的路徑,用於限定測試用例的搜索範圍,只有在pytest未指定文件目錄參數或測試用例標識符時,該選項纔會啓用。如下所示:
[pytest]
testpaths = test
7.7 更改測試搜索的規則
pytest根據一定的規則搜索並運行測試,一個標準的測試搜索規則如下所示:
- 1.從一個或多個目錄開始查找,如果未指定,則默認爲當前目錄
- 2.在該目錄及其子目錄中查找測試模塊,測試模塊是以test_.py或_test.py的文件
- 3.在測試模塊中搜索以test_開頭的函數名或Test開頭的類,且過濾掉包含__init__()的類
1.python_classes
通常,pytest的測試搜索規則是尋找以Test*開頭的測試類,而且這個類不能包含__init__()方法。但如果沒有按照這個格式對類進行命名(如 < something > Test),該怎麼辦?針對這種情況,可以使用python_classes來解決這個問題。如下所示:
[pytest]
python_classes = *Test Test* *Suite
2.python_files
與python_classes類似,python_files是更改搜索測試模塊的規則,如下所示:
[pytest]
python_files = test_* *_test check_*
3.python_functions
與前面兩個類似,python_functions是更改搜索測試函數規則,如下所示:
[pytest]
python_functions = test_* *_test check_*
7.8 禁用XPASS
設置xfail_strict = True 可以將那些被標記@pytest.mark.xfail但實際運行通過的測試用例也被報告爲測試失敗。設置如下所示:
[pytest]
xfail_strict = True
7.9 避免文件衝突
在講解Python的模塊和包時,爲了避免命名衝突,可以使用模塊和包。在測試過程中,同樣也可以同樣的解決思路。
原文地址:https://www.cnblogs.com/surpassme/p/13262604.html
本文同步在微信訂閱號上發佈,如各位小夥伴們喜歡我的文章,也可以關注我的微信訂閱號:woaitest,或掃描下面的二維碼添加關注: