api-test-runnerdeprecated

```shell # 安装 Node.js apt-get install curl build-essential curl https://raw.githubusercontent.com/creationix/nvm/v0.25.1/install.sh | bash . ~/.profile nvm install 0.12.2 nvm alias default 0.12.2

Usage no npm install needed!

<script type="module">
  import apiTestRunner from 'https://cdn.skypack.dev/api-test-runner';
</script>

README

安装

# 安装 Node.js
apt-get install curl build-essential
curl https://raw.githubusercontent.com/creationix/nvm/v0.25.1/install.sh | bash
. ~/.profile
nvm install 0.12.2
nvm alias default 0.12.2

# 安装 api-test-runner
npm install api-test-runner@latest -g

编写测试用例

'use strict'

###*
 * A test cases file is a valid node.js module written in coffeescript,
 * which exports an array of test case declarations to be run.
 *
 * You define each case with these basic components:
 *   description: describe what this api do
 *   req: declare the request to send
 *   res: assert the response received, including status code, headers, and body
 *   mongodb: make mongodb operations before or after the test, like setup and teardown in other unit test frameworks
 *
 * Supported mongodb operations: insert, remove, update
###

# Define reusable data as vars, if you don't want to repeat them over and over again
auth =
  username: 'b05eb51341fcdccffb417a1a'
  password: 'ebfedd3fef00b2b2ee1b3e7c'

geoFenceToCreate =
  name: '上海浦东软件园'
  circle:
    center:
      lng: 121.60
      lat: 31.21
    radius: 5000

geoFenceCreated =
  # You can use regular expression to assert string
  id: /^[0-9a-fA-F]{24}$/
  app: 'ba55d7d9472260547a867e00'
  name: '上海浦东软件园'
  circle:
    center:
      # Nested structure equality comparison like this is supported
      lng: 121.60
      lat: 31.21
    radius: 5000
  stats:
    messagePushes: 0
    messages: 0
  createdAt: /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.000Z$/

# Export test cases
module.exports = [
  description: 'return 200 with created geo fence'
  req:
    method: 'post'
    url: '/v2/partner/apps/ba55d7d9472260547a867e00/geoFences'
    auth: auth
    body: geoFenceToCreate
    # Arbitrary code get executed before sending the request
    beforeSend: (req) ->
      # Do something about the request going to be sent
  res:
    # Assert the response time shouldn't be longer than 10 seconds
    timeout: 10 * 1000
    status: 200
    headers:
      'content-type': 'application/json; charset=utf-8'
    body: geoFenceCreated
    # Arbitrary code get executed before asserting the response
    beforeAssert: (req, res, expectedRes) ->
      # Do something about the res going to be asserted

    # Arbitrary code get executed after asserting the response
    afterAssert: (req, res, expectedRes) ->
      # By this way, other cases can use the id
      geoFenceCreated.id = res.body.id
  mongodb:
    # Execute these commands before this test
    before: [
      collection: 'geoFences'
      remove: {}
    ]
,

  description: 'return 400 when create another one with same name'
  req:
    method: 'post'
    url: '/v2/partner/apps/ba55d7d9472260547a867e00/geoFences'
    auth: auth
    body: geoFenceToCreate
  res:
    status: 400
,

  description: 'return geo fence previously created in a list'
  req:
    method: 'get'
    url: '/v2/partner/apps/ba55d7d9472260547a867e00/geoFences'
    auth: auth
  res:
    status: 200
    headers:
      'content-type': 'application/json; charset=utf-8'
    body:
      count: 1
      list: [geoFenceCreated]
,

  # Use method instead of direct value to delay the computation, so we can use data returned by previous tests
  ->
    description: 'return 200'
    req:
      method: 'delete'
      url: "/v2/partner/apps/ba55d7d9472260547a867e00/geoFences/#{ geoFenceCreated.id }"
      auth: auth
    res:
      status: 200
]

运行测试用例

api-test-runner --baseUrl http://devapi.tuisongbao.com --mongodbUrl mongodb://localhost/tuisongbao_devOSX --files 'example/*'