Isaac Sim vs MuJoCo vs PyBullet:机器人仿真器选型终极指南(2026版)
2026/6/4 16:47:35
importrequests# 创建 session对象session=requests.Session()# 发送 验证码请求 getsession.get(url="http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=verify&r=0.1858685996048326")# 发送 登录请求 postresp=session.post(url="http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=do_login&t=0.11324043675270756",data={"username":"13812345678","password":"12345678","verify_code":"8888"})# 打印响应结果print("登录结果:",resp.json())封装思想:
分析:
importrequests# 封装 tpshop 商城 接口对象层classTpshopLoginApi(object):# 获取验证码@classmethod# 装饰器defget_verify(cls,session):session.get(url="http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=verify")# 登录@classmethoddeflogin(cls,session,login_data):resp=session.post(url="http://tpshop-test.itheima.net/index.php?m=Home&c=User&a=do_login",data=login_data)returnresp# 返回的结果# 测试,自己封装的接口,功能是否正常!(自己测试使用)if__name__=='__main__':# 创建sessionsession=requests.Session()# 发送获取验证码请求TpshopLoginApi.get_verify(session)login_data={"username":"13812345678","password":"123456","verify_code":"8888"}# 发送登录请求,调完这个接口接收的response=TpshopLoginApi.login(session,login_data)print("登录结果:",response.json())使用 接口测试框架 pytest调用自己封装的 Api,发送请求,获取响应结果,断言。完成接口测试!
测试方法按照测试用例来写。
pycharm终端运行:pytest -s xx.py
importrequestsfrompy02_tpshop_apiimportTpshopLoginApiclassTestTpshopLogin(object):# 定义 测试方法 - 登录成功deftest01_login_success(self):# 创建 session 实例session=requests.Session()# 调用自己封装的API,发送 获取验证码 请求。(类名.方法名)TpshopLoginApi.get_verify(session)# 准备 请求体数据req_data={"username":"13812345678","password":"123456","verify_code":"8888"}# 调用自己封装的API,发送 登录 请求resp=TpshopLoginApi.login(session,req_data)# 打印响应结果print("登录成功:",resp.json())# 断言assert200==resp.status_codeassert1==resp.json().get("status")assert"登陆成功"inresp.json().get("msg")# 定义 测试方法 - 密码错误deftest02_pwd_err(self):# 创建 session 实例session=requests.Session()# 调用自己封装的API,发送 获取验证码 请求TpshopLoginApi.get_verify(session)# 准备 请求体数据req_data={"username":"13812345678","password":"12345689","verify_code":"8888"}# 调用自己封装的API,发送 登录 请求resp=TpshopLoginApi.login(session,req_data)# 打印响应结果print("密码错误:",resp.json())# 断言assert200==resp.status_codeassert-2==resp.json().get("status")assert"密码错误"inresp.json().get("msg")# 定义 测试方法 - 验证码错误deftest03_verify_err(self):# 创建 session 实例session=requests.Session()# 调用自己封装的API,发送 获取验证码 请求TpshopLoginApi.get_verify(session)# 准备 请求体数据req_data={"username":"13812345678","password":"123456","verify_code":"8976"}# 调用自己封装的API,发送 登录 请求resp=TpshopLoginApi.login(session,req_data)# 打印响应结果print("验证码错误:",resp.json())# 断言assert200==resp.status_codeassert0==resp.json().get("status")assert"验证码错误"inresp.json().get("msg")在执行每个方法之前,执行一遍setup定义的方法。
importrequestsfrompy02_tpshop_apiimportTpshopLoginApiclassTestTpshopLogin(object):# 定义为类属性。 使用时,类属性 可以用 类名、实例、self、cls 均可以引用session=Nonedefsetup(self):# 创建 session 实例self.session=requests.Session()# 调用自己封装的API,发送 获取验证码 请求TpshopLoginApi.get_verify(self.session)# 定义 测试方法 - 登录成功deftest01_login_success(self):# 准备 请求体数据req_data={"username":"13812345678","password":"123456","verify_code":"8888"}# 调用自己封装的API,发送 登录 请求resp=TpshopLoginApi.login(self.session,req_data)# 打印响应结果print("登录成功:",resp.json())# 断言assert200==resp.status_codeassert1==resp.json().get("status")assert"登陆成功"inresp.json().get("msg")# 定义 测试方法 - 密码错误deftest02_pwd_err(self):# 准备 请求体数据req_data={"username":"13812345678","password":"12345689","verify_code":"8888"}# 调用自己封装的API,发送 登录 请求resp=TpshopLoginApi.login(self.session,req_data)# 打印响应结果print("密码错误:",resp.json())# 断言assert200==resp.status_codeassert-2==resp.json().get("status")assert"密码错误"inresp.json().get("msg")# 定义 测试方法 - 验证码错误deftest03_verify_err(self):# 准备 请求体数据req_data={"username":"13812345678","password":"123456","verify_code":"8976"}# 调用自己封装的API,发送 登录 请求resp=TpshopLoginApi.login(self.session,req_data)# 打印响应结果print("验证码错误:",resp.json())# 断言assert200==resp.status_codeassert0==resp.json().get("status")assert"验证码错误"inresp.json().get("msg")