HttpRunner 2.x学习extract提取token值参数关联

本文介绍了使用Python中的HttpRunner测试框架编写测试用例的过程。首先需要安装HttpRunner 2.5.7版本,然后使用登录接口作为案例进行说明。

登录接口相关信息如下:

访问地址:http://127.0.0.1:8000/api/v1/login/
请求类型:POST
请求头部:application/json
请求参数:{“username”:“test”, “password”:“123456”}
使用httpapi命令行工具访问后,可以得到以下请求和响应信息:

POST /api/v1/login/ HTTP/1.1
Accept: application/json, */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 42
Content-Type: application/json
Host: 127.0.0.1:8000
User-Agent: HTTPie/1.0.3

{
    "password": "123456",
    "username": "test"
}

HTTP/1.1 200 OK
Allow: POST, OPTIONS
Content-Length: 109
Content-Type: application/json
Date: Thu, 19 Sep 2019 15:15:18 GMT
Server: WSGIServer/0.2 CPython/3.6.0
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN

{
    "code": 0,
    "msg": "login success!",
    "token": "000038efc7edc7438d781b0775eeaa009cb64865",
    "username": "test"
}

接下来,将请求和响应信息转换成HttpRunner的YAML格式脚本,保存为test_login.yml,如下所示:

config:
    name: logincase
    variables: {}

test:
    name: login case1
    request:
        url: http://127.0.0.1:8000/api/v1/login/
        method: POST
        headers:
            Content-Type: application/json
            User-Agent: python-requests/2.18.4
        json:
            username: test
            password: 123456
    validate:
        - eq: [status_code, 200]
        - eq: [headers.Content-Type, application/json]
        - eq: [content.msg, login success!]
        - eq: [content.code, 0]

如果不喜欢YAML格式,可以使用JSON格式,新建一个test_login2.json文件,内容如下:

[
    {
        "config": {
            "name": "logincase",
            "variables": {}
        }
    },
    {
        "test": {
            "name": "login case1",
            "request": {
                "url": "http://127.0.0.1:8000/api/v1/login/",
                "method": "POST",
                "headers": {
                    "Content-Type": "application/json",
                    "User-Agent": "python-requests/2.18.4"
                },
                "json": {
                    "username": "test",
                    "password": "123456"
                }
            },
            "validate": [
                {
                    "eq": ["status_code", 200]
                },
                {
                    "eq": ["headers.Content-Type", "application/json"]
                },
                {
                    "eq": ["content.msg", "login success!"]
                },
                {
                    "eq": ["content.code", 0]
                }
            ]
        }
    }
]

最后,使用hrun命令运行测试用例,可以得到以下执行结果:

INFO     HttpRunner version: 2.5.7
INFO     Start to run testcase: logincase login case1
INFO     POST http://127.0.0.1:8000/api/v1/login/
INFO     status_code: 200, response_time(ms): 180.63 ms, response_length: 109 bytes
.
----------------------------------------------------------------------
Ran 1 test in 0.184s

OK
INFO     Start to render Html report ...
INFO     Generated Html report: reports\20200612T111815.719602.html

执行完成后,在当前目录下生成一个report文件夹,里面包含一个HTML格式的测试报告文件,可以查看测试详情,包括请求、响应和断言等信息。

extract提取token值参数关联(上个接口返回的token,传给下个接口请求参数)

如何将上个接口返回的token,传给下个接口当作请求参数?这是最常见的一个问题了。解决这个问题其实很简单,我们只需取出token值,设置为一个中间变量a,下个接口传这个变量a就可以了。那么接下来就是解决两个问题:

  1. 如何取出token值?
  2. 如何参数关联?

环境:httprunner==2.5.7

场景案例:

我现在有一个登陆接口A,登陆成功后返回一个token值。有一个获取绑定卡号的接口B,但是接口B必须要先登录后传登录的token才能访问。

A接口登录接口文档基本信息:

访问地址:http://127.0.0.1:8000/api/v1/login/
请求类型:POST
请求头部:application/json
请求参数:{“username”:“test”, “password”:“123456”}

B接口获取绑定卡号的接口文档基本信息:

访问地址:http://127.0.0.1:8000/api/v1/userinfo/
请求类型:GET
请求头部:Content-Type: application/json
请求头部token参数:Authorization: Token xxxxx login token xxxxx

先不带token去访问接口B,使用命令行工具httpie测试接口:

C:\Users\dell>http http://127.0.0.1:8000/api/v1/userinfo/

HTTP/1.1 401 Unauthorized
Allow: GET, POST, HEAD, OPTIONS
Content-Length: 58
Content-Type: application/json
Date: Sat, 21 Sep 2019 14:06:15 GMT
Server: WSGIServer/0.2 CPython/3.6.0
Vary: Accept
WWW-Authenticate: Token
X-Frame-Options: SAMEORIGIN

{
    "detail": "Authentication credentials were not provided."
}

不带token会提示没权限访问:401 Unauthorized。

接口测试:

先使用接口测试工具测试下,用postman,或者fiddler都可以,我这里为了查看报文信息方便,用httpie命令行工具。

先访问接口A获取token值234af73571da46ade79ea6a74961b1d23d609b79:

D:\>http http://127.0.0.1:8000/api/v1/login/ username=test password=123456 -v

POST /api/v1/login/ HTTP/1.1
Accept: application/json, */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 42
Content-Type: application/json
Host: 127.0.0.1:8000
User-Agent: HTTPie/1.0.3

{
    "password": "123456",
    "username": "test"
}

HTTP/1.1 200 OK
Allow: POST, OPTIONS
Content-Length: 109
Content-Type: application/json
Date: Sat, 21 Sep 2019 15:37:06 GMT
Server: WSGIServer/0.2 CPython/3.6.0
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN

{
    "code": 0,
    "msg": "login success!",
    "token": "234af73571da46ade79ea6a74961b1d23d609b79",
    "username": "test"
}

传给下个接口B:

D:\>http http://127.0.0.1:8000/api/v1/userinfo/ Authorization:"Token b7e02c959fbae4c2a0d9094f6f9b9a35fa8aaa1e" -v

GET /api/v1/userinfo/ HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Authorization: Token b7e02c959fbae4c2a0d9094f6f9b9a35fa8aaa1e
Connection: keep-alive
Host: 127.0.0.1:8000
User-Agent: HTTPie/1.0.3


HTTP/1.1 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Length: 96
Content-Type: application/json
Date: Sat, 21 Sep 2019 16:04:25 GMT
Server: WSGIServer/0.2 CPython/3.6.0
Vary: Accept
X-Frame-Options: SAMEORIGIN

{
    "msg":"sucess!",
    "code":0,
    "data":[
        {
            "id":105,
            "name":"test",
            "sex":"F",
            "age":24,
            "mail":"[email protected]",
            "create_time":"2020-06-12"
        }]
}

传头部参数用xx:xxxx格式,中间用冒号:,如:User-Agent:demo-agent/1.0 ‘Cookie:a=b;b=c’,由于Authorization参数中间有空格,用双引号包起来。

extract提取token

提取登录接口返回的token值,使用extract提取器。

extract:
    - token: content.token

下个接口的用例引用token参数使用$token,完整的用例test_info.yml如下:

- config:
    name: logincase
    variables: {}

- test:
    name: login case1
    request:
        url: http://127.0.0.1:8000/api/v1/login/
        method: POST
        headers:
            Content-Type: application/json
            User-Agent: python-requests/2.18.4
        json:
            username: test
            password: 123456
    extract:
        - token: content.token   # 提取token
    validate:
        - eq: [status_code, 200]
        - eq: [headers.Content-Type, application/json]
        - eq: [content.msg, login success!]
        - eq: [content.code, 0]


- test:
    name: get user info case1
    request:
        url: http://127.0.0.1:8000/api/v1/userinfo/
        method: GET
        headers:
            Content-Type: application/json
            User-Agent: python-requests/2.18.4
            Authorization: Token $token   # 引用token
    validate:
        - eq: [status_code, 200]
        - eq: [headers.Content-Type, application/json]
        - eq: [content.data.0.age, 24]
        - eq: [content.data.0.name, test]
        - eq: [content.data.0.mail, xxx@qq.com]

运行用例:

hrun test_info.yml

查看report报告:

打开report目录下生成的报告文件。打开报告详情,可以看到token引用成功了。

你可能感兴趣的:(学习,python,压力测试,HttpRunner)