python中pytest收集用例規(guī)則與運(yùn)行指定用例詳解
前言
上篇文章相信大家已經(jīng)了解了pytest在cmd下結(jié)合各種命令行參數(shù)如何運(yùn)行測(cè)試用例,并輸出我們想要看到的信息。那么今天會(huì)講解一下pytest是如何收集我們寫好的用例?我們又有哪些方式來運(yùn)行單個(gè)用例或者批量運(yùn)行用例呢?下面將為大家一一解答!
pytest收集用例原理分析
首先我們按照如下目錄結(jié)構(gòu)新建我們的項(xiàng)目
[pyttest搜索測(cè)試用例的規(guī)則] |[測(cè)試用例目錄1] | |__init__.py | |test_測(cè)試模塊1.py | |test_測(cè)試模塊2.py |[測(cè)試用例目錄2] | |__init__.py | |test_測(cè)試用例1.py | |測(cè)試用例.py |test_測(cè)試模塊.py |測(cè)試用例2.py
代碼實(shí)例
# test_測(cè)試模塊1.py def test_testFunc1(): print('\n我是一個(gè)測(cè)試用例! in test_testFunc1') assert 1 == 1 def func1(): print('我不是一個(gè)測(cè)試用例') assert 1 == 1 # test_測(cè)試模塊2.py class TestClass1(object): def test_class_func1(self): print('\n 我是一個(gè)類里面的測(cè)試用例 in test_class_func1') assert 1 == 1 def class_func1(self): print('我是類里面的一個(gè)普通函數(shù)!') # test_測(cè)試用例1.py class TestClass2(object): def test_class_func2(self): print('\n 我是一個(gè)類里面的測(cè)試用例 in test_class_func2',) assert 1 == 1 def class_func2(self): print('我是類里面的一個(gè)普通函數(shù)!') def test_testFunc2(): print('\n我是一個(gè)測(cè)試用例 in test_testFunc2!') assert 1 == 1 def func2(): print('我不是一個(gè)測(cè)試用例') assert 1 == 1 # 測(cè)試用例.py def test_testFunc3(): print('\n我是一個(gè)測(cè)試用例! in 測(cè)試用例.py') assert 1 == 1 def func3(): print('我不是一個(gè)測(cè)試用例') assert 1 == 1 # test_測(cè)試模塊3.py def test_testFunc4(): print('\n我是一個(gè)測(cè)試用例! in test_testFunc4') assert 1 == 1 def func4(): print('我不是一個(gè)測(cè)試用例') assert 1 == 1 class TestClass3(object): def test_class_func3(self): print('\n 我是一個(gè)類里面的測(cè)試用例 in test_class_func3') assert 1 == 1 def class_func3(self): print('我是類里面的一個(gè)普通函數(shù)!') # 測(cè)試用例2.py def test_testFunc5(): print('\n我是一個(gè)測(cè)試用例! in test_testFunc5') assert 1 == 1 def func5(): print('我不是一個(gè)測(cè)試用例') assert 1 == 1
下面我們使用cmd命令來執(zhí)行一下這個(gè)項(xiàng)目,看一下究竟會(huì)有多少條用例是有效的用例?打開cmd 切換到項(xiàng)目的根目錄執(zhí)行命令 pytest -v
D:\pytest搜索測(cè)試用例規(guī)則>pytest -v ============================= test session starts ============================= platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe cachedir: .pytest_cache metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'} rootdir: D:\pytest搜索測(cè)試用例規(guī)則, inifile: plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10 collected 6 items test_測(cè)試模塊3.py::test_testFunc4 PASSED [ 16%] test_測(cè)試模塊3.py::TestClass3::test_class_func3 PASSED [ 33%] 測(cè)試用例目錄1/test_測(cè)試模塊1.py::test_testFunc1 PASSED [ 50%] 測(cè)試用例目錄1/test_測(cè)試模塊2.py::TestClass1::test_class_func1 PASSED [ 66%] 測(cè)試用例目錄2/test_測(cè)試用例1.py::TestClass2::test_class_func2 PASSED [ 83%] 測(cè)試用例目錄2/test_測(cè)試用例1.py::test_testFunc2 PASSED [100%] ========================== 6 passed in 0.59 seconds ===========================
運(yùn)行結(jié)果可以看到一共有6條用例passed,且詳細(xì)的列出了是哪6條,那么按照我們上面編寫的用例其實(shí)并不止6條,那么為什么會(huì)只運(yùn)行了6條呢?綜合以上的代碼結(jié)構(gòu)和我們的執(zhí)行結(jié)果對(duì)比,我們應(yīng)該能發(fā)現(xiàn)這樣的規(guī)律
pytets會(huì)從我們當(dāng)前運(yùn)行的目錄開始查找所有目錄,查找以test_開頭的文件且文件中所有以test_開頭的函數(shù)和以Test開頭的類和類里面以test_開頭的函數(shù)為測(cè)試用例。這就是為什么上面之運(yùn)行了6條測(cè)試用例!
pytest運(yùn)行指定測(cè)試用例
我們?nèi)匀皇褂蒙厦娴捻?xiàng)目作為演示(cdm切換到項(xiàng)目的根目錄)
1.運(yùn)行指定目錄下的所有用例
我們指定運(yùn)行測(cè)試用例目錄1里面的所有用例(pytest -v 測(cè)試用例目錄1)
D:\pytest搜索測(cè)試用例規(guī)則>pytest -v 測(cè)試用例目錄1 ============================= test session starts ============================= platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe cachedir: .pytest_cache metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'} rootdir: D:\pytest搜索測(cè)試用例規(guī)則, inifile: plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10 collected 2 items 測(cè)試用例目錄1/test_測(cè)試模塊1.py::test_testFunc1 PASSED [ 50%] 測(cè)試用例目錄1/test_測(cè)試模塊2.py::TestClass1::test_class_func1 PASSED [100%] ========================== 2 passed in 0.05 seconds =========================== # 這樣就會(huì)只搜索和指定指定目錄下面所有的用
2.運(yùn)行指定文件中的所有用例
我們指定運(yùn)行test_測(cè)試模塊1.py(pytest -v 測(cè)試用例目錄1/test_測(cè)試模塊1.py )
D:\pytest搜索測(cè)試用例規(guī)則>pytest -v 測(cè)試用例目錄1/test_測(cè)試模塊1.py ============================= test session starts ============================= platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe cachedir: .pytest_cache metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'} rootdir: D:\pytest搜索測(cè)試用例規(guī)則, inifile: plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10 collected 1 item 測(cè)試用例目錄1/test_測(cè)試模塊1.py::test_testFunc1 PASSED [100%] ========================== 1 passed in 0.09 seconds =========================== # 運(yùn)行指定文件下的所有用例
3.運(yùn)行指定文件中的測(cè)試類
我們指定運(yùn)行test_測(cè)試模塊2.py中的測(cè)試類Testclass1(pytest -v 測(cè)試用例目錄1/test_測(cè)試模塊2.py::TestClass1)
D:\pytest搜索測(cè)試用例規(guī)則>pytest -v 測(cè)試用例目錄1/test_測(cè)試模塊2.py::TestClass1 ============================= test session starts ============================= platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe cachedir: .pytest_cache metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'} rootdir: D:\pytest搜索測(cè)試用例規(guī)則, inifile: plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10 collected 1 item 測(cè)試用例目錄1/test_測(cè)試模塊2.py::TestClass1::test_class_func1 PASSED [100%] ========================== 1 passed in 0.05 seconds =========================== # 運(yùn)行指定的測(cè)試類中的所有測(cè)試用
4.運(yùn)行指定的測(cè)試用例函數(shù)
我們指定運(yùn)行test_testFunc1(pytest -v 測(cè)試用例目錄1/test_測(cè)試模塊1.py::test_testFunc1)
D:\pytest搜索測(cè)試用例規(guī)則>pytest -v 測(cè)試用例目錄1/test_測(cè)試模塊1.py::test_testFunc1 ============================= test session starts ============================= platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe cachedir: .pytest_cache metadata: {'Python': '3.6.4', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '3.8.0', 'py': '1.6.0', 'pluggy': '0.7.1'}, 'Plugins': {'metadata': '1.8.0', 'html': '1.20.0', 'allure-adaptor': '1.7.10'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_181'} rootdir: D:\pytest搜索測(cè)試用例規(guī)則, inifile: plugins: metadata-1.8.0, html-1.20.0, allure-adaptor-1.7.10 collected 1 item 測(cè)試用例目錄1/test_測(cè)試模塊1.py::test_testFunc1 PASSED [100%] ========================== 1 passed in 0.03 seconds ===========================
總結(jié)
收集用例規(guī)則:搜索所有以test_開頭的測(cè)試文件,以Test開頭的測(cè)試類,以test_開頭的測(cè)試函數(shù)
執(zhí)行用例規(guī)則:從-v 參數(shù)輸出的執(zhí)行信息我們就應(yīng)該能發(fā)現(xiàn),運(yùn)行指定的目錄下用例 使用命令 pytest 目錄/目錄 即可;運(yùn)行指定文件使用 pytest 目錄/文件 即可;運(yùn)行指定類或者函數(shù) 使用命令 pytest 目錄/文件::類名::函數(shù)名 或者 pytest 目錄/文件::函數(shù)名
搜索用例規(guī)則也是我們命名用例文件,測(cè)試類,測(cè)試函數(shù)的規(guī)則;執(zhí)行指定測(cè)試用例記住規(guī)則即可
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
pytorch?液態(tài)算法實(shí)現(xiàn)瘦臉效果
在PS中,我們可以利用液化工具對(duì)人像進(jìn)行形變處理,例如瘦臉、瘦腿、放大眼睛等一系列的常規(guī)操作。今天我們來了解一下這些操作的算法原理,并用pytorch來實(shí)現(xiàn)瘦臉效果2021-11-11Django項(xiàng)目使用CircleCI的方法示例
這篇文章主要介紹了Django項(xiàng)目使用CircleCI的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Python使用sklearn庫實(shí)現(xiàn)的各種分類算法簡(jiǎn)單應(yīng)用小結(jié)
這篇文章主要介紹了Python使用sklearn庫實(shí)現(xiàn)的各種分類算法,結(jié)合實(shí)例形式分析了Python使用sklearn庫實(shí)現(xiàn)的KNN、SVM、LR、決策樹、隨機(jī)森林等算法實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-07-07python批量獲取html內(nèi)body內(nèi)容的實(shí)例
今天小編就為大家分享一篇python批量獲取html內(nèi)body內(nèi)容的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01