LoginSignup
0
2

More than 1 year has passed since last update.

Step CIでGitHub Actionsを使ったAPI自動テスト

Last updated at Posted at 2022-10-23

Step CIとは

Step CIは、APIのテストとモニタリングを簡単にするOSSツールです。

Node、DockerまたはGitHub Actionsでの実行をサポートしています。

使い方

使い方はテストの定義を宣言したYAMLファイルを用意してコマンドを実行するだけです。

公式のGet startedをちょっとアレンジしてJSONPlaceholderに対してテストを行ってみます。

tests/workflow.yml
version: "1.1"
name: My API Tests
env:
  host: jsonplaceholder.typicode.com
tests:
  example:
    steps:
      - name: Status 200 OK
        http:
          url: https://{{env.host}}/todos/1
          method: GET
          check:
            status: /^200/
      - name: JSON Schema Check
        http:
          url: https://{{env.host}}/posts/1
          method: GET
          check:
            schema:
              type: object
              properties:
                userId:
                  type: integer
                id:
                  type: integer
                title:
                  type: string
                body:
                  type: string
              required:
                - userId
                - id
                - title
                - body
      - name: Post JSON and Performance Check
        http:
          url: https://{{env.host}}/posts
          method: POST
          body:
            userId: 1
            title: "qui est esse"
            body: "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
          check:
            status: /^201/
            performance:
              firstByte:
                - lte: 500
              total:
                - lte: 1000

単純なステータスコードのチェックや、戻り値のJSONスキーマのチェック、JSONのPOSTやパフォーマンス(TTFB)チェックを行っています。
これを実行すると次のようになります。

$ npm install -g stepci
$ stepci run tests/workflow.yml

 Status 200 OK  PASSED  in 0.1s

Request

 GET  https://jsonplaceholder.typicode.com/todos/1  200 OK

Checks

Status
✔ 200

 JSON Schema Check  PASSED  in 0.094s

Request

 GET  https://jsonplaceholder.typicode.com/posts/1  200 OK

Checks

JSON Schema
✔ [object Object]

 Post JSON and Performance Check  PASSED  in 0.472s

Request

 POST  https://jsonplaceholder.typicode.com/posts  201 Created

Checks

Status
✔ 201

Performance
✔ firstByte: 448
✔ total: 466

✔ example passed in 0.666s

✔ My API Tests (tests/workflow.yml) passed in 0.713s

次にこれをGitHub Actionsを使って動かしてみます。

.github/workflows/ci.yml
name: ci

on: [push]

jobs:
  api_test:
    runs-on: ubuntu-latest
    name: API Tests
    steps:
      - uses: actions/checkout@v3
      - name: Step CI Action
        uses: stepci/stepci@main
        with:
          workflow: "tests/workflow.yml"

{5DB2ECAB-E045-4E79-BEA6-90E21F38D16E}.tmp.png

公式のアクションが用意されているので定義ファイルさえあれば簡単にGitHub Actionsで動かすことができます。

他にもコマンドラインで環境変数やシークレットを渡したり、Basic認証やSSLのチェックなど基本的なリクエストに必要な機能は網羅されているようです。
まだリリースされて日が浅いようで公式以外の情報は少ないですが、APIのテストをCI/CDに組み込むのに便利に使えそうです。

0
2
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
0
2