0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PostmanのコレクションをGit管理できるYAMLテストに移して、CIとClaude Codeで回す

0
Posted at

はじめに

Postmanの無料プランは、2026年3月から1ユーザー限定になり、チームを作れなくなりました(公式ドキュメント)。少人数のチームでコレクションを共有していた方は、有料化か移行かを決めることになったと思います。

こうした状況の中、私たちはせっかくなら以前からの不満もまとめて解消したいと考えました。

  • コレクションがコードと別の場所にあり、PRでレビューできない
  • テストがリクエストごとのJavaScriptに散らばり、CIで回すには別の仕組みが要る
  • コードを変えてもコレクションは追従しない。どのエンドポイントが押さえられていないかも分からない

この記事では、私たちがMITライセンスで公開しているCLI「Ajiro API」を使って、PostmanのコレクションをエンドポイントごとのYAMLに移す手順を紹介します。Brunoの .bru フォルダも同じ手順で対応しています。

移行後の形

エンドポイント1本につきYAMLファイルを1つ作り、アプリのリポジトリに一緒にコミットします。

api/
  auth/post-auth-login.yaml
  todos/get-todos.yaml
  todos/post-todos.yaml
  todos/get-todos-stats.yaml
  ...
ajiro-api.yaml        # 接続先や環境の設定
.env                  # パスワードなど(コミットしない)

このYAMLが、テストとモックと仕様書の元になります。実行は単一バイナリのCLIで、アカウント登録もクラウドへの同期もありません。

1. コレクションを取り込む

Postmanからコレクションをv2.1形式でエクスポートし、アプリのルートで次を実行します。

npx -y ajiro-api init
npx -y ajiro-api import postman todo.postman_collection.json
4 created, 0 updated, 0 skipped
  + api/auth/post-auth-login.yaml
  + api/todos/get-todos.yaml
  + api/todos/post-todos.yaml
  + api/todos/get-todos-stats.yaml
warning: request `Login`: pre-request/test scripts are not converted

Suggested variables for ajiro-api.yaml:
  environments:
    local:
      variables:
        baseUrl: http://localhost:3000

フォルダ名は tags に、リクエスト名は summary に入ります。保存していたレスポンスの例は mock に入ります。

api/todos/post-todos.yaml
version: 1
id: todos.create
summary: Create todo
tags:
  - Todos
request:
  method: POST
  path: /todos
  headers:
    Authorization: Bearer {{ token }}
    Content-Type: application/json
  body:
    title: Buy milk
tests:
  - name: returns 201
    expect:
      status: 201
mock:
  default:
    status: 201
    body:
      id: td_1
      title: Buy milk
      done: false

2. テストスクリプトを書き換える

警告にあるとおり、PostmanのJavaScript(TestsやPre-request Script)は変換されません。よく使う書き方は、次のようにYAMLに置き換えます。

Postman Ajiro API
pm.response.to.have.status(200) expect: { status: 200 }
pm.expect(json.title).to.eql("Buy milk") expect: { body: { match: { $.title: Buy milk } } }
pm.collectionVariables.set('token', json.token) capture: { token: $.token }
環境変数に入れたパスワード .env に置いて {{ env.NAME }} で参照
リクエストの実行順 depends_on: [先に実行するid]

ログインは、取り込んだ結果をこう直しました。

api/auth/post-auth-login.yaml
id: auth.login.create
request:
  method: POST
  path: /auth/login
  body:
    username: demo
    password: '{{ env.DEMO_PASSWORD }}'
tests:
  - name: logs in
    expect:
      status: 200
    capture:
      token: $.token

トークンを使う側には depends_on: [auth.login.create] を足します。ログインが先に実行され、{{ token }} に値が入ります。トークンやパスワードは、出力では *** に伏せられます。

ほとんどのエンドポイントにログインが要る場合は、ajiro-api.yaml の環境に setup と共通の headers を書くと、ファイルごとの depends_on が要らなくなります。

3. コードと突き合わせる

ここが移行で一番嬉しいところです。scan を実行すると、アプリのルーティングを静的解析して、取り込んだYAMLと突き合わせます。

npx -y ajiro-api scan
~ POST /auth/login  api/auth/post-auth-login.yaml
+ GET /health  api/health/get-health.yaml
~ GET /todos  api/todos/get-todos.yaml
~ POST /todos  api/todos/post-todos.yaml
~ GET /todos/stats  api/todos/get-todos-stats.yaml
+ DELETE /todos/{id}  api/todos/delete-todos-id.yaml
+ GET /todos/{id}  api/todos/get-todos-id.yaml
+ PATCH /todos/{id}  api/todos/patch-todos-id.yaml
hono: 4 created, 4 updated, 0 unchanged (8 endpoints, 9 ms)

~ の4本は、取り込んだファイルにハンドラの場所(source)が書き足されたものです。+ の4本は、コードにはあるのにコレクションにはなかったエンドポイントです。この例では、更新と削除が丸ごと押さえられていませんでした。

取り込んだ tests や mock は、scanしても上書きされません。以後はコードを変えるたびにscanすれば、定義がコードに追従します。

4. 実行する

アプリを起動してから実行します。

npx -y ajiro-api test
Environment: local (http://localhost:3000)
✓ auth.login.create / logs in (2ms)
✓ health.get / smoke (0ms)
- todos.delete / smoke
    skipped: smoke test skipped: DELETE may change data; add tests to api/todos/delete-todos-id.yaml (or run with --smoke-unsafe)
- todos.get / smoke
    skipped: smoke test skipped: path parameter `{id}` has no value: set request.params.id (or a test override), or define a variable named `id`
✓ todos.stats.list / responds with 2xx (0ms)
✓ todos.list / responds with 2xx (0ms)
- todos.update / smoke
    skipped: smoke test skipped: PATCH may change data; add tests to api/todos/patch-todos-id.yaml (or run with --smoke-unsafe)
✓ todos.create / returns 201 (0ms)

5 passed, 0 failed, 0 errors, 3 skipped (8 total) in 6ms

テストを書いていないエンドポイントには、「500番台を返さないこと」だけを見る簡易テストがかかります。ただしDELETEやPATCHは、データを壊さないように飛ばします。IDが要るパスも、値がないので飛ばします。なぜ飛ばしたかがそのまま書かれているので、次に書くべきテストの一覧として使えます。

5. 残りのテストはClaude Codeに書かせる

飛ばされた3本のテストは、AIに任せるのが早いです。Claude Codeでプラグインを入れます。

/plugin marketplace add niti-lab/ajiro-api
/plugin install ajiro-api@ajiro-api

あとは次のように頼みます。

ajiro-api test で skipped になっているエンドポイントにテストを書いて。
成功、バリデーションエラー、認証なし、存在しないIDを押さえて、全部通るまで回して。

プラグインのスキルには、「通すためにテストを緩めない」「コードとテストのどちらが間違っているか分からなければ人に聞く」といったルールも入っています。書かれたテストはYAMLの差分としてレビューできます。失敗したときにエージェントがどう直すかは、Zennの記事にまとめています。

6. 保存していたレスポンス例をモックにする

Postmanに保存していたレスポンス例は、そのままモックサーバーになります。

npx -y ajiro-api mock
Ajiro mock server: 8 endpoints on http://127.0.0.1:4010
  GET http://127.0.0.1:4010/__ajiro/endpoints lists them; select a scenario with the X-Ajiro-Scenario header or ?__scenario=
POST /todos → 201 todos.create [default] 0ms

エラー系のレスポンスを試したいときは、mock.scenarios に書いて X-Ajiro-Scenario ヘッダーで切り替えます。フロント担当がAPIの完成を待たずに画面を作れます。

7. コレクションの代わりに仕様書を渡す

Postmanのコレクションを共有していた相手には、HTMLの仕様書を渡せます。

npx -y ajiro-api docs -o api-docs.html --title "Todo API"

ブラウザで開くだけの1ファイルなので、PostmanのアカウントもSwaggerの環境も要りません。OpenAPIが欲しい相手には ajiro-api openapi export -o openapi.yaml で書き出せます。

8. CIで回す

ajiro-api scan --check --ignore-lines        # コードとYAMLがずれたら失敗
ajiro-api test --env ci --junit report.xml   # JUnit XMLも出力

scan --check を入れておくと、エンドポイントを追加したのに定義を更新し忘れたPRが落ちます。コレクションが古くなる問題は、ここで止まります。GitHub Actionsのサンプルはリポジトリにあります。

Brunoから移る場合

npx -y ajiro-api import bruno ./bruno-collection

あとの手順は同じです。どちらの取り込みも、もう一度実行すると差分だけが入ります。--overwrite を付けると request と mock を上書きしますが、tests は常に残ります。

移行しないほうがいいケース

GUIでリクエストを組み立てて手で試す使い方が中心なら、PostmanやBrunoを使い続けるほうが快適です。移行が向いているのは、次のような場合です。

  • テストをコードと一緒にPRでレビューしたい
  • 同じテストをCIで回したい
  • 実装をAIに任せていて、確認もAIに回させたい

対応フレームワークは、Next.jsのRoute Handlers、Hono、FastAPI、Express、Go(net/http、chi、Echo)です。それ以外でも、OpenAPIから取り込めばテストとモックは使えます(scanによる突き合わせは対応フレームワークのみ)。

リポジトリはこちらになります。移行でつまずいたコレクションの書き方があれば、Issueで教えていただけると助かります。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?