httprunner learning 7-extract extract content objects returned

Foreword

Extract object data response returned by extract keywords. In front of about token value by content.tokenvalue.
How Benpian explain in detail the various types of data you want to extract the data returned from json

content objects

httprunner inside contentis actually inside the request r.content, returns a byte type.

Json returned data such as

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

Json above data, which can be converted into the corresponding type dict python, extraction Extract

  • content.code later takes a value corresponding to code 0
  • content.msg corresponding value taken later msg "login success!"
  • content.username back extracted username value "test"
  • content.token back extracted token value "b3f7e8e12d23591ea671374dee818c63b1599d4d"

list the type of value

If the data returned json converted into python inside the object list, the following structure

[{
        "age": 20,
        "create_time": "2019-09-15",
        "id": 1,
        "mail": "[email protected]",
        "name": "yoyo",
        "sex": "M"
    },
    {
        "age": 21,
        "create_time": "2019-09-16",
        "id": 2,
        "mail": "[email protected]",
        "name": "yoyo111",
        "sex": "M"
    }
]

This is converted into the above list of objects inside the python, the subject can be taken out by the corresponding data content.int method, extraction method corresponding Extract

  • content.0 取出list里面的第一组数据{"age": 20, "create_time": "2019-09-15", "id": 1, "mail": "[email protected]", "name": "yoyo", "sex": "M"}
  • content.1 取出list里面的第二组数据{"age": 21, "create_time": "2019-09-16", "id": 2, "mail": "[email protected]", "name": "yoyo111", "sex": "M"}
  • content.0.name extracted first set of data values ​​corresponding to the name "yoyo"
  • content.1.name removed name corresponding to the second set of data values ​​"yoyo111"
  • content.0.mail mail extracted first set of data corresponding to a value "[email protected]"

May be removed by a method content.int value string inside, for example I to remove a first set of data, name corresponding to the value "yoyo" inside the third character, can be used content.0.name.2, this take the string rarely used

Practice cases

Overall, the value of content objects on 2 ways: content.keyand content.int, according to the layer hierarchy to find it

There is a demo case, in response to a request result of the use of the following httpie

C:\Users\dell>http http://127.0.0.1:8000/api/test/demo
HTTP/1.1 200 OK
Content-Length: 255
Content-Type: application/json
Date: Sun, 22 Sep 2019 10:11:07 GMT
Server: WSGIServer/0.2 CPython/3.6.0
X-Frame-Options: SAMEORIGIN

{
    "code": 0,
    "datas": [
        {
            "age": 20,
            "create_time": "2019-09-15",
            "id": 1,
            "mail": "[email protected]",
            "name": "yoyo",
            "sex": "M"
        },
        {
            "age": 21,
            "create_time": "2019-09-16",
            "id": 2,
            "mail": "[email protected]",
            "name": "yoyo111",
            "sex": "M"
        }
    ],
    "msg": "success!"
}

Objective: In addition to taking the value of the data inside the datas mail corresponding to a set of data from the result of the response [email protected] inside and asserted.

test_demo.yml script content

# 上海悠悠,QQ交流群:750815713
- config:
    name: test_demo
    variables: {}
- test:
    name: test_demo case1
    request:
        url: http://127.0.0.1:8000/api/test/demo
        method: GET
        headers:
            Content-Type: application/json
            User-Agent: python-requests/2.18.4
        json:
            username: test
            password: 123456
    extract:
        - mail: content.datas.0.mail         # 提取mail
    validate:
        - eq: [status_code, 200]
        - eq: [content.msg, success!]
        - eq: [$mail, [email protected]]

operation result

D:\soft\untitled>hrun test_demo.yml
test_demo case1
INFO     GET http://127.0.0.1:8000/api/test/demo
INFO     status_code: 200, response_time(ms): 4.99 ms, response_length: 255 bytes
INFO     start to extract from response object.
INFO     start to validate.
.

----------------------------------------------------------------------
Ran 1 test in 0.012s

OK
INFO     Start to render Html report ...
INFO     Generated Html report: D:\soft\untitled\reports\1569147460.html

Running success!

Guess you like

Origin www.cnblogs.com/yoyoketang/p/11568565.html