相对复杂的接口测试

Posted 软件测试培训

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相对复杂的接口测试相关的知识,希望对你有一定的参考价值。

https://item.jd.com/34295655089.html

https://item.jd.com/12082665.html


店铺二维码:


下面来看一下如何测试建立订单。先来看一下与订单相关的两个数据库,

goods_orders

goods_order

一个商品对应的单条订单。包含数量,商品编号,用户编号和表goods_orders的外键。

案例:生成订单的接口测试。

# 订单测试的数据准备def check_condition_for_order(self):# 获取Excel信息username =self.get_user_data()[0] # 创建订单的用户名password =self.get_user_data()[1] # 创建订单的用户名对应的密码goods_name = self.get_goods_data()[0] # 获取订单商品名称goods_price = self.get_goods_data()[1] # 获取订单商品单goods_pic = self.get_goods_data()[2] # 获取订单商品图片goods_desc = self.get_goods_data()[3] # 获取订单商品描述address_name =self.get_address_data()[0] # 获取订单地址信息phone = self.get_address_data()[1] # 获取订单地址对应的电话# 检查订单用户是否存在self.check_user_existence(username,password,1)# 获取用户名对应的iduser_id = self.get_user_id(username)# 查看id=1、id=2、id=3的商品信息是否存在,不存在自动建立self.check_goods_for_order(goods_name,goods_price,goods_pic,goods_desc)# 获取登录用户的一个配货地址idaddress_id = self.get_user_addess_id(user_id,address_name,phone)return{"username":username,"password":password,"address_id":address_id,"user_id":user_id}


其中get_user_id(username)获取当前和用户的id号。

# 获取当前用户的id号def get_user_id(self,username):db =DB()db.connect()condition = "username='"+username+"'"results =db.searchByCondition("goods_user",condition)for result in results:user_id = result[0]breakdb.close()return user_id


check_goods_for_order(goods_name,goods_price,goods_pic,goods_desc),检查是否存在id123的商品信息,如果没有,就用excel表中的数据(goods_price,goods_pic,goods_desc来自excel表)。

# 查看id=1、id=2、id=3的商品信息是否存在defcheck_goods_for_order(self,goods_name,price,picture,dec):db =DB()db.connect()for i in range(3):result =db.searchById("goods_goods",str(i+1))if len(list(result))==0:value =(str(i+1)+",'"+goods_name+"','"+price+"','upload/"+picture+"','"+dec+"'")print("商品信息'"+goods_name+"'由于数据问题由测试程序自动生成的数据")db.insert("goods_goods",value)db.close()


# 获取当前用户有没有送货地址iddefget_user_addess_id(self,user_id,address_name,phone):db =DB()db.connect()address_id=""condition = "user_id ="+str(user_id)results =db.searchByCondition("goods_address",condition)if len(list(results))==0:value ="'"+address_name+"','"+phone+"',"+str(user_id)db.insert("goods_address(address,phone,user_id)",value)print("地址信息'"+address_name+"'由于数据问题由测试程序自动生成的数据")condition = "user_id ="+str(user_id)results =db.searchByCondition("goods_address",condition)else:results =db.searchByCondition("goods_address",condition)for result in results:address_id = result[0]breakdb.close()return address_id


# 检查创建订单def test_create_order(self):# 初始化订单测试数据info =self.myutil.check_condition_for_order()mysession = requests.Session() # 关联session对象建立#用户登录参数payload={"username":info["username"],"password":info["password"],"csrfmiddlewaretoken":self.token}# 模拟用户登录data =mysession.post(self.url_product,data=payload,cookies=self.cookie)# 创建订单参数payload={"address":info["address_id"],"csrfmiddlewaretoken":self.token}# 创建产品id,分别为id=1、id=2、id=3号self.cookie.update({"1":"1","2":"2","3":"3"})# 创建订单data =mysession.post(self.url_craete_order,data=payload,cookies=self.cookie) # 验证返回码self.assertEqual("200",str(data.status_code))# 验证返回内容self.assertIn("生成时间",str(data.text))# 验证完毕删除订单self.myutil.delete_order(info["user_id"])


案例:删除订单的接口测试

# 删除订单def delete_order(self,user_id):db =DB()db.connect()# 通过user_id,good_id查询orderresults =db.searchByCondition("goods_order","user_id="+str(user_id)+"and goods_id=1")for result in results:order_id = result[4]break# 删除分订单db.deleteByCondition("goods_order","order_id="+str(order_id))# 删除总订单db.deleteById("goods_orders",str(order_id))db.close()


书写完了建立订单的测试代码,再来看看删除订单的测试代码。删除订单首先要建立订单,建立订单之前仍旧需要检查订单数据的有效性,有了前面测试用例的封装,有些代码就可以复用了。在这里先来展示测试代码。

# 检查删除订单def test_delete_order(self):# 初始化订单测试数据info =self.myutil.check_condition_for_order()# 建立订单orders_id =self.myutil.create_order(info["address_id"],info["user_id"])# 关联session对象建立mysession = requests.Session()# 用户登录参数payload={"username":info["username"],"password":info["password"],"csrfmiddlewaretoken":self.token}# 模拟用户登录data =mysession.post(self.url_product,data=payload,cookies=self.cookie)# 创建订单参数payload={"address":info["address_id"],"csrfmiddlewaretoken":self.token}# 创建产品id,分别为id=1、id=2、id=3号self.cookie.update({"1":"1","2":"2","3":"3"})# 创建订单data =mysession.post(self.url_deleted_order+str(orders_id)+"/3/",data=payload,cookies=self.cookie)# 验证返回码self.assertEqual("200",str(data.status_code))# 验证返回内容self.assertNotIn("删除",str(data.text))


check_condition_for_order()仍旧可以使用,然后使用数据库建立订单数据。

# 生成订单defcreate_order(self,address_id,user_id):db =DB()db.connect()# 建立ordersfield="(create_time,status,address_id)"values = "'"+time.strftime('%Y-%m-%d%H:%M:%S',time.localtime(time.time()))+"','0',"+str(address_id)db.insert("goods_orders"+field,values)# 获取刚建立订单的id号results =db.searchByCondition("goods_orders","address_id="+str(address_id))for result in results:orders_id = result[0]break# 建立orderfield="(count,goods_id,user_id,order_id)"values ="1,1,"+str(user_id)+","+str(orders_id)db.insert("goods_order"+field,values)# 获取刚建orders的id号results =db.searchByCondition("goods_order","order_id="+str(orders_id))for result in results:orders_id = result[0]breakdb.close()return(orders_id)


最后就可以测试订单删除功能了。

顾老师性能测试课程

接口自动化测试

https://study.163.com/course/courseMain.htm?courseId=1209794815&share=2&shareId=480000002205486

以上是关于相对复杂的接口测试的主要内容,如果未能解决你的问题,请参考以下文章

什么是接口测试?为什么要做接口测试?

什么是接口测试?为什么要做接口测试?

什么是接口测试?为什么要做接口测试?建议收藏

接口测试

JMeter接口测试-模块控制器

Flask 编写http接口api及接口自动化测试