蝙蝠-行为API测试器

A 类 盖克金 用于测试 HTTP API 的基于 DSL Cu. 联合来文。 。 。 .
- 用普通英语写作RESTFUL/HTTP API测试.
- 与 Open API 规格相融合 .
- 容易扩展与 Cu. 联合来文。 。 。 .
Given I am anonymous
When I send a 'GET' request to '{base}/pets'
And I add the query string parameters:
| sort | desc |
| filter | mammal |
Then I should receive a response with the status 200
And I should receive a response within 1000ms
And the response body json path at "$.[1].name" should equal "Rover"
见 步骤定义参考 全文
所有可用的列表 Given, (中文(简体) ), When 财务报告和审定财务报表 Then 步骤.
目录
安装
npm i --save-dev @harver/bat
开始吧
1. 安装木瓜
npm install --save-dev cucumber
2. 创建文件 features/support/setup.js 下列代码:
const {
setWorldConstructor,
After, AfterAll, Before, BeforeAll,
Given, When, Then
} = require('cucumber');
const { registerHooks, World, registerSteps } = require('@harver/bat');
setWorldConstructor(World);
// Allow Bat to hook into your Cucumber dependencty:
registerHooks({ Before, BeforeAll, After, AfterAll });
registerSteps({ Given, Then, When });
3. 编写特征文件和情景
features/some-scenario.feature数字 :
Scenario: Testing Gets
When I send a 'GET' request to '{base}/pets'
And I add the query string parameters:
| sort | desc |
| filter | red |
Then I should receive a response with the status 200
And I should receive a response within 1000ms
And the response body json path at "$.[1].name" should equal "Rover"
见 步骤 关于所有可用步骤的文件.
4. 使用 黄瓜. JS CLI 来运行您的规格
./node_modules/.bin/cucumber-js
提示
使用 Postman 兼容环境文件定义变量 :
ENV_FILE=env/uat.json cucumber-js
掩体文件将看起来像这样:
{
"values": [
{
"key": "base",
"value": "http://localhost:3000"
}
]
}
然后,你可以在步法中引用这些变量,比如:
When I send a 'GET' request to '{base}/pets'
与 Open API 3 规格合并 :
API_SPEC_FILE=test/openapi.yaml cucumber-js
开放 API 光谱可与提供的步骤结合使用, 例如 提取实例请求机构或验证对其的答复 计划.
步骤简表
步骤以可读的英文形式写出,但这可以看起来相当动词. 因此,大多数步骤都有另一种简短的形式。 例如:
Scenario: Testing short forms
When GET '/pets'
And qs:
| sort | desc |
| filter | mammal |
Then receive status 200
And within 1000ms
And json path at "$.[1].name" should equal "Rover"
添加缓冲缓冲
如果你正在使用 I should receive a response within {int}ms 步入您期望异常慢的网络连接,
您可以使用 LATENCY_BUFFER 环境变量 :
LATENCY_BUFFER=1000 cucumber-js
此示例允许所有请求额外完成一秒钟 .
扩展
在引擎盖下,蝙蝠用 超级代理 用于生成 HTTP 请求。 您可以通过调用获得一个新的 SuperAgent 代理, 而无需 SuperAgent 直接作为依赖 this.newAgent() 在习惯内
步骤定义 :
const agent = this.newAgent();
蝙蝠还保留着一种在木瓜假想中持续存在的特效. 这个
表示如果每个情景都使用 Given 步骤设置一些授权,一个 HTTP 会话或 Bearer 令牌可以重新使用,而无需每次重新登录.
下面的代码示例(从测试中取出),显示了一种习惯 Given 步骤
用于登录和维护客户端会话:
const { setWorldConstructor, After, AfterAll, Before, BeforeAll, Given, When, Then } = require('cucumber');
const { registerHooks, World, registerSteps } = require('@harver/bat');
setWorldConstructor(World);
registerHooks({ After, AfterAll, Before, BeforeAll });
registerSteps({ Given, Then, When });
// a custom login step
Given('I am logged in as a {string}', async function(role) {
// does an agent for this role already exist?
const roleAgent = this.getAgentByRole(role);
if (roleAgent) {
this.setAgentByRole(role, roleAgent);
return;
}
// construct and send a login request
const agent = this.newAgent();
const req = agent.post(this.replaceVars('{base}/my-login'));
await req.send({
// this gets predefined credentials for this role from the `env/dev.json` file
username: this.replaceVars(`{${role}_user}`),
});
// this also sets `this.currentAgent` so this agent will be used
// for creating the next request.
this.setAgentByRole(role, agent);
});
见 Cu. 联合来文 文档,以了解关于写步骤定义的更多信息.
参考文献
步骤参考 用于支持写入特性文件.
世界新闻社 用于支持自定义步骤定义.