HttpRunner helper function debugtalk.py

Auxiliary function debugtalk.py

  In the Httprunner framework, yaml or json files are used to describe use cases, and some complex operations cannot be performed, such as saving some data and calling across files, or implementing some complex logic judgments. In order to solve this problem, the debugtalk.py auxiliary function is introduced to perform some Complicated operations.

Steps:

1. Create a new debugtalk.py in the root directory of the project, and write the logic code function you want to implement in this py file, as shown in the figure below:

2. Use ${function name ()} to reference the corresponding function in yaml or json, as shown in the figure below:

# 调用 debugtalk.py文件中的函数
- config:
    name: 百度主页
    base_url: https://www.baidu.com
    output:
      - title

- test:
    name: 百度搜索
    request:
      url: /s
      method: GET
      headers:
        Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
        Accept-Encoding: gzip, deflate, br
        Accept-Language: zh-CN,zh;q=0.9
        User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36
      params:
        wd: ${get_value()}
    extract:
      - title: <title>(.+?)</title>
    validate:
      - eq: [$title,"猫_百度搜索"]

hook mechanism

  The hook mechanism in the Httprunner framework is equivalent to the setup and teardown functions in the unittest framework, which are used to initialize the environment before the test case is executed and clean up the environment after the test case is executed.

  The hooks mechanism in httprunner can be used at the test case level or at the test step level. The keywords are: setup_hooks, teardown_hooks

Steps for usage:

1. Generally write the corresponding initialization and environment cleaning functions in debugtalk.py

2. Add setup_hooks and teardown_hooks as needed in config and test in the test case file

# hook 是完成初始化和清理工作的
- config:
    name: 百度主页
    base_url: https://www.baidu.com
    output:
      - title
    # 放到用例层级
    setup_hooks:
      - ${setup_case()}
    teardown_hooks:
      - ${teardown_case()}

- test:
    name: 百度搜索
    # 放到步骤层级
    setup_hooks:
      - ${setup_start(我是传入的参数)}
    teardown_hooks:
      - ${teardown_close(我是传入的参数)}
    request:
      url: /s
      method: GET
      headers:
        Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
        Accept-Encoding: gzip, deflate, br
        Accept-Language: zh-CN,zh;q=0.9
        User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36
      params:
        wd: ${get_value()}
    extract:
      - title: <title>(.+?)</title>

    validate:
      - eq: [$title,"猫_百度搜索"]

Ignore the skip case:

  The Httprunner framework uses a method similar to unittest to ignore and skip use cases. There are 3 keywords:

skip: unconditionally skip use cases

skipIf: The condition is established, and the return value is true when it is true (non-0, non-empty, etc. are true)

kipUnless: If the condition is not true, it is true when the return value is False (0, empty, etc. are false)

The above keywords can only be used in the test test step

# skip是用来忽略跳过测试用例
- config:
    name: 百度主页
    base_url: https://www.baidu.com
    output:
      - title

- test:
    name: 百度搜索
    # 忽略跳过用例只能在测试步骤中使用
    skip: 无条件跳过
#    skipIf: True         # 条件为 True 时跳过
#    skipUnless: False    # 条件为 False 时跳过
    request:
      url: /s
      method: GET
      headers:
        Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
        Accept-Encoding: gzip, deflate, br
        Accept-Language: zh-CN,zh;q=0.9
        User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36
      params:
        wd: 猫
    extract:
      - title: <title>(.+?)</title>

    validate:
      - eq: [$title,"猫_百度搜索"]

Practical case

Optical theory is useless, you have to learn to follow along, and you have to do it yourself, so that you can apply what you have learned to practice. At this time, you can learn from some actual combat cases.

If it is helpful to you, please like and collect it to give the author an encouragement. It is also convenient for you to quickly find it next time.

If you don’t understand, please consult the small card below. The blogger also hopes to learn and progress with like-minded testers

At the right age, choose the right position, and try to give full play to your own advantages.

My road of automated test development is inseparable from the plan of each stage along the way, because I like planning and summarizing,

Test and develop video tutorials, study notes and receive portals! ! !

Guess you like

Origin blog.csdn.net/Liuyanan990830/article/details/129915873