vowsjs/api-easy

语言: JavaScript

git: https://github.com/vowsjs/api-easy

用于针对RESTful API生成誓言测试的流畅(即可链接)语法。
Fluent (i.e. chainable) syntax for generating vows tests against RESTful APIs.
README.md (中文)

APIeasy

一种流畅的(即可链接的)语法,用于针对RESTful API生成誓言测试。

安装

安装npm(节点包管理器)

  $ curl http://npmjs.org/install.sh | sh

安装APIeasy

  $ [sudo] npm install api-easy

目的

APIeasy旨在成为在node.js和Javascript中测试RESTful API的简单方法。主要设计目标是减少完全覆盖HTTP上给定API的所有主要和边缘用例所需的测试代码行数。

入门

该库的大多数文档都可以通过带注释的源代码获得,这可以通过jashkenas和docco获得。如果您对此不满意,请继续阅读。 tldr ;?阅读如何在您自己的项目中使用API​​easy

如果您打算使用API​​easy(我希望您这样做),那么花点时间了解誓言管理流控制的方式。在vowsjs.org上阅读(在“测试套件的结构”下),或者只记得誓言使用这种语法结构:

  Suite   → Batch*
  Batch   → Context*
  Context → Topic? Vow* Context*

得到它了?好。 APIeasy套件和誓言套件之间存在一对一的关系; APIeasy本质上是一种更简单的语法,用于管理符合此模式的特定基于誓言的测试集:

  1. 通过对API服务器发出HTTP请求来执行测试
  2. 断言是针对HTTP响应和JSON响应主体进行的
  3. 冲洗。重复。

以下是APIeasy消除的样板代码示例:

  var request = require('request'),
      vows = require('vows'),
      assert = require('assert');

  vows.describe('your/awesome/api').addBatch({
    "When using your awesome api": {
      "and your awesome resource": {
        "A POST to /awesome": {
          topic: function () {
            request({
              uri: 'http://localhost:8080/awesome',
              method: 'POST',
              body: JSON.stringify({ test: 'data' }),
              headers: {
                'Content-Type': 'application/json'
              }
            }, this.callback)
          },
          "should respond with 200": function (err, res, body) {
            assert.equal(res.statusCode, 200);
          },
          "should respond with ok": function (err, res, body) {
            var result = JSON.parse(body);
            assert.equal(result.ok, true);
          },
          "should respond with x-test-header": function (err, res, body) {
            assert.include(res.headers, 'x-test-header');
          }
        }
      }
    }
  }).export(module);

使用API​​easy可以像这样实现相同的代码:

  var APIeasy = require('api-easy'),
      assert = require('assert');

  var suite = APIeasy.describe('your/awesome/api');

  suite.discuss('When using your awesome API')
       .discuss('and your awesome resource')
       .use('localhost', 8080)
       .setHeader('Content-Type', 'application/json')
       .post('/awesome', { test: 'data' })
         .expect(200, { ok: true })
         .expect('should respond with x-test-header', function (err, res, body) {
           assert.include(res.headers, 'x-test-header');
         })
       .export(module);

在您自己的项目中使用API​​easy

在您自己的项目中使用API​​easy有两种方法:

  1. 使用npm
  2. 直接使用誓言

使用API​​easy和npm

如果您之前在npm中使用过npm test命令,那么这应该不是什么新鲜事。您可以在此处阅读有关npm test命令的更多信息。您需要做的就是将以下内容添加到package.json文件中:

 {
   "dependencies": {
     "api-easy": "0.2.x"
   },
   "scripts": {
     "test": "vows test/*-test.js"
   }
 }

注意:test / * - test.js由您自行决定。它只是项目中所有测试文件的表达式。

将此添加到package.json文件后,您可以运行以下命令来执行测试:

  $ cd path/to/your/project
  $ npm install
  $ npm test

这里还有一个如何使用这种方法的完整工作样本。

使用API​​easy与誓言

当您安装APIeasy或将其作为package.json文件中的依赖项时,它将不会全局安装vows,因此要使用vows,您必须全局安装它。

  $ [sudo] npm install vows -g

安装誓言后,您只需从项目内部运行它:

  $ cd /path/to/your/project
  $ vows

路线图

  1. 获取有关通过此库可以显示的其他内容的反馈。
  2. 改进它。
  3. 重复(1)+(2)。

运行测试


npm test

作者:查理罗宾斯

本文使用googletrans自动翻译,仅供参考, 原文来自github.com

en_README.md

APIeasy Build Status

A fluent (i.e. chainable) syntax for generating vows tests against RESTful APIs.

Installation

Installing npm (node package manager)

  $ curl http://npmjs.org/install.sh | sh

Installing APIeasy

  $ [sudo] npm install api-easy

Purpose

APIeasy is designed to be a simple way to test RESTful APIs in node.js and Javascript. The primary design goal was to reduce the number of lines of test code required to fully cover all primary and edge use cases of a given API over HTTP.

Getting Started

Most of the documentation for this library is available through the annotated source code, available here thanks to jashkenas and docco. If you're not feeling up for that, just keep reading here. tldr;? Read how to use APIeasy in your own projects

If you're going to use APIeasy (and I hope you do), it's worth taking a moment to understand the way that vows manages flow control. Read up here on vowsjs.org (Under "Structure of a test suite"), or just remember vows uses this grammatical structure:

  Suite   → Batch*
  Batch   → Context*
  Context → Topic? Vow* Context*

Got it? Good. There is a 1-to-1 relationship between a APIeasy suite and a vows suite; APIeasy is essentially a simpler syntax to manage a particular set of vows-based tests that conform to this pattern:

  1. Tests are performed by making HTTP requests against an API server
  2. Assertions are made against the HTTP response and JSON response body
  3. Rinse. Repeat.

Here's a sample of the boilerplate code that APIeasy eliminates:

  var request = require('request'),
      vows = require('vows'),
      assert = require('assert');

  vows.describe('your/awesome/api').addBatch({
    "When using your awesome api": {
      "and your awesome resource": {
        "A POST to /awesome": {
          topic: function () {
            request({
              uri: 'http://localhost:8080/awesome',
              method: 'POST',
              body: JSON.stringify({ test: 'data' }),
              headers: {
                'Content-Type': 'application/json'
              }
            }, this.callback)
          },
          "should respond with 200": function (err, res, body) {
            assert.equal(res.statusCode, 200);
          },
          "should respond with ok": function (err, res, body) {
            var result = JSON.parse(body);
            assert.equal(result.ok, true);
          },
          "should respond with x-test-header": function (err, res, body) {
            assert.include(res.headers, 'x-test-header');
          }
        }
      }
    }
  }).export(module);

This same code can be implemented like this using APIeasy:

  var APIeasy = require('api-easy'),
      assert = require('assert');

  var suite = APIeasy.describe('your/awesome/api');

  suite.discuss('When using your awesome API')
       .discuss('and your awesome resource')
       .use('localhost', 8080)
       .setHeader('Content-Type', 'application/json')
       .post('/awesome', { test: 'data' })
         .expect(200, { ok: true })
         .expect('should respond with x-test-header', function (err, res, body) {
           assert.include(res.headers, 'x-test-header');
         })
       .export(module);

Using APIeasy in your own project

There are two ways to use APIeasy in your own project:

  1. Using npm
  2. Using vows directly

Using APIeasy with npm

If you've used the npm test command in npm before, this should be nothing new. You can read more about the npm test command here. All you need to do is add the following to your package.json file:

 {
   "dependencies": {
     "api-easy": "0.2.x"
   },
   "scripts": {
     "test": "vows test/*-test.js"
   }
 }

Note: test/*-test.js is at your discretion. It's just an expression for all test files in your project.

After adding this to your package.json file you can run the following to execute your tests:

  $ cd path/to/your/project
  $ npm install
  $ npm test

There is also a full working sample of how to use this approach here.

Using APIeasy with vows

When you install APIeasy or take it as a dependency in your package.json file it will not install vows globally, so to use vows you must install it globally.

  $ [sudo] npm install vows -g

After installing vows you can simply run it from inside your project:

  $ cd /path/to/your/project
  $ vows

Roadmap

  1. Get feedback on what else could be exposed through this library.
  2. Improve it.
  3. Repeat (1) + (2).

Run Tests


npm test

Author: Charlie Robbins