Furumaiは、サーバーサイドシステムの振る舞いを、実装言語やフレームワークに依存せずテストするためのフレームワーク。
Stimulate the system, observe its behavior, and verify the result.
HTTP API、DB、Kafka、shell commandなど、サーバーサイドで発生する処理を「システムへの刺激」として扱い、その結果として観測される状態を検証する。
go get github.com/ningenMe/furumaiテストは given / when / then の3ステップで書く。
given: テストの前提条件を整える(DB seed、事前のKafka publishなど)when: 検証対象そのものに刺激を与える(HTTP request、shell commandなど)then: 刺激の結果を観測し、期待するフルステートと構造比較する
given/whenが呼ぶ処理を Stimulus、thenが検証する値を Observation と呼ぶ。冒頭の "Stimulate the system, observe its behavior, and verify the result." はこの対応をそのまま表しており、各protocol adapterの型名・関数名もこの語彙に沿って付けている。
package examples
import (
"net/http"
"testing"
"github.com/ningenMe/furumai"
"github.com/ningenMe/furumai/adapter/rest"
)
func TestGreetingAPI(t *testing.T) {
client := rest.NewStimulus("http://localhost:8080")
var resp *rest.Response
furumai.When(t, func() error {
var err error
resp, err = client.Get("/greeting", rest.WithQuery("name", "Alice"))
return err
})
furumai.ThenEqual(t, *resp, rest.Response{
StatusCode: http.StatusOK,
Headers: furumai.Ignore(),
Body: `{"greeting":"hello, Alice"}`,
})
}givenとwhenは同じStimulus adapter(上の例ではrest.Stimulus)を共用する。プロトコルごとのadapterはadapter/配下のsubpackageに分かれている(HTTPはadapter/rest)。thenは期待する完全な状態を1つの構造体として書き、furumai.ThenEqualが実際の状態との差分を全てまとめて報告する。値の一部だけを確認したい場合はfurumai.Any()/Regex()/Within()/Ignore()/AnyOrder()といったmatcherを埋め込める。
テストの実行は通常のgo test。Parameterized testもGoのtable-driven testパターンでそのまま書ける。より多くのサンプルはexamples/を参照。
go install github.com/ningenMe/furumai/cmd/furumai@latest
furumai version # バージョン表示
furumai help # ヘルプ表示