第一種方式:ids
1.參數化(pytest.mark.parametrize)
# test_a.py import pytest import allure def login(username, password): '''登錄''' print("輸入賬號:%s" % username) print("輸入密碼:%s" % password) # 返回 return {"code": 0, "msg": "success!"} # 測試數據 test_datas = [ ({"username": "yoyo1", "password": "123456"}, "success!"), ({"username": "yoyo2", "password": "123456"}, "failed!"), ({"username": "yoyo3", "password": "123456"}, "success!"), ] @allure.story("登錄用例") @pytest.mark.parametrize("test_input,expected", test_datas ) def test_login(test_input, expected): '''測試登錄用例''' # 獲取函數返回結果 result = login(test_input["username"], test_input["password"]) # 斷言 assert result["msg"] == expected
運行用例
> pytest --alluredir ./report test_a.py
> allure serve ./report
這樣生成的報告在用例列表里面並不能很友好的展示出每個用例的執行場景,只知道哪個用例報錯了。
於是需要對每個用例加上描述,加一個 ids 參數
2.ids參數使用
在用例部分代碼里面加個 ids 參數,用於描述每個用例的運行場景。
@allure.story("登錄用例") @pytest.mark.parametrize("test_input,expected", test_datas, ids=[ "輸入正確賬號,密碼,登錄成功", "輸入錯誤賬號,密碼,登錄失敗", "輸入正確賬號,密碼,登錄成功", ] ) def test_login(test_input, expected): '''測試登錄用例''' # 獲取函數返回結果 result = login(test_input["username"], test_input["password"]) # 斷言 assert result["msg"] == expected
第二種方式:allure.title
@allure.title("用例描述,測試輸入:{test_input}")
在 allure_pytest/utils.py 源碼里面可以找到對應的代碼
# allure_pytest/utils.py def allure_name(item, parameters): name = escape_name(item.name) title = allure_title(item) return title.format(**parameters) if title else name
當沒有加allure.title()時候,用例的描述就是 item.name 值(也就是上面的 ids 用例的名稱),
如果加了allure.title(),那么用例的描述就是添加的title值,這兩個地方取其中的一個。
重點來了:把用例描述當成參數傳給測試用例
import pytest import allure # 作者:上海-悠悠 QQ交流群:779429633 def login(username, password): '''登錄''' print("輸入賬號:%s" % username) print("輸入密碼:%s" % password) # 返回 return {"code": 0, "msg": "success!"} # 測試數據 test_datas = [ ({"username": "yoyo1", "password": "123456"}, "success!", "輸入正確賬號,密碼,登錄成功"), ({"username": "yoyo2", "password": "123456"}, "failed!", "輸入錯誤賬號,密碼,登錄失敗"), ({"username": "yoyo3", "password": "123456"}, "success!", "輸入正確賬號,密碼,登錄成功"), ] @allure.story("登錄用例") @allure.title("{title}") @pytest.mark.parametrize("test_input,expected,title", test_datas ) def test_login(test_input, expected, title): '''測試登錄用例''' # 獲取函數返回結果 result = login(test_input["username"], test_input["password"]) # 斷言 assert result["msg"] == expected