009、TCSOFT 介面用例設計、並用指令碼實現自動化
阿新 • • 發佈:2021-10-26
題目如下:
本人做的答案如下:
1、測試用例,放在data列表中,當然也可以其他的方式儲存資料 :
all_cases = [ {"req_data": {"username": "admin", "password": "admin123"}, "excepted_result": {"data": "歡迎", "errorid": "0"}}, {"req_data": {"username": "normal_user", "password": "admin123"}, "excepted_result": {"data": "登入失敗", "errorid": "-1"}} ]
2、python3 + pytest + requests ,自動化測試指令碼如下:
import jsonpath import pytest import requests # mock_url_success = 'https://www.fastmock.site/mock/60357489daf3dab0fbcd29eec66a17ff/interview/login_success' # mock_url_fail = 'https://www.fastmock.site/mock/60357489daf3dab0fbcd29eec66a17ff/interview/login_fail'url = 'http://localhost/login' all_cases = [ {"req_data": {"username": "admin", "password": "admin123"}, "excepted_result": {"data": "歡迎", "errorid": "0"}}, {"req_data": {"username": "normal_user", "password": "admin123"}, "excepted_result": {"data": "登入失敗", "errorid": "-1"}} ] @pytest.mark.parametrize("case", all_cases) def test_login(case): req_data = case.get('req_data') res = requests.post(url=url, json=req_data) data = jsonpath.jsonpath(res.json(), "$.data")[0] errorid = jsonpath.jsonpath(res.json(), "$.errorid")[0] actual_result = {} actual_result['data'] = data actual_result['errorid'] = errorid assert actual_result == case.get('excepted_result')