009、【TCSOFT】 接口用例设计、并用脚本实现自动化
题目如下:
本人做的答案如下:
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')