第十期_接口测试框架_20191013

课前准备

  • requests
  • json
  • jsonpath
  • 代理工具charles

参考资料

Requests框架

常用HTTP方法

def request(method, url, **kwargs):
    """Constructs and sends a :class:`Request <Request>`.

    :param method: method for the new :class:`Request` object.
    :param url: URL for the new :class:`Request` object.
    :param params: (optional) Dictionary, list of tuples or bytes to send
        in the query string for the :class:`Request`.
    :param data: (optional) Dictionary, list of tuples, bytes, or file-like
        object to send in the body of the :class:`Request`.
    :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.
    :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
    :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
    :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
        ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
        or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
        defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
        to add for the file.
    :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
    :param timeout: (optional) How many seconds to wait for the server to send data
        before giving up, as a float, or a :ref:`(connect timeout, read
        timeout) <timeouts>` tuple.
    :type timeout: float or tuple
    :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
    :type allow_redirects: bool
    :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
    :param verify: (optional) Either a boolean, in which case it controls whether we verify
            the server's TLS certificate, or a string, in which case it must be a path
            to a CA bundle to use. Defaults to ``True``.
    :param stream: (optional) if ``False``, the response content will be immediately downloaded.
    :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
    :return: :class:`Response <Response>` object
    :rtype: requests.Response

    Usage::

      >>> import requests
      >>> req = requests.request('GET', 'https://httpbin.org/get')
      <Response [200]>
    """

    # By using the 'with' statement we are sure the session is closed, thus we
    # avoid leaving sockets open which can trigger a ResourceWarning in some
    # cases, and look like a memory leak in others.
    with sessions.Session() as session:
        return session.request(method=method, url=url, **kwargs)


response的结构定义:/Users/seveniruby/PycharmProjects/Hogwarts10/venv/lib/python3.7/site-packages/requests/models.py

json请求

POST /post HTTP/1.1
Host: 47.95.238.18:8090
User-Agent: python-requests/2.22.0
Accept-Encoding: gzip, deflate
accept: application/json
a: 2
bcd: header demo
Content-Length: 48
Content-Type: application/json
Connection: keep-alive

{"key1": "value1", "key2": ["value2", "value3"]}

data格式

POST /post HTTP/1.1
Host: 47.95.238.18:8090
User-Agent: python-requests/2.22.0
Accept-Encoding: gzip, deflate
accept: application/json
a: 2
bcd: header demo
Content-Length: 35
Content-Type: application/x-www-form-urlencoded
Connection: keep-alive

key1=value1&key2=value2&key2=value3

普通断言

JsonPath断言

schema断言

断言作用

  • 字段有没有丢失
  • 字段的类型是否发生变化
  • 字段的内容是否符合常见标准,比如最大,最小,长度,正则等

课间作业1

  • test_get_login() 发起get请求https://testerhome.com/api/v3/topics.json?limit=2 断言最后一条结果中的帖子的用户名是liangqiangWang
  • test_xueqiu_search 在雪球上发起股票搜索,搜索sogo股票,断言结果中有名字为“搜狗”的股票
curl 'https://xueqiu.com/stock/search.json?code=sogo&size=3&page=1' -H 'Pragma: no-cache' -H 'Sec-Fetch-Site: same-origin' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36' -H 'elastic-apm-traceparent: 00-bc6451de99efe2adca83c45c80d617eb-056132c150797d3a-01' -H 'Sec-Fetch-Mode: cors' -H 'Accept: application/json, text/plain, */*' -H 'Cache-Control: no-cache' -H 'Referer: https://xueqiu.com/k?q=sogo' -H 'Cookie: device_id=24700f9f1986800ab4fcc880530dd0ed; aliyungf_tc=AQAAAOTGXRY/YQUAFgm3c9RY0OWcM+Al; acw_tc=2760827f15709520483665936e798d7d5df765e1672a47048c3f51b14b0625; xq_a_token=d831cd39b53563679545656fba1f4efd8e48faa0; xq_r_token=fd2f0f487c8298cad8e7519f1560abb7a18c589d; u=821570952049723; Hm_lvt_1db88642e346389874251b5a1eded6e3=1569419106,1569504678,1570952051; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1570952072' -H 'Connection: keep-alive' --compressed

JSON XML请求

JSON XML响应断言

auth认证

课后作业2

企业微信的接口初步熟悉