httprunner learn 4-variables and reference variable declaration

Foreword

In HttpRunner, the support variable declarations (variables) and references ($ var) mechanism. In the config and test the key variables can be defined by variables, then test step can be referenced by way of variable $ + variable name.
The difference is that, in the config defined global variables, the entire test (TestCase) can be referenced where all; test variable defined in the scope of the current test is limited to step (TestStep)

Local variables

In case login, account number and password are hard-coded, usually when writing use cases, this may become our best to write a single parameter variables. To achieve separation of test data and code for subsequent maintenance.
If we declare the variables in the test, the scope is only valid in the current test. Declare variables variables, the variables and the corresponding values of a key-value pair, such as

- test:
    name: login case1
    variables:
        user: test
        psw: 123456

Reference user and psw variables $user, $pswcomplete the following script test_var.yml

- config:
    name: logincase
    variables: {}
- test:
    name: login case1
    variables:
        user: test
        psw: 123456
    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: $user
            password: $psw
    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]

Local variables with the commencement of the test the current embodiment, no effect in other cases use test

Run a use case

Use hrun operating results

D:\soft\untitled>hrun test_var.yml
login case1
INFO     POST http://127.0.0.1:8000/api/v1/login/
INFO     status_code: 200, response_time(ms): 384.72 ms, response_length: 109 bytes
INFO     start to extract from response object.
INFO     start to validate.
.

----------------------------------------------------------------------
Ran 1 test in 0.394s

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

Global Variables

If you want to set a global variable, the need to declare a variable (variables) put under the config, so that the entire file into effect .yml

- config:
    name: logincase
    variables:
        user: test
        psw: 123456
- 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: $user
            password: $psw
    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]

operation result

D:\soft\untitled>hrun test_config_var.yml
login case1
INFO     POST http://127.0.0.1:8000/api/v1/login/
INFO     status_code: 200, response_time(ms): 580.17 ms, response_length: 109 bytes
INFO     start to extract from response object.
INFO     start to validate.
.

----------------------------------------------------------------------
Ran 1 test in 0.584s

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

Guess you like

Origin www.cnblogs.com/yoyoketang/p/11565908.html
Recommended