IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

HttpRunner学习6–使用parameters参数化

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

前言

在使用HttpRunner测试过程中,我们可能会遇到这种场景:

账号登录功能,需要输入用户名和密码,设计测试用例后有 N 种组合情况

如果测试组合比较少,比如只有2个,那我们直接在YAML脚本中写2个 test 就行了,但如果测试组合有10个,或者上百个,我们总不可能写10个,甚至100个 test 吧?

因此,在这里我们就需要用到参数化。在HttpRunner中,通过关键字 parameters 来实现参数化数据驱动机制,不管有多少测试组合,我们只需写1个 test 就行了。

本人环境:HttpRunner V1.5.8

测试场景

登录接口文档说明:http://doc.nnzhp.cn/index.php?s=/6&page_id=12

我们继续以之前的登录接口来学习,如果不使用 parameters 来进行参数化,那么我们的用例可能就是这样的:

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

在上面的用例中,我们可能要写很多的 test ,在这些 test 中,除了 request-data 中 username 和 passwd 的值不一样,其他内容都完全一样。

当我们使用 parameters 来实现参数化后,我们只要写 1个test 就行了,避免了重复的脚本代码,同时脚本内容也变得更加简洁。

parameters参数化–一对一

我目前的 HttpRunner 版本是 1.5.8 ,关键字 parameters 可以在 config 或 test 中使用,我们只以在 config 中使用 parameters 来进行说明。

注意:在 HttpRunner 1.5.11 之后的版本中,不再支持在 test 中使用 parameters 。

我们准备 3 组登录用的数据,其中,第1组和第3组是能正常登录的用户名和密码,第2组是错误的用户名和密码。使用 parameters 如下:

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

然后,只保留一个 test 即可,并在 request-data 中引入参数,如下:

      data:
        username: $username
        passwd: $passwd

完整的YAML用例:

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

运行用例,然后即可得到测试报告,如下:

101_1.png

parameters参数化–多对一

上面介绍的是用户名和密码一对一的情况,如果是 多对一 或 一对多 关系,比如 3 组登录用的数据中,用户名不一样,但密码都是相同的,这个时候,如果用上面的写法,就不太方便了。

对于这种情况,我们可以这样写:

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

只需改动 config 中的 parameters ,其他内容都不需要修改。运行用例后的报告如下:

101_2.png

parameters参数化–多对多

有时候,我们可能存在多对多的测试场景,即笛卡尔积的组合情况。如果有 3 个不同的用户名,2 个不同的密码,对于这种情况,使用笛卡尔积组合,就应该存在 2 * 3 = 6 种测试组合。

对于这种情况,我们可以这样写:

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

只需改动 config 中的 parameters ,其他内容都不需要修改。运行用例后的报告如下:

101_3.png

文章永久链接:https://tech.souyunku.com/?p=32939


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(69) 打赏



未经允许不得转载:搜云库技术团队 » HttpRunner学习6–使用parameters参数化

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367