日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

pythontdd测试命名_荐Pytest之测试命名规则

發布時間:2024/1/23 python 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pythontdd测试命名_荐Pytest之测试命名规则 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

背景:

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文件,添加配置,達到個性化運行的目的。

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的pythontdd测试命名_荐Pytest之测试命名规则的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。