pytest
# 安裝:
pip install pytest
# 編寫規(guī)范
1. 測(cè)試文件以test_開頭(以_test結(jié)尾也可以)
1. 測(cè)試類以Test開頭,并且不能帶有 __init__ 方法
1. 測(cè)試函數(shù)以test_開頭
1. 斷言使用基本的assert即可
# 執(zhí)行方式:
py.test # 執(zhí)行單簽所有測(cè)試用例
py.test test_1.py # 執(zhí)行test_1文件下的用例
py.test dir1 # 執(zhí)行dir下的所有用例
py.test test_mod.py::test_func # 執(zhí)行某個(gè)方法
allure測(cè)試報(bào)告-圖形化工具
官網(wǎng):https://docs.qameta.io/allure/
安裝:
# Mac系統(tǒng),使用HomeBrew安裝:
brew install allure
pip install pytest
pip install allure-pytest
@allure.feature # 用于定義被測(cè)試的功能,被測(cè)產(chǎn)品的需求點(diǎn)
@allure.story # 用于定義被測(cè)功能的用戶場(chǎng)景,即子功能點(diǎn)
with allure.step # 用于將一個(gè)測(cè)試用例,分成幾個(gè)步驟在報(bào)告中輸出
allure.attach # 用于向測(cè)試報(bào)告中輸入一些附加的信息,通常是一些測(cè)試數(shù)據(jù)信息
@pytest.allure.step # 用于將一些通用的函數(shù)作為測(cè)試步驟輸出到報(bào)告,調(diào)用此函數(shù)的地方會(huì)向報(bào)告中輸出步驟
---
Severity定制詳解
Allure中對(duì)嚴(yán)重級(jí)別的定義:
1) Blocker級(jí)別:中斷缺陷(客戶端程序無(wú)響應(yīng),無(wú)法執(zhí)行下一步操作)
2)Critical級(jí)別:臨界缺陷( 功能點(diǎn)缺失)
3) Normal級(jí)別:普通缺陷(數(shù)值計(jì)算錯(cuò)誤)
4) Minor級(jí)別:次要缺陷(界面錯(cuò)誤與UI需求不符)
5)Trivial級(jí)別:輕微缺陷(必輸項(xiàng),無(wú)提示,或者提示不規(guī)范)
執(zhí)行用例
# 執(zhí)行全部的測(cè)試用例
pytest ./Allure/test_allure_shoping.py --alluredir ./Allure/result/
# 執(zhí)行指定features或者stories執(zhí)行一部分測(cè)試用例,比如執(zhí)行‘報(bào)告購(gòu)物車’下的‘加入購(gòu)物車’子功能的測(cè)試用例
pytest ./Allure/test_allure_shopping.py --allure_features="報(bào)告購(gòu)物車" --allure_stories="加入購(gòu)物車" ../Allure/part_result/
瀏覽器打開報(bào)告
allure serve /home/path/to/project/target/surefire-reports/
生成報(bào)告
# 生成測(cè)試報(bào)告
allure generate ./Alluer/result/ -o ./Allure/report/ --clean
# 打開測(cè)試報(bào)告
allure open -h 127.0.0.1 -p 8083 ./Allure/report/
示例
import allure
import pytest
@allure.feature('產(chǎn)品需求點(diǎn)1') # 需求點(diǎn)
@allure.story('測(cè)試需求點(diǎn)1') # 測(cè)試需求點(diǎn)
@allure.severity('blocker') # 嚴(yán)重程度
def test_case_01():
assert 0
@allure.feature('產(chǎn)品需求點(diǎn)1')
@allure.story('測(cè)試需求點(diǎn)2')
@allure.severity('critical')
def test_case_02():
assert 0 == 0
@allure.feature('產(chǎn)品需求點(diǎn)1')
@allure.story('測(cè)試需求點(diǎn)3')
@allure.severity('normal')
def test_case_03():
assert 0
@allure.feature('產(chǎn)品需求點(diǎn)1')
@allure.story('測(cè)試需求點(diǎn)3')
@allure.severity('minor')
def test_case_04():
assert 0 == 0
>>>命令
pytest testCode/test_a.py --alluredir ./testCode/report
allure generate ./report -o ./report2 --clean
allure open -h 127.0.0.1 -p 8083 ./report2
參考文檔:https://juejin.im/post/5def3d64f265da33e67b3352

張良_幽蘭居士.jpg