1.方式一:
測試用例中with allure.step():
# common_fucntion.py import allure import pytest ''' 流程性的用例,添加測試步驟,讓用例更清晰 用例步驟:1.登陸, 2.瀏覽商品 3.添加購物車 4.生成訂單 5.支付成功 ''' 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.支付成功 ''' @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
測試報告
2.方式二:
直接使用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")
用例
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
報告
3.兩種方式比較
使用 with allure.step("step:步驟")
這種方式代碼可讀性更好一點,但不會帶上函數里面的傳參和對應的值。
使用 @allure.step("step:步驟")
這種方式會帶上函數的傳參和對應的值。
這兩種方式結合起來使用,才能更好的展示測試報告!
結合使用案例:
with allure step(): 可以集成一些截圖,通過attach來格式化輸出報告