面试拯救计划!!!-------两周没写接口测试用例了--怎么从0到熟悉?

1.先复习基础概念和逻辑

https://www.cnblogs.com/bojiandkake/p/15760745.html

2.写一段代码,文字也要打出来

复制代码
import requests
def sign_test():
    #http请求消息体中 参数以x-www-form-urlencoded存储
    #请求消息体包含username和password
    #所以需要携带username password参数,把他们写到字典payload里去
    #然后把payload的值传给data参数,因为payload代表的是请求消息体,这个需要用data参数接收
    #如果是url里 a=c&b=d这种类型的参数,也可以写到字典里去,但是需要传给params参数
    #如果是请求头参数,要传给headers
    s=requests.Session()
    payload={'username':'byhy','password':'88888888'}
    response=s.post("http://127.0.0.1/api/mgr/signin",data=payload)
    #打印响应状态码
    print("---通过response.status_code打印出响应的状态码")
    print(response.status_code)
    #打印响应的响应头
    print("---通过response.headers打印出响应的响应头")
    print(response.headers,"\n")
    print("----通过response.text打印响应的消息体")
    body=response.text
    print(response.text,"\n")
    print("响应消息体的数据类型是",type(body))
sign_test()
复制代码

 

3.把这个接口文档里的接口全部实现一遍http://www.byhy.net/tut/webdev/django/doc_api_v1_2/

系统文档在这里https://www.byhy.net/prac/pub/info/bysms/

复制代码
import requests
import json
from hytest  import *

class APIMgr:
    # 请用 Python 和 Requests库,为接口文档 里面每个API 接口, 开发一个函数 发送对应的HTTP请求,接收并且打印出响应消息体中的内容。
    # 并且在测试过程中,使用 fiddler 抓包查看发送接收的HTTP消息。
    def print_response(self,res):
        print("\n*************response打印开始*************\n")
        # 打印响应行
        print(res.status_code)
        # 打印响应头
        for k, v in res.headers.items():
            print(f'{k}:{v}')
        # 打印响应体
        print(res.content.decode('utf8'))
        # print(res.content)

        print("\n*************response打印完成*************\n")

    def sign_test(self):
        # http 请求消息 body 中 参数以 格式 x-www-form-urlencoded 存储
        # 须携带username  password参数
        print("\n*************开始调用sign函数*************\n")
        self.s = requests.Session()
        payload = {'username': 'byhy', 'password': '88888888'}
        response = self.s.post("http://127.0.0.1/api/mgr/signin", data=payload)
        self.print_response(response)#调用类里的实例方法
        print("\n*************结束调用sign函数*************\n")
        return response

    def list_all_customer(self,action='list_customer', pagesize=1, pagenum=1, keywords=''):
        # json格式存储消息体数据,还是写到字典里,但是调用的时候用json=payload或者data=json.dumps(payload)

        # proxies={'http':'http://127.0.0.1:8888',
        # 'https':'http://127.0.0.1:8888'}
        urlpara = {'action': action, 'pagesize': pagesize, 'pagenum': pagenum, 'keywords': keywords}
        res = self.s.get('http://127.0.0.1/api/mgr/customers', params=urlpara)
        self.print_response(res)
        return res

    def add_customer(self,name, phonenumber, address):
        payload = {
            "action": "add_customer",
            "data": {
                "name": name,
                "phonenumber": phonenumber,
                "address": address
            }
        }
        # 请求消息体是json格式的,所以要写成data=json.dumps(payload)或者json=payload
        res = self.s.post('http://127.0.0.1/api/mgr/customers', data=json.dumps(payload))
        self.print_response(res)
        return res

    def add_customer2(self,data):
        payload = {
            "action": "add_customer",
            "data": data
        }
        # 请求消息体是json格式的,所以要写成data=json.dumps(payload)或者json=payload
        res = self.s.post('http://127.0.0.1/api/mgr/customers', data=json.dumps(payload))
        self.print_response(res)
        return res

    def modify_customer_information(self,cid,name, phonenumber, address):

        # 请求消息体格式是json,传递给参数data,用data=json.dumps(payload)或者直接传递给参数json,json=payload
        payload = {
            "action": "modify_customer",
            "id": cid,
            "newdata": {
                "name": name,
                "phonenumber": phonenumber,
                "address": address
            }
        }
        res = self.s.post('http://127.0.0.1/api/mgr/customers', data=json.dumps(payload))
        self.print_response(res)
        return res

    def delete_customer_information(self,cid):

        # 请求消息体格式是json,传递给参数data,用data=json.dumps(payload)或者直接传递给参数json,json=payload
        payload = {
            "action": "del_customer",
            "id": cid
        }
        res = self.s.post('http://127.0.0.1/api/mgr/customers', data=json.dumps(payload))
        self.print_response(res)
        return res
    def delete_all_customer(self):
        #返回的是json格式的数据,所以要调用res.json变成python里的字典格式数据
        res=self.list_all_customer(pagesize=100, pagenum=1)
        thelist=res.json()['retlist']
        print(thelist,"----需要删除的客户列表")
        for one in thelist:
            self.delete_customer_information(cid=one['id'])
            print(f'删除id为{one["id"]}的数据')



    def list_all_medicine(self,action='list_medicine', pagesize=1, pagenum=1, keywords=''):

        # url里的参数用params接收
        urlpara = {'action': action, 'pagesize': pagesize, 'pagenum': pagenum, 'keywords': keywords}
        res = self.s.get('http://127.0.0.1/api/mgr/medicines', params=urlpara)
        self.print_response(res)
        return res

    def add_medicine(self,name="青霉素", desc="青霉素 国字号", sn="099877883837"):

        # 请求消息体的格式是json,所以应该用参数data来接收,data=json.dumps(payload)或者用参数json直接接收json=payload
        payload = {
            "action": "add_medicine",
            "data": {
                "name": name,
                "desc": desc,
                "sn": sn
            }
        }
        res = self.s.post('http://127.0.0.1/api/mgr/medicines', json=payload)
        self.print_response(res)
        return res

    def modify_medicine(self,name="青霉素", desc="青霉素 国字号", sn="099877883837"):

        # 请求消息体的格式是json,所以应该用参数data来接收,data=json.dumps(payload)或者用参数json直接接收json=payload
        payload = {
            "action": "modify_medicine",
            "id": 6,
            "newdata": {
                "name": name,
                "desc": desc,
                "sn": sn
            }
        }
        res = self.s.post('http://127.0.0.1/api/mgr/medicines', json=payload)
        self.print_response(res)
        return res

    def delete_medicine(self,cid):

        # 请求消息体的格式是json,所以应该用参数data来接收,data=json.dumps(payload)或者用参数json直接接收json=payload
        payload = {
            "action": "del_medicine",
            "id": cid
        }
        res = self.s.post('http://127.0.0.1/api/mgr/medicines', json=payload)
        self.print_response(res)
        return res

    def delete_all_medicine(self):
        # 返回的是json格式的数据,所以要调用res.json变成python里的字典格式数据
        res = self.list_all_medicine(pagesize=100, pagenum=1)
        thelist = res.json()['retlist']
        for one in thelist:
            self.delete_medicine(cid=one['id'])

    def list_all_orders(self,action='list_order', pagesize=1, pagenum=1, keywords=''):

        urlpara = {
            'action': action,
            'pagesize': pagesize,
            'pagenum': pagenum,
            'keywords': keywords

        }
        res = self.s.get('http://127.0.0.1/api/mgr/orders', params=urlpara)
        self.print_response(res)
        return res

    def add_order(self,name="华山医院订单002", customerid=3, medicinelist=[
        {"id": 16, "amount": 5, "name": "环丙沙星"},
        {"id": 15, "amount": 5, "name": "克林霉素"}
    ]):

        payload = {
            "action": "add_order",
            "data": {
                "name": name,
                "customerid": customerid,
                "medicinelist": medicinelist
            }
        }
        # 请求消息体是json  可以传递给参数data,data=json.dumps(payload)或者传递给json,json=payload
        res = self.s.post('http://127.0.0.1/api/mgr/orders', json=payload)
        self.print_response(res)
        return res

    def delete_order(self,action="delete_order", cid=6):

        payload = {
            "action": "delete_order",
            "id": cid
        }
        res = self.s.post('http://127.0.0.1/api/mgr/orders', json=payload)
        self.print_response(res)
        return res

    def delete_all_order(self):
        # 返回的是json格式的数据,所以要调用res.json变成python里的字典格式数据
        res = self.list_all_orders(pagesize=100, pagenum=1)
        thelist = res.json()['retlist']
        for one in thelist:
            self.delete_order(cid=one['id'])

apimgr=APIMgr()
复制代码

 

4.思考删除一个客户和删除所有客户的实现方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#删除一个客户,按id
  def delete_customer(self,id):
      payload={
          "action": "del_customer",
          "id":id
      }
      #json.dumps()将python对象编码成json字符串,因为接口文档中,这个删除接口要求的消息体格式是json格式
      res=self.s.delete('http://127.0.0.1/api/mgr/customers',data=json.dumps(payload))
      self.printResponse(res)
      return res
#删除所有客户  用for循环
  def delete_all_customer(self):
      res=self.list_all_customers()
      resObj=res.json()
      retlist=resObj['retlist']
      #[{'id': 244, 'name': '武汉市桥西医院', 'phonenumber': '13345679934', 'address': '武汉市桥西医院北路'},
      # {'id': 242, 'name': '武汉市桥西医院', 'phonenumber': '13345679934', 'address': '武汉市桥西医院北路'}]
      for one in retlist:
          self.delete_customer(one['id'])

  

 

posted @   今天也是开心的一天呀  阅读(66)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示