Popular in the whole network, HttpRunner automated testing framework - CSV file data (detailed summary)


foreword

When the amount of data is relatively large, we hope that the test data can be written into a CSV file, and then directly reference the CSV file data.

testing scenarios

If the parameter list content is specified directly in the YAML/JSON script, our use case might look like this:

- config:
    name: parameters test
    request:
      base_url: http://api.nnzhp.cn
    parameters:
      - username-passwd:
          - ["niuhanyang", "aA123456"]
          - ["test10101", "fasdfasfa"]
          - ["test1010", "aA123456"]

- test:
    name: login case
    request:
      url: /api/user/login
      method: POST
      headers:
        Content-Type: application/x-www-form-urlencoded
        User-Agent: Fiddler
      data:
        username: $username
        passwd: $passwd
    validate:
      - eq: [status_code, 200]
      - eq: [content.error_code, 0]

In the above use case, there are only 3 sets of test data. When we have 10 or 100 sets of test data, it may be inconvenient to write directly in the test script.

We can write all these test data to CSV files, and then directly reference the CSV files, which is much more convenient and our scripts are more concise.

Store a single parameter in CSV

For the CSV data file, the following agreed rules need to be followed:
the file must be placed in the same directory as the test case file;
the first line in the CSV file must be the parameter name, and the second line must be the parameter value, each (group) values ​​occupy one line;

If there are multiple parameters in the same CSV file, the separator between the parameter name and the value must be a comma.

For example, if we have multiple sets of login data, the usernames are different, but the passwords are the same, we can create username.csv, and write the parameter of username to username.csv, as follows:

username
niuhanyang
test10101
test1010

Then in the YAML/JSON test case, you can refer to the CSV file through the built-in parameterize (abbreviated as P) function of HttpRunner.

parameters:
  - username: ${
    
    P(username.csv)} # 或者 ${
    
    parameterize(username.csv)}
  - passwd: ["aA123456"]

Note: In HttpRunner, the parameter name specified in parameters must be consistent with the parameter name in the first line of the CSV file.

Store multiple parameters in CSV

For multiple parameters with correlation, for example, we have multiple sets of login data, and the username and password are different, then we can create user.csv and write the parameters of username and password to user.csv.

as follows:

username,passwd
niuhanyang,aA123456
test10101,fasdfasfa
test1010,aA123456

Modify the YAML script again:

parameters:
  - username-passwd: ${
    
    P(user.csv)} # 或者 ${parameterize(user.csv)}

Note: The above writing method is a one-to-one relationship between username and password. If it is written as follows, it will be a Cartesian product combination, resulting in 3 * 3 = 9 combinations.

parameters:
  - username: ${
    
    P(user.csv)}
  - passwd: ${
    
    P(user.csv)}

In addition, when specifying parameters in the test script, only some parameters can be used, and the order of the parameters does not need to be consistent with the order of the parameter names in the CSV file.

Run the use case

Complete YAML format use case (test_csv.yml):

- config:
    name: parameters test
    request:
      base_url: http://api.nnzhp.cn
    parameters:
      - username-passwd: ${
    
    P(user.csv)} # 或者 ${parameterize(user.csv)}

- test:
    name: login case
    request:
      url: /api/user/login
      method: POST
      headers:
        Content-Type: application/x-www-form-urlencoded
        User-Agent: Fiddler
      data:
        username: $username
        passwd: $passwd
    validate:
      - eq: [status_code, 200]
      - eq: [content.error_code, 0]

In HttpRunner 1.x version, to quote CSV data files (such as user.csv), you need to put user.csv and test_csv.yml in the same directory, and then run the use case.

The report after running the use case is as follows:

C1

The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled

1. From entry to mastery of Python programming

Please add a picture description

2. Interface automation project actual combat

Please add a picture description

3. Actual Combat of Web Automation Project

Please add a picture description

4. Actual Combat of App Automation Project

Please add a picture description

5. Resume of first-tier manufacturers

Please add a picture description

6. Test and develop DevOps system

Please add a picture description

7. Commonly used automated testing tools

Please add a picture description

Eight, JMeter performance test

Please add a picture description

9. Summary (little surprise at the end)

Struggle is a journey inseparable from sweat, and every drop of sweat bears in mind the courage and perseverance of struggle. Let go of doubts, hold your chest up, and take firm steps. Only through wind and rain can you see a rainbow.

Take the dream as the course, and sail to the ideal shore with struggle. The road may be difficult, but every step is a mark of growth. Persistence and perseverance are the cornerstones of success, and perseverance is the password to realize dreams. Believe in yourself, go forward bravely, you will create infinite possibilities!

On the road of struggle, don't be afraid of challenges, because they are catalysts for growth. Use courage to dispel fear, use hard work to write brilliance, every effort is the driving force for the future.

Guess you like

Origin blog.csdn.net/x2waiwai/article/details/132209855