Airborne使用教程

1.安装环境

前提条件:系统已安装Ruby

打开终端输入如下命令

bash 复制代码
gem install airborne

或者在Gemfile添加

ruby 复制代码
gem 'airborne'

然后运行bundle install

2.编写脚本

在项目中新建api_tests_spec.rb文件

以GET接口"https://www.thunderclient.com/welcome"为例编写测试用例

ruby 复制代码
require 'airborne'
Airborne.configure do |config|
  config.base_url = 'https://www.thunderclient.com'
end
describe 'GET/welcome' do
  it 'Verify Response' do
    get '/welcome'
    expect_status(200)
    expect_json('supports.graphql', true)
  end
end

3.运行测试

或者在终端输入

bash 复制代码
rspec api_tests_spec.rb

4.更多用法

常用断言方法

Airborne 提供了丰富的断言方法,可以快速验证 API 响应内容:

  • expect_status(status_code): 验证 HTTP 响应状态码。

  • expect_json_types(path, key: type): 验证 JSON 的键值类型,例如 :string, :int, :array, :bool。

  • expect_json(path, key: value): 验证 JSON 的键值内容。

  • expect_header(key, value): 验证响应头的值。

  • expect_body_contains(content): 验证响应体中是否包含某个字符串。

高级用法

配置认证token

如果你的 API 需要认证,可以通过配置 Headers:

ruby 复制代码
Airborne.configure do |config|
  config.headers = { 'Authorization' => 'Bearer your_token' }
end

使用动态变量

你可以将 API 响应结果存储到变量中,以便在后续测试中使用:

ruby 复制代码
describe 'Dynamic testing' do
  it 'uses the created resource' do
    post '/users', { name: 'Alice' }
    user_id = json_body[:id]
    
    get "/users/#{user_id}"
    expect_json(name: 'Alice')
  end
end

更多操作可以参考官方文档:https://github.com/brooklynDev/airborne

相关推荐
AIZHINAN3 天前
如何使用 JMeter 进行接口测试。
jmeter·接口测试
cxyll12343 天前
postman 用于接口测试,举例
开发语言·lua·接口测试·postman
测试界清流6 天前
接口测试:如何定位BUG的产生原因
接口测试
测试界清流8 天前
postman常用快捷键
接口测试
测试老哥9 天前
如何用Postman做接口测试?
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
测试老哥15 天前
Jmeter自动化性能测试常见问题汇总
自动化测试·软件测试·测试工具·jmeter·接口测试·压力测试·性能测试
天才测试猿22 天前
接口自动化测试难点:数据库验证解决方案
自动化测试·软件测试·数据库·python·测试工具·职场和发展·接口测试
天才测试猿1 个月前
Jmeter+ant+jenkins接口自动化测试框架
自动化测试·软件测试·python·jmeter·职场和发展·jenkins·接口测试