前言
一般流程性的測試用例,寫成自動化用例時,步驟較多寫起來會比較長。在測試用例里面添加詳細的步驟有助於更好的閱讀,也方便報錯后快速的定位到問題。
舉個常見的測試場景用例:從登陸開始,到瀏覽商品添加購物車,最后下單支付。
用例步驟:1.登陸,2.瀏覽商品,3.添加購物車,4.生成訂單,5.支付成功
用例設計
先把上面的每個環節,寫成函數放到c_function.py
#c_function.py
import pytest
def login(uname,pwd):
'''登錄'''
print("前置操作:登錄")
def browse_goods():
'''瀏覽商品'''
print("瀏覽商品")
def add_goods(goods_id="110"):
'''添加購物車'''
print("添加購物車")
def buy_goods():
print("生成訂單")
def pay_goods():
print("付款")
接下來測試用例設計,登陸可以單獨拿出來,當成前置操作,后面的步驟合起來就是一個用例test_a.py
#test_a.py
import allure
import pytest
from .c_function import *
@pytest.fixture(scope="session")
def login_setup():
login("admin","123456")
@allure.feature("功能模塊")
@allure.story("測試小故事")
@allure.title("測試用例名稱")
def test_add_buy_goods(login_setup):
'''用例描述:
前置:登錄
用例步驟:瀏覽,添加,生成訂單,支付
'''
with allure.step("step1:瀏覽商品"):
browse_goods()
with allure.step("step2:添加購物車"):
add_goods()
with allure.step("step three:生成訂單"):
buy_goods()
with allure.step("step four:支付"):
pay_goods()
with allure.step("斷言"):
assert 1==1
執行用例
pytest --alluredir ./report/x
生成allure報告
allure serve ./report/x
報告展示效果如下

測試步驟@allure.step()
測試步驟也可以在c_function.py里面定義的函數上加上裝飾器實現:@allure.step()
#c_function.py
import pytest
import allure
@allure.step("setup:登錄")
def login(uname,pwd):
'''登錄'''
print("前置操作:登錄")
@allure.step("step:瀏覽商品")
def browse_goods():
'''瀏覽商品'''
print("瀏覽商品")
@allure.step("step:添加購物車")
def add_goods(goods_id="110"):
'''添加購物車'''
print("添加購物車")
@allure.step("生成訂單")
def buy_goods():
print("生成訂單")
@allure.step("支付")
def pay_goods():
print("付款")
測試用例設計test_a.py
#test_a.py
import allure
import pytest
from .c_function import *
@pytest.fixture(scope="session")
def login_setup():
login("admin","123456")
@allure.feature("功能模塊")
@allure.story("測試小故事")
@allure.title("測試用例名稱")
def test_add_buy_goods(login_setup):
'''用例描述:
前置:登錄
用例步驟:瀏覽,添加,生成訂單,支付
'''
#with allure.step("step1:瀏覽商品"):
browse_goods()
#with allure.step("step2:添加購物車"):
add_goods()
#with allure.step("step three:生成訂單"):
buy_goods()
#with allure.step("step four:支付"):
pay_goods()
#with allure.step("斷言"):
assert 1==1
執行用例
pytest --alluredir ./report/x
生成allure報告
allure serve ./report/x
報告展示效果如下

兩種方式對比
使用with allure.step("step:步驟")這種方式代碼可讀性更好一點,但報告中不會帶上函數里面的傳參和對應的值。
使用@ allure.step("step:步驟")這種方式會帶上函數的傳參和對應的值。
這兩種方式結合起來使用,才能更好的展示測試報告!
