前言
一般流程性的測試用例,寫成自動化用例時,步驟較多寫起來會比較長。在測試用例里面添加詳細的步驟有助於更好的閱讀,也方便報錯后快速的定位到問題。
舉個常見的測試場景用例:從登陸開始,到瀏覽商品添加購物車,最后下單支付
用例步驟:1.登陸, 2.瀏覽商品 3.添加購物車 4.生成訂單 5.支付成功
用例設計
先把上面的每個環節,寫成函數放到common_fucntion.py
# common_fucntion.py
import allure
import pytest
'''
流程性的用例,添加測試步驟,讓用例更清晰
用例步驟:1.登陸, 2.瀏覽商品 3.添加購物車 4.生成訂單 5.支付成功
作者:上海-悠悠 QQ交流群:779429633
'''
def login(username, password):
'''登陸'''
print("前置操作:先登陸")
def open_goods():
'''瀏覽商品'''
print("瀏覽商品")
def add_shopping_cart(goods_id="10086"):
'''添加購物車'''
print("添加購物車")
def buy_goods():
'''生成訂單'''
print("buy")
def pay_goods():
'''支付'''
print("pay")
接下來測試用例設計,登陸可以單獨拿出來,當成前置操作,后面的步驟合起來就是一個用例test_allure_step.py
# test_allure_step.py
import allure
import pytest
from .common_function import *
'''
流程性的用例,添加測試步驟,讓用例更清晰
用例步驟:1.登陸, 2.瀏覽商品 3.添加購物車 4.生成訂單 5.支付成功
作者:上海-悠悠 QQ交流群:779429633
'''
@pytest.fixture(scope="session")
def login_setup():
login("yoyo", "123456")
@allure.feature("功能模塊")
@allure.story("測試用例小模塊-成功案例")
@allure.title("測試用例名稱:流程性的用例,添加測試步驟")
def test_add_goods_and_buy(login_setup):
'''
用例描述:
前置:登陸
用例步驟:1.瀏覽商品 2.添加購物車 3.購買 4.支付成功
'''
with allure.step("step1:瀏覽商品"):
open_goods()
with allure.step("step2:添加購物車"):
add_shopping_cart()
with allure.step("step3:生成訂單"):
buy_goods()
with allure.step("step4:支付"):
pay_goods()
with allure.step("斷言"):
assert 1 == 1
執行用例,生成allure報告
> pytest --alluredir ./allure_report test_allure_step.py
> allure serve ./allure_report
報告展示效果如下
測試步驟@allure.step()
測試步驟也可以在 common_fucntion.py 里面定義的函數上加上裝飾器實現:@allure.step()
import allure
import pytest
'''
流程性的用例,添加測試步驟,讓用例更清晰
用例步驟:1.登陸, 2.瀏覽商品 3.添加購物車 4.生成訂單 5.支付成功
'''
@allure.step("setup:登陸")
def login(username, password):
'''登陸'''
print("前置操作:先登陸")
@allure.step("step:瀏覽商品")
def open_goods():
'''瀏覽商品'''
print("瀏覽商品")
@allure.step("step:添加購物車")
def add_shopping_cart(goods_id="10086"):
'''添加購物車'''
print("添加購物車")
@allure.step("step:生成訂單")
def buy_goods():
'''生成訂單'''
print("buy")
@allure.step("step:支付")
def pay_goods():
'''支付'''
print("pay")
測試用例設計 test_allure_step_x.py
import allure
import pytest
from .common_function import *
# 作者:上海-悠悠 QQ交流群:779429633
@pytest.fixture(scope="session")
def login_setup():
login("yoyo", "123456")
@allure.feature("功能模塊")
@allure.story("測試用例小模塊-成功案例")
@allure.title("第二種實現方式:流程性的用例,添加測試步驟")
def test_add_goods_and_buy_2(login_setup):
'''
用例描述:
前置:登陸
用例步驟:1.瀏覽商品 2.添加購物車 3.購買 4.支付成功
'''
open_goods()
add_shopping_cart(goods_id="10086")
buy_goods()
pay_goods()
assert 1 == 1
執行用例,生成allure報告
> pytest --alluredir ./allure_report test_allure_step_x.py
> allure serve ./allure_report
報告展示效果如下
兩種方式對比
使用 with allure.step("step:步驟")
這種方式代碼可讀性更好一點,但不會帶上函數里面的傳參和對應的值。
使用 @allure.step("step:步驟")
這種方式會帶上函數的傳參和對應的值。
這兩種方式結合起來使用,才能更好的展示測試報告!