13
3

APIのテストをStepCIで実装した際のナレッジ

Posted at

APIのリグレッションテストが元々はJMeterで実装されていたが、人によりJAVAの環境が違って動かなかったり、スクリプトがjavascriptで書かれていたり(古いJAVAでしか動かない)など運用が面倒になっていたので、StepCIで実装しなおす事に。

テストコードがyamlで記述でき可読性、メンテ性も良くなる。かつCIの実装も簡単になるのでヨシ。
ただ動的なテストデータを使用したい場合は多少スクリプトを書く必要があるため、それを書いた際のナレッジ。

以下、実装サンプル 
stepci.js
import { run } from '@stepci/runner'

// Workflow定義
const workflow = {
  version: "1.0",
  name: "Sample Test Workflow",
  // includeしているworkflow共通で使用する変数
  env: {
    host: "localhost:8080",
    header: {
      "access-key": "sample-test-key",
      "access-token": "hogehogehogehogehogehogehogehogehogehoge",
      "content-type": "application/json",
    },
    "api-path": "/v1/dummy-api",
    body: {
      user_id: 111,
    },
  },
  include: [
    // 複数のworkflowを定義できる
    "sample_workflow.yml",
    ...
  ],
}

// テスト実行
run(workflow).then(console.log)
sample_workflow.yaml
tests:
  SampleTest:
    # このworkflowだけで使う変数
    env:
      "action": "create_user",
    steps:
      - name: "Create SP User"
        http:
          # js,yamlどちらで定義した変数も `env.~` で使用
          url: http://${{env.host}}/${{env.api-path}}/${{env.action}}
          method: POST
          headers:
            X-access-key: ${{env.header.access-key}}
            X-access-token: ${{env.header.access-token}}
            Content-Type: ${{env.header.content-type}}
          body: ${{env.body.user_id}}
          check:
            status: 200
13
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
3