Httprunner的基本使用

Posted wenm1128

tags:

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

 

 

断言

- config:
    name: testcase description
    variables: {}

- test:
    name: /api/get-token
    request:
        headers:
            Content-Type: application/json
            User-Agent: python-requests/2.18.4
            app_version: 2.8.6
            device_sn: FwgRiO7CNA50DSU
            os_platform: ios
        json:
            sign: 9c0c7e51c91ae963c833a4ccbab8d683c4a90c98
        method: POST
        url: http://127.0.0.1:5000/api/get-token
    validate:
        - eq: [status_code, 200]
        - eq: [headers.Content-Type, application/json]
        - eq: [content.success, true]

- test:
    name: /api/users/1000
    request:
        headers:
            Content-Type: application/json
            User-Agent: python-requests/2.18.4
            device_sn: FwgRiO7CNA50DSU
            token: baNLX1zhFYP11Seb
        json:
            name: user1
            password: 123456
        method: POST
        url: http://127.0.0.1:5000/api/users/1000
    validate:
        - eq: [status_code, 201]
        - eq: [headers.Content-Type, application/json]
        - eq: [content.success, true]
        - eq: [content.msg, user created successfully.]

 

 

 

 参数关联

- config:
    name: testcase description
    variables: {}

- test:
    name: /api/get-token
    request:
        headers:
            Content-Type: application/json
            User-Agent: python-requests/2.18.4
            app_version: 2.8.6
            device_sn: FwgRiO7CNA50DSU
            os_platform: ios
        json:
            sign: 9c0c7e51c91ae963c833a4ccbab8d683c4a90c98
        method: POST
        url: http://127.0.0.1:5000/api/get-token
    extract:
        token: content.token
    validate:
        - eq: [status_code, 200]
        - eq: [headers.Content-Type, application/json]
        - eq: [content.success, true]

- test:
    name: /api/users/1000
    request:
        headers:
            Content-Type: application/json
            User-Agent: python-requests/2.18.4
            device_sn: FwgRiO7CNA50DSU
            token: $token
        json:
            name: user1
            password: 123456
        method: POST
        url: http://127.0.0.1:5000/api/users/1000
    validate:
        - eq: [status_code, 201]
        - eq: [headers.Content-Type, application/json]
        - eq: [content.success, true]
        - eq: [content.msg, user created successfully.]

 

 

 

抽离公用的base_url

每个测试步骤的 URL 中,都采用的是完整的描述(host+path),但大多数情况下同一个用例中的 host 都是相同的,区别仅在于 path 部分。

因此,我们可以将各个测试步骤(test) URL 的 base_url 抽取出来,放到全局配置模块(config)中,在测试步骤中的 URL 只保留 PATH 部分。

- config:
    name: testcase description
    base_url: http://127.0.0.1:5000
    variables: {}

- test:
    name: /api/get-token
    request:
        headers:
            Content-Type: application/json
            User-Agent: python-requests/2.18.4
            app_version: 2.8.6
            device_sn: FwgRiO7CNA50DSU
            os_platform: ios
        json:
            sign: 9c0c7e51c91ae963c833a4ccbab8d683c4a90c98
        method: POST
        url: /api/get-token
    extract:
        token: content.token
    validate:
        - eq: [status_code, 200]
        - eq: [headers.Content-Type, application/json]
        - eq: [content.success, true]

- test:
    name: /api/users/1000
    request:
        headers:
            Content-Type: application/json
            User-Agent: python-requests/2.18.4
            device_sn: FwgRiO7CNA50DSU
            token: $token
        json:
            name: user1
            password: 123456
        method: POST
        url: /api/users/1000
    validate:
        - eq: [status_code, 201]
        - eq: [headers.Content-Type, application/json]
        - eq: [content.success, true]
        - eq: [content.msg, user created successfully.]

 

 

 

变量的申明和引用

- config:
    name: testcase description
    base_url: http://127.0.0.1:5000
    variables: {}

- test:
    name: /api/get-token
    variables:
        app_version: 2.8.6
        device_sn: FwgRiO7CNA50DSU
        os_platform: ios
    request:
        headers:
            Content-Type: application/json
            User-Agent: python-requests/2.18.4
            app_version: $app_version
            device_sn: $device_sn
            os_platform: $os_platform
        json:
            sign: 9c0c7e51c91ae963c833a4ccbab8d683c4a90c98
        method: POST
        url: /api/get-token
    extract:
        token: content.token
    validate:
        - eq: [status_code, 200]
        - eq: [headers.Content-Type, application/json]
        - eq: [content.success, true]

- test:
    name: /api/users/$user_id
    variables:
        device_sn: FwgRiO7CNA50DSU
        user_id: 1000
    request:
        headers:
            Content-Type: application/json
            User-Agent: python-requests/2.18.4
            device_sn: $device_sn
            token: $token
        json:
            name: user1
            password: 123456
        method: POST
        url: /api/users/$user_id
    validate:
        - eq: [status_code, 201]
        - eq: [headers.Content-Type, application/json]
        - eq: [content.success, true]
        - eq: [content.msg, user created successfully.]

 

 

 

抽取公共变量

- config:
    name: testcase description
    base_url: http://127.0.0.1:5000
    variables:
        device_sn: FwgRiO7CNA50DSU

- test:
    name: /api/get-token
    variables:
        app_version: 2.8.6
        os_platform: ios
    request:
        headers:
            Content-Type: application/json
            User-Agent: python-requests/2.18.4
            app_version: $app_version
            device_sn: $device_sn
            os_platform: $os_platform
        json:
            sign: 9c0c7e51c91ae963c833a4ccbab8d683c4a90c98
        method: POST
        url: /api/get-token
    extract:
        token: content.token
    validate:
        - eq: [status_code, 200]
        - eq: [headers.Content-Type, application/json]
        - eq: [content.success, true]

- test:
    name: /api/users/$user_id
    variables:
        user_id: 1000
    request:
        headers:
            Content-Type: application/json
            User-Agent: python-requests/2.18.4
            device_sn: $device_sn
            token: $token
        json:
            name: user1
            password: 123456
        method: POST
        url: /api/users/$user_id
    validate:
        - eq: [status_code, 201]
        - eq: [headers.Content-Type, application/json]
        - eq: [content.success, true]
        - eq: [content.msg, user created successfully.]

 

 

 

 

 

 

 

 

 

 

 

1

以上是关于Httprunner的基本使用的主要内容,如果未能解决你的问题,请参考以下文章

httprunner基本使用01简介安装发get/post请求

httpRunner 使用方式与代码编写样式

httpRunner 使用方式与代码编写样式

HttpRunner基础使用一:

03-Httprunner-JMESPath提取返回结果

HttpRunner使用总结