Popular in the whole network, HttpRunner automated testing framework-parameters parameterization (ultra-fine finishing)


foreword

In the process of using HttpRunner to test, you may encounter this kind of scenario:
account login function, you need to enter the user name and password, and there are N kinds of combinations after designing the test case

If there are few test combinations, for example, there are only 2, then we can write 2 tests directly in the YAML script, but if there are 10 or hundreds of test combinations, it is impossible for us to write 10 or even 100 tests Bar?

Therefore, here we need to use parameterization. In HttpRunner, the parameterized data-driven mechanism is implemented through the keyword parameters. No matter how many test combinations there are, we only need to write one test.

testing scenarios

If parameters were not used for parameterization, our use case might look like this:

- config:
    name: login test
    request:
      base_url: http://api.nnzhp.cn

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

# 省略很多个 test

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

In the above use case, we may have to write a lot of tests. In these tests, except for the values ​​of username and passwd in request-data, the other contents are exactly the same.

When we use parameters to achieve parameterization, we only need to write one test, avoiding repeated script code, and the script content becomes more concise.

parameters parameterization (one-to-one)

The keyword parameters can be used in config or test, we only use parameters in config for illustration.

Note: In versions after HttpRunner 1.5.11, the use of parameters in test is no longer supported.

We prepare 3 sets of data for login, among which, the first and third sets are user names and passwords that can be logged in normally, and the second set is wrong user names and passwords.
Use parameters as follows:

parameters:
  - username-passwd:
	  - ["niuhanyang", "aA123456"] # 正确的用户名密码
	  - ["test10101", "fasdfasfa"] # 错误的用户名密码
	  - ["test1010", "aA123456"] # 正确的用户名密码

Then, only keep one test, and introduce parameters in request-data, as follows:

data:
	username: $username
	passwd: $passwd

Full YAML use case:

- 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]

Run the use case, and then you can get the test report, as follows:

C1

parameters parameterization (many to one)

The above describes the one-to-one situation of user name and password. If it is a many-to-one or one-to-many relationship, for example, in the three sets of login data, the user names are different, but the passwords are the same. At this time, if you use The above writing method is not very convenient.

For this case, we can write:

parameters:
  - username: ["niuhanyang", "test10101", "test1010"]
  - passwd: ["aA123456"]

You only need to change the parameters in config, and no other content needs to be modified.
The report after running the use case is as follows:

C2

parameters parameterization (many to many)

Sometimes, we may have a many-to-many test scenario, that is, a combination of Cartesian products. If there are 3 different usernames, 2 different passwords, for this case, using Cartesian product combinations, there should be 2 * 3 = 6 test combinations.

For this case, we can write:

parameters:
  - username: ["niuhanyang", "test10101", "test1010"]
  - passwd: ["aA123456", "fasdfasfa"]

You only need to change the parameters in config, and no other content needs to be modified.
The report after running the use case is as follows:

C3

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)

Life may be rough and difficult, but don't bow your head and compromise. With courage and dreams in mind, fly against the wind and pursue brilliance. Don't lose confidence, move forward with determination, and the steps of struggle create infinite possibilities!

Go forward bravely and pursue infinite possibilities; defy hardships and set sail to break through the waves. Dream is the guide to success, and struggle is the way to the top. Persistence, hard work, every effort is a step closer to the dream!

No matter how tortuous and rugged the front is, challenges are opportunities for struggle. Persist in love, embrace change, and pursue excellence. Take every step bravely, water the flowers of your dreams with sweat, and bloom your own brilliant life!

Guess you like

Origin blog.csdn.net/x2waiwai/article/details/132231238
Recommended