首页 > 解决方案 > 如何自动化一个 API,它采用不同的查询参数组合并给出相应的结果(使用空手道)

问题描述

我有一个通用的 get API,它接受多个查询参数并为每个查询参数组合提供相应的响应。组合的集合被定义。我需要自动化 API,以便涵盖每个案例并使用空手道验证响应。我希望使用最少数量的功能文件。请提出一种解决方法。

标签: karate

解决方案


您真的应该阅读文档并查看演示并节省大量时间。

请注意,您可以params为此用例使用关键字,并且null值将被忽略(不会设置查询参数)。

Scenario Outline:
    * def query = { name: <name>, country: <country>, active: <active>, limit: <limit> }

    Given path 'search'
    And params query
    When method get
    Then status 200
    # response should NOT contain a key expected to be missing
    And match response !contains <missing>

    # observe how strings are enclosed in quotes, and we set null-s here below
    # and you can get creative by stuffing json into table cells !
    Examples:
    | name   | country   | active | limit | missing                                                      |
    | 'foo'  | 'IN'      | true   |     1 | {}                                                           |
    | 'bar'  | null      | null   |     5 | { country: '#notnull', active: '#notnull' }                  |
    | 'baz'  | 'JP'      | null   |  null | { active: '#notnull', limit: '#notnull' }                    |
    | null   | 'US'      | null   |     3 | { name: '#notnull', active: '#notnull' }                     |
    | null   | null      | false  |  null | { name: '#notnull', country: '#notnull', limit: '#notnull' } |

推荐阅读