This is the 27th day of my participation in the August Genwen Challenge.More challenges in August

What is the extract

Extract is used to extract and reference data.

Usage scenarios

Typically, testing frameworks have mechanisms for getting data and storing it in variables. Httprunner is no exception; extract is used to extract variables for use in subsequent steps.

In interface testing, most interfaces require authentication, and it is impossible to repeat token acquisition in every test step, or even in test cases. Usually, we solve this problem with decorators, method calls, variable references, etc.

In Httprunner, we can extract the token once at the beginning of the use case and store it in a variable so that subsequent apis and steps can use the variable directly. Of course we can also use debugTalk implementation.

This article mainly introduces extract extract data and references.

In case the extract

There are two steps in this case. Assuming that our step1 is to obtain the token and save it to a variable, Step 2 will carry the token to access the interface.

case

config:
    name: "demo testcase"
    variables:
        device_sn: "ABC"
        username: ${ENV(USERNAME)}
        password: ${ENV(PASSWORD)}
    base_url: "https://getman.cn/mock"

teststeps:
-
    name: demo step 1
    api: api/demo_api.yml
    variables:
        user_agent: 'the Mozilla / 5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'
        device_sn: $device_sn
    extract:
        - token: content.name
    validate:
        - eq: ["status_code".200]
    setup_hooks:
        - ${hook_print(setup)}
    teardown_hooks:
        - ${hook_print(teardown)}
-
    name: demo step 2
    api: api/demo_api2.yml
    variables:
        token: $token
    validate:
        - eq: [reason.OK]
Copy the code

Content and status_code in the above code are built-in keywords that we can use directly, and you’ll notice that they have the same name as the keywords in Requests.

demo_api2.yml

The headers in demo_API2 carries the Token variable, and the content of the Token is the Token saved by extract

name: demo api 2
variables:
    var1: Chrome1.2
    var2: value2
request:
    url: /
    method: GET
    headers:
        User-Agent: $var1
        Content-Type: "application/json"
        Token: $token
    json:
        key: $var2
validate:
    - eq: ["status_code".200]
Copy the code

In the test report

The response of step1

Step2 the headers

Above, we can see that the extract token value from Step1 has been successfully saved and passed to the headers of Step2.

Thank you for reading, don’t forget to follow, like, comment, forward four consecutive yo!