LoginSignup
0
0

【Go言語】echoのAPIをテストする [ハンドラー単体のテスト]

Last updated at Posted at 2023-12-23

テストによってはサーバーを起動させることなくハンドラーの単体テストを行いたいことがあります。そういった場合にはリクエストを直接ハンドラーに流してあげることで結果を得ることができます。

テスト用にプログラムを用意する

APIを作成する

前回の記事、【Go言語】echoのAPIをテストする [サーバーを起動させてテスト]で作成したAPIのコードをそのまま利用しても同じようにテストができるということを証明するためにコードを再利用します。

package main

import "github.com/labstack/echo/v4"

func InitServer() *echo.Echo {
	e := echo.New()

	e.GET("/", func(c echo.Context) error {
		return c.String(200, "Hello, World!")
	})

	return e
}

func main() {
	e := InitServer()
	e.Logger.Fatal(e.Start(":8080"))
}

テストの作成

テスト側のコードは以下の通りです。順番に説明してきます。

package main

import (
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestEchoHandler(t *testing.T) {
	e := InitServer()
	r := httptest.NewRequest(http.MethodGet, "/", nil)
	w := httptest.NewRecorder()

	e.ServeHTTP(w, r)

	if status := w.Code; status != http.StatusOK {
		t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
	}

	expected := `Hello, World!`
	if w.Body.String() != expected {
		t.Errorf("handler returned unexpected body: got %v want %v", w.Body.String(), expected)
	}
}

サーバーのインスタンスを作成

InitServer関数でechoのサーバーのインスタンスを持ってきます。

e := InitServer()

リクエストの作成

httptest.NewRequestを用いてリクエストの作成を行っています。引数を変えることで任意のエンドポイントを指定できます。

r := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()

ハンドラの起動

ここでハンドラにリクエストを渡すして処理を行ってもらい、wに結果を格納してもらいます。

e.ServeHTTP(w, r)

結果の検証

テストで検証していきます。ステータスコードのテスト、bodyの内容テストなどをここで行っています。

`go
if status := w.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}

expected := Hello, World!
if w.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v", w.Body.String(), expected)
}
`

まとめ

上記のようにすることでハンドラー単体のテストが行なえます。

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