httprunner parametric study 6- (reference external data csv)

Foreword

Previous parameterization has been achieved, but the data is on file .yml inside, when a lot of test data, we hope to test data is written to a csv file.

Independent parameters

For existing parameter list, and the large amount of data, the more suitable manner the parameter value list is stored in the CSV data file.

For the CSV data file, you need to follow a few rules agreed as follows:

  • File should be placed in the same directory as the test case file;
  • The first row of the CSV file must be the name of the parameter, the second parameter value is a start line, each (set) value of a line;
  • If a plurality of parameters having the same CSV file, parameter names and values ​​Spacer Needed commas.

For example, user_id parameter ranges from 1001 to 2000, then we can create user_id.csv, and described as follows in the form of a file.

user_id
1001
1002
...
1999
2000

Then YAML / JSON test file, it can be built to Parameterize (may be abbreviated as P) function references a CSV file.

- config:
    parameters:
        - user_id: ${parameterize(user_id.csv)}
        - user_id: ${P(user_id.csv)}    # 简写方式

csv file to store user data

To prepare test data, prepare four sets of login with account and password, the account is test1, test2, test3, test4, a unified password is set to 123456.

user_name.csv test data file

user
test1
test2
test3
test4

user_name.csv test_param_csv.yml files and files into the same directory, references csv file syntax${P(user_name.csv)}

# 上海悠悠,QQ交流群:750815713
- config:
    name: logincase
    parameters:
        - user: ${P(user_name.csv)}    # 参数化
    variables:
        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]

csv store user data and psw

user_psw.csv test data file

user,psw
test1,123456
test2,123456
test3,123456
test4,123456

test_param_csv_psw.yml file and user_psw.csv test data in the same directory

# 上海悠悠,QQ交流群:750815713
- config:
    name: logincase
    parameters:
        - user-psw: ${P(user_psw.csv)} # 参数化
- 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

cmd executionhrun test_param_csv_psw.yml

D:\soft\untitled>hrun test_param_csv_psw.yml
login case1
INFO     POST http://127.0.0.1:8000/api/v1/login/
INFO     status_code: 200, response_time(ms): 529.26 ms, response_length: 110 bytes
INFO     start to extract from response object.
INFO     start to validate.
.
login case1
INFO     POST http://127.0.0.1:8000/api/v1/login/
INFO     status_code: 200, response_time(ms): 428.52 ms, response_length: 110 bytes
INFO     start to extract from response object.
INFO     start to validate.
.
login case1
INFO     POST http://127.0.0.1:8000/api/v1/login/
INFO     status_code: 200, response_time(ms): 438.94 ms, response_length: 110 bytes
INFO     start to extract from response object.
INFO     start to validate.
.
login case1
INFO     POST http://127.0.0.1:8000/api/v1/login/
INFO     status_code: 200, response_time(ms): 350.67 ms, response_length: 110 bytes
INFO     start to extract from response object.
INFO     start to validate.
.

----------------------------------------------------------------------
Ran 4 tests in 1.766s

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

V1.x versions of the test data csv file can only be a test case files in the same directory, V2.x version supports write path to the file path for the project root directory (the path debugtalk.py) based on the relative path. Such as: data / user_id.csv

Guess you like

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