如果你還想從頭學起Pytest,可以看看這個系列的文章哦!
https://www.cnblogs.com/poloyy/category/1690628.html
前言
- 參數化 @pytest.mark.parametrize 的學習:https://www.cnblogs.com/poloyy/p/12675457.html
- 默認 allure 報告上的測試用例標題不設置默認就是用例名稱,這樣可讀性不高
- 當結合 @pytest.mark.parametrize 參數化完成數據驅動時,如果標題寫死,這樣可讀性也不高
- 所以我們希望標題可以動態的生成,來看看如何做吧
參數化無標題的栗子
測試代碼
#!/usr/bin/env python # -*- coding: utf-8 -*- """ __title__ = __Time__ = 2020/10/28 15:08 __Author__ = 小菠蘿測試筆記 __Blog__ = https://www.cnblogs.com/poloyy/ """ import allure import pytest @pytest.fixture() def login(request): """登錄""" param = request.param print(f"賬號是:{param['username']},密碼是:{param['pwd']}") # 返回 return {"code": 0, "msg": "success!"} datas = [ {"username": "name1", "pwd": "pwd1"}, {"username": "name2", "pwd": "pwd2"}, {"username": "name3", "pwd": "pwd3"} ] @allure.story('登錄功能') @pytest.mark.parametrize('login', datas, indirect=True) def test_login1(login): """ 登錄測試用例1 """ assert login['code'] == 0
allure 報告
標題就是方法名+參數化的數據,看着可讀性就不咋滴
參數化有標題寫死的栗子
測試代碼
將上面的測試代碼添加一個 @allure.title 就可以了
@allure.story('登錄功能') @allure.title('登錄測試用例2') @pytest.mark.parametrize('login', datas, indirect=True) def test_login2(login): """ 登錄測試用例2 """ assert login['code'] == 0
allure 報告
因為參數化可以生成三條用例,所以三條用例都用了同一個 title,可讀性也不咋滴
參數化使用 ids 的栗子
測試代碼
#!/usr/bin/env python # -*- coding: utf-8 -*- """ __title__ = __Time__ = 2020/10/28 15:08 __Author__ = 小菠蘿測試筆記 __Blog__ = https://www.cnblogs.com/poloyy/ """ import allure import pytest @pytest.fixture() def login(request): """登錄""" param = request.param print(f"賬號是:{param['username']},密碼是:{param['pwd']}") # 返回 return {"code": 0, "msg": "success!"} datas = [ {"username": "name1", "pwd": "pwd1"}, {"username": "name2", "pwd": "pwd2"}, {"username": "name3", "pwd": "pwd3"} ] ids = [ "username is name1,pwd is pwd1", "username is name2,pwd is pwd2", "username is name3,pwd is pwd3" ] @allure.story('登錄功能') @pytest.mark.parametrize('login', datas, ids=ids, indirect=True) def test_login1(login): """ 登錄測試用例1 """ assert login['code'] == 0
allure 報告
參數化動態生成標題的栗子
測試代碼
#!/usr/bin/env python # -*- coding: utf-8 -*- """ __title__ = __Time__ = 2020/10/28 15:08 __Author__ = 小菠蘿測試筆記 __Blog__ = https://www.cnblogs.com/poloyy/ """ import allure import pytest @pytest.fixture() def login(request): """登錄""" param = request.param print(f"賬號是:{param['username']},密碼是:{param['pwd']}") # 返回 return {"code": 0, "msg": "success!"} datas = [ {"username": "name1", "pwd": "pwd1"}, {"username": "name2", "pwd": "pwd2"}, {"username": "name3", "pwd": "pwd3"} ] data2 = [ ("name1", "123456"), ("name2", "123456"), ("name3", "123456") ] @allure.story('分別傳值') @allure.title('登錄測試用例2-賬號是:{username}-密碼是:{pwd}') @pytest.mark.parametrize('username,pwd', data2) def test_login1(username, pwd): """ 登錄測試用例1 """ print(username, pwd) @allure.story('字典參數化') @allure.title('登錄測試用例2-{dict}') @pytest.mark.parametrize('dict', datas) def test_login2(dict): """ 登錄測試用例1 """ print(dict['username'], dict['pwd']) @allure.story('傳值進fixture') @allure.title('登錄測試用例2{login}') @pytest.mark.parametrize('login', datas, indirect=True) def test_login3(login): """ 登錄測試用例2 """ assert login['code'] == 0
allure 報告
傳入的如果是一個字典則顯示完整字典值
參數化動態生成標題最優方案的栗子
測試代碼
#!/usr/bin/env python # -*- coding: utf-8 -*- """ __title__ = __Time__ = 2020/10/28 15:08 __Author__ = 小菠蘿測試筆記 __Blog__ = https://www.cnblogs.com/poloyy/ """ import allure import pytest data = [ ("name1", "123456", "name1 登錄成功"), ("name2", "123456", "name2 登錄失敗"), ("name3", "123456", "name3 登錄成功") ] @allure.story('分別傳值') @allure.title('登錄測試用例-{title}') @pytest.mark.parametrize('username,pwd,title', data) def test_login1(username, pwd, title): """ 登錄測試用例1 """ print(username, pwd)
allure 報告
這種做法的優點
- 可以自定義各式各樣的標題
- 單獨一個值去維護標題值
- 可讀性比較好,容易維護