一盏小灯 自动化测试框架 karate 试用(1)- API 测试

Jerry li · 2023年08月05日 · 3169 次阅读

前言

最近公司其他部门的团队正在 share 用 karate 做的接口测试自动化。虽然实现的效果其实和我们用的 pytest 大差不差,重点是框架和语言不一样,但还是抽时间先研究一下。这是第一篇。

karate 简介

以下是 karate 的官网,感兴趣的可以自行了解,这里就不逐一搬运了。https://karatelabs.github.io/karate/


重点如下:

  • 基于 java 进行运行和扩展。
  • 支持使用 cucumber 的 feature 直接编写测试用例,也就是可以直接用 given,when,then 的 BDD 格式编写用例并且执行。
  • 本次试用 maven 来安装。
  • 本次使用 idea 作为 IDE。
  • 在 IDE 中安装 karate 插件。

安装过程这里就忽略了,安装官网的教程很容易。

API demo

为了便于测试想要试用的几个场景,首先用自己熟悉的 python flask api 框架来编写一个小 demo: https://github.com/jerrylizilong/flask_api_demo
支持一下接口:

/users
- get: get all users
- post: insert new user

/user
- get: get user detail by user id.
- put: update user information 
- delete: delete user

使用 karate 进行测试。

具体测试代码如下: https://github.com/jerrylizilong/jerry_karate_demo

测试场景:
users.feature

Feature: sample karate test script
  for help, see: https://github.com/karatelabs/karate/wiki/IDE-Support

  Background:
    * url 'http://127.0.0.1:5000'

  Scenario: get all users and then get the first user by id
    Given path 'users'
    When method get
    Then status 200

    * def first = response[0]

    Given path 'user', first.userid
    When method get
    Then status 200
    And match response == first

  Scenario: get all users and then update the first user by id
    Given path 'users'
    When method get
    Then status 200

    * def first = response[0]
    * def newuser = first
    * newuser.age = '35'

    Given path 'user', first.userid
    When request newuser
    And method put
    Then status 201
    And match response == newuser

    Given path 'user', first.userid
    When method get
    Then status 200
    And match response == newuser

  Scenario: create a user and then get it by id

    * def user =
      """
        {
          'username': 'Yao Ming',
          'age': '31',
          'team': 'Houston Rockets',
          'position': 'Center'
        }
      """

    Given path 'users'
    And request user
    When method post
    Then status 201


    * def id = response.userid
    * print 'created id is: ', id
    * def expected_user = user
    * expected_user.user_id = id

    Given path 'user', id
    When method get
    Then status 200
    And response.user = expected_user

  Scenario: get all users and then delete the last user by id
    Given path 'users'
    When method get
    Then status 200

    * def size = karate.sizeOf(response)
    * def last = response[size-1]

    Given path 'user', last.userid
    And method delete
    Then status 204

    Given path 'user', last.userid
    When method get
    Then status 404
    And match response.message == "user "+ last.userid +" doesn't exist"

测试报告

总结

可以看到 karate 对于新手是挺友好的,基本上可以做到开箱即用。
比如如果做 UI 自动化,以下是一个简单的例子。
PS: API 测试和 UI 测试是可以用同一个 maven 依赖的,而且 UI 测试可以不需要下载对应的 driver,直接调用浏览器执行(当然也可以指定对应的 driver ),这个比 selenium 感觉还要方便呀!

Feature: browser automation 1

  Background:
    * configure driver = { type: 'chrome', showDriverLog: true }
  # * configure driverTarget = { docker: 'justinribeiro/chrome-headless', showDriverLog: true }
  # * configure driverTarget = { docker: 'ptrthomas/karate-chrome', showDriverLog: true }
  # * configure driver = { type: 'chromedriver', showDriverLog: true }
  # * configure driver = { type: 'geckodriver', showDriverLog: true }
  # * configure driver = { type: 'safaridriver', showDriverLog: true }
  # * configure driver = { type: 'iedriver', showDriverLog: true, httpConfig: { readTimeout: 120000 } }

  Scenario: try to login to github
  and then do a google search

    Given driver 'https://github.com/login'
    And input('#login_field', 'dummy')
    And input('#password', 'world')
    When submit().click("input[name=commit]")
    Then match html('.flash-error') contains 'Incorrect username or password.'

    Given driver 'https://google.com'
    And input("textarea[name=q]", 'karate dsl')
    When submit().click("input[name=btnI]")
    Then waitForUrl('https://github.com/karatelabs/karate')
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册