Cucumber API Steps 技术文档
2024-12-26 05:51:33作者:俞予舒Fleming
1. 安装指南
1.1 依赖项
在安装 cucumber-api-steps 之前,确保你的项目中已经安装了以下依赖项:
1.2 安装步骤
-
在你的
Gemfile中添加以下行,建议将其放在test或cucumber组中:gem 'cucumber-api-steps', :require => false -
在
env.rb文件中添加以下行,以便在你的特性文件中使用这些步骤定义:require 'cucumber/api_steps' -
运行
bundle install安装 gem。
2. 项目的使用说明
cucumber-api-steps 提供了一组 Cucumber 步骤定义,用于简化 REST 风格 API 的基本测试。它支持 XML 和 JSON 格式的请求和响应。
2.1 基本使用
你可以在 Cucumber 特性文件中使用这些步骤定义来测试 API。以下是一些常见的用例:
2.1.1 发送和接受 JSON 请求
Feature: API
Scenario: List tweets in JSON
When I send and accept JSON
And I send a GET request to "/api/tweets"
Then the response status should be "200"
And the JSON response should be:
"""
[{"tweet":"Hello World!"},{"tweet":"New Rails has been released"}]
"""
And the JSON response should have "$..tweet" with the text "Hello World!"
And the JSON response should have "$..tweet" with a length of 2
2.1.2 发送和接受 XML 请求
Feature: API
Scenario: List tweets in XML
When I send and accept XML
And I send a GET request to "/api/tweets"
Then the XML response should have "tweet" with the text "Hello World!"
2.1.3 使用 POST 参数发送请求
Feature: API
Scenario: Post tweet using POST-params
When I send a POST request to "/api/tweets" with the following:
| tweet | Hello World! |
| lat | 42.848282 |
| lng | 74.634933 |
Then the response status should be "201"
2.1.4 使用 JSON 请求体发送请求
Feature: API
Scenario: Post tweet using json in POST body
When I send a POST request to "/api/tweets" with the following:
"""
{"tweet":"Hello World!","lat":"42.848282", "lng":"74.634933"}
"""
Then the response status should be "201"
2.1.5 基本认证
Feature: API
Scenario: Basic authentication
When I authenticate as the user "joe" with the password "password123"
And I send a GET request to "/api/tweets"
Then the response status should be "200"
2.1.6 摘要认证
Feature: API
Scenario: Digest authentication
When I digest-authenticate as the user "joe" with the password "password123"
And I send a GET request to "/api/tweets"
Then the response status should be "200"
3. 项目 API 使用文档
cucumber-api-steps 提供了一系列步骤定义,用于测试 API。以下是一些常用的步骤:
3.1 发送请求
When I send and accept JSON:设置请求和响应的内容类型为 JSON。When I send and accept XML:设置请求和响应的内容类型为 XML。When I send a GET request to "/api/tweets":发送一个 GET 请求到指定的路径。When I send a POST request to "/api/tweets" with the following::发送一个 POST 请求到指定的路径,并附带参数或请求体。
3.2 验证响应
Then the response status should be "200":验证响应的状态码。Then the JSON response should be::验证 JSON 响应的内容。Then the JSON response should have "$..tweet" with the text "Hello World!":验证 JSON 响应中特定路径的值。Then the XML response should have "tweet" with the text "Hello World!":验证 XML 响应中特定节点的值。
3.3 认证
When I authenticate as the user "joe" with the password "password123":使用基本认证。When I digest-authenticate as the user "joe" with the password "password123":使用摘要认证。
4. 项目安装方式
4.1 通过 Gemfile 安装
在 Gemfile 中添加以下行:
gem 'cucumber-api-steps', :require => false
然后运行 bundle install 安装 gem。
4.2 手动安装
你也可以通过以下命令手动安装 gem:
gem install cucumber-api-steps
安装完成后,在 env.rb 文件中添加以下行:
require 'cucumber/api_steps'
这样,你就可以在你的 Cucumber 特性文件中使用 cucumber-api-steps 提供的步骤定义了。
结语
cucumber-api-steps 是一个强大的工具,可以帮助你轻松测试 REST 风格的 API。通过本文档,你应该能够顺利安装并使用这个 gem 来编写和执行 API 测试。如果你有任何问题或需要进一步的帮助,请参考项目的 GitHub 页面。
登录后查看全文
热门项目推荐
GLM-5.1GLM-5.1是智谱迄今最智能的旗舰模型,也是目前全球最强的开源模型。GLM-5.1大大提高了代码能力,在完成长程任务方面提升尤为显著。和此前分钟级交互的模型不同,它能够在一次任务中独立、持续工作超过8小时,期间自主规划、执行、自我进化,最终交付完整的工程级成果。Jinja00
MiniMax-M2.7MiniMax-M2.7 是我们首个深度参与自身进化过程的模型。M2.7 具备构建复杂智能体应用框架的能力,能够借助智能体团队、复杂技能以及动态工具搜索,完成高度精细的生产力任务。Python00- QQwen3.5-397B-A17BQwen3.5 实现了重大飞跃,整合了多模态学习、架构效率、强化学习规模以及全球可访问性等方面的突破性进展,旨在为开发者和企业赋予前所未有的能力与效率。Jinja00
HY-Embodied-0.5这是一套专为现实世界具身智能打造的基础模型。该系列模型采用创新的混合Transformer(Mixture-of-Transformers, MoT) 架构,通过潜在令牌实现模态特异性计算,显著提升了细粒度感知能力。Jinja00
LongCat-AudioDiT-1BLongCat-AudioDiT 是一款基于扩散模型的文本转语音(TTS)模型,代表了当前该领域的最高水平(SOTA),它直接在波形潜空间中进行操作。00
ERNIE-ImageERNIE-Image 是由百度 ERNIE-Image 团队开发的开源文本到图像生成模型。它基于单流扩散 Transformer(DiT)构建,并配备了轻量级的提示增强器,可将用户的简短输入扩展为更丰富的结构化描述。凭借仅 80 亿的 DiT 参数,它在开源文本到图像模型中达到了最先进的性能。该模型的设计不仅追求强大的视觉质量,还注重实际生成场景中的可控性,在这些场景中,准确的内容呈现与美观同等重要。特别是,ERNIE-Image 在复杂指令遵循、文本渲染和结构化图像生成方面表现出色,使其非常适合商业海报、漫画、多格布局以及其他需要兼具视觉质量和精确控制的内容创作任务。它还支持广泛的视觉风格,包括写实摄影、设计导向图像以及更多风格化的美学输出。Jinja00
项目优选
收起
暂无描述
Dockerfile
675
4.32 K
deepin linux kernel
C
28
16
Ascend Extension for PyTorch
Python
517
627
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
947
886
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
398
302
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.56 K
909
暂无简介
Dart
921
228
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
1.07 K
559
昇腾LLM分布式训练框架
Python
142
169
Oohos_react_native
React Native鸿蒙化仓库
C++
335
381