背景:
pytest以特定規則搜索測試用例,所以測試用例文件、測試類以及類中的方法、測試函數這些命名都必須符合規則,才能被pytest搜索到并加入測試運行隊列中。
默認搜索規則:
- 如果pytest命令行有指定目錄,則從該目錄中開始查找測試用例文件,如果沒有指定,則從當前運行目錄開始查找文件。注意,該查找是遞歸查找,子目錄中的文件也會被查找到。
- 并不是能夠查找到目錄下的所有文件,只有符合命名規則的文件才會被查找。默認規則是以test_開頭或者以_test結尾的.py文件。
- 在測試文件中查找Test開頭的類,以及類中以test_開頭的方法,查找測試文件中test_開頭的函數。
測試用例默認命名規則
- 除非pytest命令指定到測試用例文件,否則測試用例文件命名應該以 test_開頭或者以_test結尾。
- 測試函數命名,測試類的方法命名應該以test_開頭。
- 測試類命名應當以Test開頭。
tips: 測試類的不應該有構造函數。
筆者習慣裝測試用例的文件夾,測試用例文件,測試函數,類中的測試方法都以test_開頭。建議保持一種統一的風格。
示例:
# func.py
def add(a,b):
return a+b
# ./test_case/test_func.py
import pytest
from func import *
class TestFunc:
#def __init__(self):
#self.a = 1
def test_add_by_class(self):
assert add(2,3) == 5
def test_add_by_func():
assert add(4,6) == 10
'''
# stdout:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1
rootdir: D:\Python3.7\project\pytest
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collected 2 items
test_case\test_func.py .. [100%]
============================== 2 passed in 0.04s ==============================
[Finished in 1.3s]
######################################################################
'''
測試結果中,test_case\test_func.py … 。兩個點號代表兩個測試用例。
錯誤示范,當測試類有構造函數時:
# func.py
def add(a,b):
return a+b
# ./test_case/test_func.py
import pytest
from func import *
class TestFunc:
def __init__(self):
self.a = 1
def test_add_by_class(self):
assert add(2,3) == 5
def test_add_by_func():
assert add(4,6) == 10
'''
# stdout:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1
rootdir: D:\Python3.7\project\pytest
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collected 1 item
test_case\test_func.py . [100%]
============================== warnings summary ===============================
test_case\test_func.py:4
D:\Python3.7\project\pytest\test_case\test_func.py:4: PytestCollectionWarning: cannot collect test class 'TestFunc' because it has a __init__ constructor (from: test_case/test_func.py)
class TestFunc:
-- Docs: https://docs.pytest.org/en/latest/warnings.html
======================== 1 passed, 1 warning in 0.04s =========================
[Finished in 1.4s]
######################################################################
'''
會報錯,pytest只能找到test_開頭的函數,但是不能找到Test開頭的含有構造函數的測試類。
自定義測試用例命名規則
如果因為某種需要,需要使用其他命名規則命名的測試文件、測試函數、測試類以及測試類的方法,可以通過pytest.ini配置文件做到。
在測試系統的頂層目錄創建pytest.ini文件,在pytest.ini文件中寫入如下配置:
[pytest]
# 更改測試文件命名規則
python_files = HG*
# 更改測試類命名規則
python_classes = HG*
# 更嗨測試函數命名規則
python_functions = HG*
示例:
# func.py
def add(a,b):
return a+b
# ./test_case/HG_func.py
import pytest
from func import *
class HGFunc:
#def __init__(self):
#self.a = 1
def HG_add_by_class(self):
assert add(2,3) == 5
def HG_add_by_func():
assert add(4,6) == 10
'''
stdout:
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- D:\Python3.7\python.exe
cachedir: .pytest_cache
rootdir: D:\Python3.7\project\pytest, inifile: pytest.ini
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collecting ... collected 2 items
test_case/HG_func.py::HGFunc::HG_add_by_class PASSED [ 50%]
test_case/HG_func.py::HG_add_by_func PASSED [100%]
============================== 2 passed in 0.03s ==============================
[Finished in 1.3s]
'''
Tips:
- pytest.ini是可以改變pytest運行方式的配置文件,但是正常情況下,測試系統里根本不需要存在pytest.ini文件,我們使用默認的運行方式即可工作。
- pytest.ini還有許多其他個性化配置,當有需要時,可以在自動化測試項目的頂層目錄里創建pytest.ini文件,添加配置,達到個性化運行的目的。
到此這篇關于Pytest之測試命名規則的使用的文章就介紹到這了,更多相關Pytest 命名規則內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 詳解用Pytest+Allure生成漂亮的HTML圖形化測試報告
- python pytest進階之conftest.py詳解
- python pytest進階之fixture詳解
- Pytest測試框架基本使用方法詳解
- Pytest mark使用實例及原理解析
- 簡單了解pytest測試框架setup和tearDown
- python的pytest框架之命令行參數詳解(下)
- python單元測試框架pytest的使用示例