pytest(三十三)--allure.step()新增測試用例步驟
阿新 • • 發佈:2020-08-01
前言
一般流程性的測試用例,寫成自動化用例時,步驟較多寫起來會比較長。在測試用例裡面新增詳細的步驟有助於更好的閱讀,也方便報錯後快速的定位到問題。
舉個常見的測試場景用例:從登陸開始,到瀏覽商品新增購物車,最後下單支付。
用例步驟: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:步驟")這種方式會帶上函式的傳參和對應的值。
這兩種方式結合起來使用,才能更好的展示測試報告!