1.实现多组合测试0-0、0-1、1-0、1-1
测试数据:
iphone_json = [{ "devicePhonePojoList": [ { "registrationId": "65ou4oadi7o7fuo", "mobile": "" } ] }, { "devicePhonePojoList": [ { "registrationId": "65ou4oadi7o7fuo", "mobile": "18321793913" } ] }] testMobile = [[""],["18321793913"]]
测试代码:
@pytest.mark.parametrize('mobile', testMobile) # 输出 @pytest.mark.parametrize('iphone', iphone_json) # 输入
def test_update_iphone(mobile, iphone): url = url_web + '/phone' headers = {"Content-Type": "application/json", "key": "2dbe655e88c80"} tel = RequestHandler() response = tel.visit(method='post', url=url, headers=headers, json=iphone) assert response.status_code == 200 logging.info("update iphone接口调用成功,status_code==200") GetNum = get_iphone() time.sleep(2) try: assert mobile == GetNum logging.info('update iphone成功') except Exception as e: logging.info('update的iphone,与get的iphone,不一致')
运行结果:
test_webapi.py::test_update_iphone[iphone0-mobile0] test_webapi.py::test_update_iphone[iphone1-mobile1] test_webapi.py::test_update_iphone[iphone0-mobile1] test_webapi.py::test_update_iphone[iphone1-mobile0]
2.确定的测试数据和输出结果验证
测试数据:
t1 = ({'devicePhonePojoList': [{'registrationId': '65ou4oadi7o7fuo', 'mobile': ''}]}, ['1']) t2 = ({'devicePhonePojoList': [{'registrationId': '65ou4oadi7o7fuo', 'mobile': '18321793913'}]}, ['18321793913'])
测试代码:
@pytest.mark.parametrize('iphone,mobile',[t1,t2]) def test_update_iphone(mobile, iphone): url = url_web + '/phone' headers = {"Content-Type": "application/json", "key": "2dbe655e88c80"} tel = RequestHandler() response = tel.visit(method='post', url=url, headers=headers, json=iphone) assert response.status_code == 200 logging.info("update iphone接口调用成功,status_code==200") GetNum = get_iphone() time.sleep(2) try: assert mobile == GetNum logging.info('update iphone成功') except Exception as e: logging.info('update的iphone,与get的iphone,不一致')
运行结果:
test_webapi.py::test_update_iphone[iphone0-mobile0] PASSED [ 50%] test_webapi.py::test_update_iphone[iphone1-mobile1] PASSED [100%] ====================================================== 2 passed in 4.28s ======================================================