pytest測(cè)試框架+allure超詳細(xì)教程
1、測(cè)試識(shí)別和運(yùn)行
文件識(shí)別:
- 在給定的目錄中,搜索所有test_.py或者_(dá)test.py文件
用例識(shí)別:
- Test*類包含的所有test_*的方法(測(cè)試類不能有__init__方法)
- 不在類中的所有test_*方法
- pytest也能執(zhí)行unit test寫的用例和方法
運(yùn)行方式
1、pycharm頁(yè)面修改默認(rèn)的測(cè)試運(yùn)行方式
settings頁(yè)面,輸入pytest,修改Default test runner
2、右鍵執(zhí)行python文件
3、命令行界面執(zhí)行,點(diǎn)擊pycharm下方的terminal,打開命令行界面,執(zhí)行pytest 命令
4、pycharm代碼邊界界面,左側(cè)單條用例運(yùn)行按鈕
5、主函數(shù)運(yùn)行方式
在運(yùn)行文件中編寫主函數(shù),主函數(shù)啟動(dòng)需要導(dǎo)入pytest包,不然找不到pytest方法
import pytest class TestClass: def test_one(self): x = "this" assert "h" in x def test_two(self): x = "hello" assert hasattr(x, "check") if __name__ == '__main__': # pytest.main() pytest.main("test_study.py")
pytest.main()會(huì)自動(dòng)讀取當(dāng)前目錄下的所有test開頭的.py文件,運(yùn)行test方法或者類
可以傳入不同的參數(shù),讓運(yùn)行更加定制化
pytest.main(['./']) # 運(yùn)行./目錄下所有(test_*.py 和 *_test.py) pytest.main (['./subpath1']) # 運(yùn)行./subpath1 目錄下用例 pytest.main (['./subpath1/test_module1.py']) # 運(yùn)行指定模塊 pytest.main (['./subpath1/test_module1.py::test_m1_1']) # 運(yùn)行模塊中的指定用例 pytest.main (['./subpath2/test_module2.py::TestM2::test_m2_02']) # 運(yùn)行類中的指定用例 pytest.main (['-k','pp']) # 匹配包含pp的用例(匹配目錄名、模塊名、類名、用例名) pytest.main(['-k','spec','./subpath1/test_module1.py']) # 匹配test_module1.py模塊下包含spec的用例 pytest.main(['-k','pp','./subpath2/test_module2.py::TestM2']) # 匹配TestM2類中包含pp的用例
2、參數(shù)化
@pytest.mark.parametrize(argnames,argvalues)
- argnames:要參數(shù)化的變量,可以是string(用逗號(hào)分割),list,tuple
- argvalues:參數(shù)化的值,list[tuple],以列表形式傳入元組,每個(gè)元組都是一條測(cè)試數(shù)據(jù)
- ids,:默認(rèn)為none,用來(lái)重新定義測(cè)試用例的名稱
# 使用string分割參數(shù)化的變量 @pytest.mark.parametrize('a,b',[(10,20),(30,40)]) def test_param(a, b): print(a,b) # 使用list分割參數(shù)化的變量 @pytest.mark.parametrize(['a', 'b'],[(10,20),(30,40)]) def test_param(a, b): print(a,b) # 使用tuple分割參數(shù)化的變量 @pytest.mark.parametrize(('a', 'b'),[(10,20),(30,40)]) def test_param(a, b): print(a,b) # 使用ids重新定義用例名稱 @pytest.mark.parametrize('a,b',[(10,20),(30,40)], ids=['case1', 'case2']) def test_param(a, b): print(a,b)
yaml參數(shù)化
pip install PyYAML
yaml實(shí)現(xiàn)list
- 10 - 20 - 30
yaml實(shí)現(xiàn)字典
by: id locator: name action: click
yaml二維數(shù)組
companies: - id: 1 name: company1 price: 200w - id: 2 name: company2 price: 500w fruites: - name: 蘋果 price: 8.6 - name: 香蕉 price: 2.6
讀取yaml文件
yaml.safe_load(open(‘./data.yaml’))
3、測(cè)試報(bào)告美化-allure
1、操作系統(tǒng)內(nèi)部先安裝allure
2、安裝allure-pytest插件
pip install allure-pytest
3、運(yùn)行測(cè)試用例
查看pytest中allure相關(guān)的命令行參數(shù)
C:\Users\Administrator>pytest --help | findstr allure <== linux 使用grep過(guò)濾 --allure-severities=SEVERITIES_SET <== 根據(jù)用例級(jí)別過(guò)濾需要執(zhí)行的用例 --allure-epics=EPICS_SET --allure-features=FEATURES_SET <== 根據(jù)用例設(shè)置的feature名稱過(guò)濾需要執(zhí)行的用例 --allure-stories=STORIES_SET <== 根據(jù)用例設(shè)置的story名稱過(guò)濾需要執(zhí)行的用例 --allure-ids=IDS_SET Comma-separated list of IDs. --allure-link-pattern=LINK_TYPE:LINK_PATTERN --alluredir=DIR <== 指定存放用例執(zhí)行結(jié)果的目錄 --clean-alluredir 清除alluredir文件夾(如果存在) --allure-no-capture Do not attach pytest captured logging/stdout/stderr to
4、執(zhí)行測(cè)試命令
# --alluredir: 用于指定存儲(chǔ)測(cè)試結(jié)果的路徑 pytest [測(cè)試文件] -vs --alluredir=./result/ --clean-alluredir
5、查看測(cè)試報(bào)告
在線查看報(bào)告,直接打開默認(rèn)瀏覽器展示當(dāng)前報(bào)告
# 注意這里的serve書寫,后面接用例執(zhí)行結(jié)果(./result/:就是存放執(zhí)行結(jié)果的目錄路徑) allure serve ./result/
從結(jié)果生成報(bào)告
1.生成報(bào)告
# 注意:覆蓋路徑加--clean allure generate ./result/ -o ./report/ --clean
2.打開報(bào)告
allure open -h 127.0.0.1 -p 8883 ./report/
allure常用特性
支持在報(bào)告中查看測(cè)試功能、子功能或場(chǎng)景、測(cè)試步驟和測(cè)試附加信息等,可以通過(guò)@feature、@story、@step和@attach等裝飾器實(shí)現(xiàn)
實(shí)現(xiàn)的步驟
- import allure
- 功能上加@allure.feature(“功能名稱”)
- 子功能上加@allure.story(“子功能名稱”)
- 用例標(biāo)題@allure.title(“用例名稱”)
- 用例描述@allure.description(“用例描述”)
- 步驟上加@allure.step(“步驟細(xì)節(jié)”)
- @allure.attach(“具體文本信息”),需要附加的信息,可以是數(shù)據(jù)、文本、圖片、視頻和網(wǎng)頁(yè)
- 用例級(jí)別@allure.severity(級(jí)別)
- 如果只測(cè)試登錄功能運(yùn)行的時(shí)候,可以加限制過(guò)濾
# 注意這里--allure-features中間是-中線, 需要使用雙引號(hào) # 不能過(guò)濾--allure-features下的--allure-stories pytest 文件名 --allure-features="登錄模塊"
- feature相當(dāng)于一個(gè)功能,一個(gè)大模塊,將case分類到某個(gè)feature中,報(bào)告中Behaviors(功能中展示),相當(dāng)于testsuite
- story相當(dāng)于這個(gè)功能或者模塊下的不能場(chǎng)景,分支功能,屬于feature之下的結(jié)構(gòu),報(bào)告中features中展示,詳單與tescase
- feature與story類似于父子關(guān)系
- 2、allure特性-step
- 測(cè)試過(guò)程中每個(gè)步驟,一般放在具體邏輯方法中
- 可以放在關(guān)步驟中,在報(bào)告中顯示
- 在app、web自動(dòng)化測(cè)試中,建議每切換到一個(gè)新頁(yè)面就做一個(gè)step
- 用法:
- @allure.step():只能以裝飾器的形式放在類或者方法上面
- with allure.step():可以放在測(cè)試用例方法里面,但是測(cè)試步驟代碼需要被該語(yǔ)句包含
3、allure特性-testcase
關(guān)聯(lián)測(cè)試用例,可以直接給測(cè)試用例的地址鏈接,一般用于關(guān)聯(lián)手工測(cè)試用例
實(shí)力代碼:
import pytest import allure @allure.feature('用戶登錄') class TestLogin: @allure.story('登錄成功') def test_login_success(self): with allure.step('步驟1:打開應(yīng)用'): print('打開應(yīng)用') with allure.step('步驟2:進(jìn)入登錄頁(yè)面'): print('進(jìn)入登錄頁(yè)面') with allure.step('步驟3:輸入用戶名和密碼'): print('輸入用戶名和密碼') print('這是登錄成功測(cè)試用例') @allure.story('登錄失敗') def test_login_fail(self): print('這是登錄失敗測(cè)試用例') @allure.story('登錄失敗') @allure.title('用戶名缺失') def test_login_fail_a(self): print('這是登錄失敗測(cè)試用例') @allure.story('登錄失敗') @allure.testcase('https://www.baidu.com/', '關(guān)聯(lián)測(cè)試用例地址') @allure.title('密碼缺失') @allure.description('這是一個(gè)用例描述信息') def test_login_fail_b(self): with allure.step('點(diǎn)擊用戶名'): print('輸入用戶名') with allure.step('點(diǎn)擊密碼'): print('輸入密碼') print('點(diǎn)擊登錄') with allure.step('點(diǎn)擊登錄之后登錄失敗'): assert '1' == 1
按重要性級(jí)別 進(jìn)行一定范圍測(cè)試
通常測(cè)試用PO、冒煙測(cè)試、驗(yàn)證上線測(cè)試。按照重要性級(jí)別來(lái)分別執(zhí)行
缺陷嚴(yán)重級(jí)別
1. Blocker級(jí)別——中斷缺陷 客戶端程序無(wú)響應(yīng),無(wú)法執(zhí)行下一步操作。 2. Critical級(jí)別――臨界缺陷,包括: 功能點(diǎn)缺失,客戶端爆頁(yè)。 3. Major級(jí)別——較嚴(yán)重缺陷,包括: 功能點(diǎn)沒(méi)有滿足需求。 4. Normal級(jí)別――普通缺陷,包括: 1. 數(shù)值計(jì)算錯(cuò)誤 2. JavaScript錯(cuò)誤。 5. Minor級(jí)別———次要缺陷,包括: 1. 界面錯(cuò)誤與UI需求不符。 2. 打印內(nèi)容、格式錯(cuò)誤 3. 程序不健壯,操作未給出明確提示。 6. Trivial級(jí)別——輕微缺陷,包括: 1. 輔助說(shuō)明描述不清楚 2. 顯示格式不規(guī)范,數(shù)字,日期等格式。 3. 長(zhǎng)時(shí)間操作未給用戶進(jìn)度提示 4. 提示窗口文字未采用行業(yè)術(shù)語(yǔ) 5. 可輸入?yún)^(qū)域和只讀區(qū)域沒(méi)有明顯的區(qū)分標(biāo)志 6. 必輸項(xiàng)無(wú)提示,或者提示不規(guī)范。 7. Enhancement級(jí)別——測(cè)試建議、其他(非缺陷) 1. 以客戶角度的易用性測(cè)試建議。 2. 通過(guò)測(cè)試挖掘出來(lái)的潛在需求。
解決方法:
- 通過(guò)附加pytest.mark標(biāo)記
- 通過(guò)allure.feature,allure.story
- 也可以通過(guò)allure.servity來(lái)附加標(biāo)記
步驟:
在方法,函數(shù)和類上面加:@allure.severity(allure.severity_level.TRIVIAL)
執(zhí)行時(shí)過(guò)濾:pytest -vs [文件名] --allure-severities normal, critical
前端自動(dòng)化測(cè)試-截圖
前端自動(dòng)化測(cè)試經(jīng)常需要附加圖片或html,在適當(dāng)?shù)牡胤?,適當(dāng)時(shí)機(jī)截圖
@allure.attach 實(shí)現(xiàn)不同類型附件,可以補(bǔ)充測(cè)試步驟或測(cè)試結(jié)果
使用方式:
- 在測(cè)試報(bào)告附加網(wǎng)頁(yè)
allure.attach(body(內(nèi)容), name, attachment_type, extension) allure.attach('<body>這是一段html</body>', 'html測(cè)試', attachment_type=allure.attachment_type.HTML)
在測(cè)試報(bào)告附加圖片
allure.attach.file(source, name, attachment_type, extension) allure.attach.file('./123.jpg', name='這是一個(gè)圖片', attachment_type=allure.attachment_type.JPG)
示例代碼:
import allure def test_attach_text(): allure.attach('這是一個(gè)純文本', attachment_type=allure.attachment_type.TEXT) def test_attach_html(): allure.attach('<body>這是一段html</body>', 'html測(cè)試', attachment_type=allure.attachment_type.HTML) def test_attach_phote(): allure.attach.file('./123.jpg', name='這是一個(gè)圖片', attachment_type=allure.attachment_type.JPG) def test_attach_video(): allure.attach.file('./123.mp4',name='這是一個(gè)視頻',attachment_type=allure.attachment_type.MP4)
import allure from selenium import webdriver import time import pytest @allure.testcase('https://www.baidu.com/', '百度搜索功能') @pytest.mark.parametrize('data', ['allure', 'pytest', 'unittest'], ids=['search allure', 'search pytest', 'search unittest'] ) def test_search(data): with allure.step('步驟1:打開瀏覽器輸入百度地址'): driver = webdriver.Chrome() driver.implicitly_wait(5) driver.get('https://www.baidu.com/') with allure.step(f'步驟2:在搜索框中輸入{data}, 并點(diǎn)擊百度一下'): driver.find_element_by_id('kw').send_keys(data) driver.find_element_by_id('su').click() time.sleep(2) with allure.step('步驟3: 截圖保存到項(xiàng)目中'): driver.save_screenshot(f'./result/{data}.jpg') allure.attach.file(f'./result/{data}.jpg', name=f'搜索{data}的截圖', attachment_type=allure.attachment_type.JPG) allure.attach(driver.page_source, f'搜索{data}的網(wǎng)頁(yè)內(nèi)容', allure.attachment_type.HTML) with allure.step('步驟4:關(guān)閉瀏覽器,退出'): driver.quit()
到此這篇關(guān)于pytest測(cè)試框架+allure超詳細(xì)教程的文章就介紹到這了,更多相關(guān)pytest allure測(cè)試框架內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Pytest測(cè)試報(bào)告工具Allure的高級(jí)用法
- Pytest測(cè)試報(bào)告工具Allure用法介紹
- Pytest+Request+Allure+Jenkins實(shí)現(xiàn)接口自動(dòng)化
- 詳解pytest+Allure搭建方法以及生成報(bào)告常用操作
- Pytest+request+Allure實(shí)現(xiàn)接口自動(dòng)化框架
- Pytest allure 命令行參數(shù)的使用
- 詳解用Pytest+Allure生成漂亮的HTML圖形化測(cè)試報(bào)告
- pytest allure添加環(huán)境信息實(shí)例講解
相關(guān)文章
Anaconda中Python虛擬環(huán)境的創(chuàng)建使用與刪除方法詳解
這篇文章主要為大家介紹了在Anaconda環(huán)境下,創(chuàng)建、使用與刪除Python虛擬環(huán)境的方法,具有一定的借鑒價(jià)值,需要的小伙伴可以跟隨小編一起了解一下2023-08-08django foreignkey(外鍵)的實(shí)現(xiàn)
這篇文章主要介紹了django foreignkey(外鍵)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07django 框架實(shí)現(xiàn)的用戶注冊(cè)、登錄、退出功能示例
這篇文章主要介紹了django 框架實(shí)現(xiàn)的用戶注冊(cè)、登錄、退出功能,結(jié)合實(shí)例形式詳細(xì)分析了Django框架用戶注冊(cè)、登陸、退出等功能具體實(shí)現(xiàn)方法及操作注意事項(xiàng),需要的朋友可以參考下2019-11-11