go-ginチュートリアル — Part6

GinのBlog API’s 作成 — Testing&CI周り

gavin.zhou
9 min readDec 6, 2018

プロジェクトを作る時、ちゃんとテストケースを作れないといけません。 unit test を作りましょう。

Create unit testing

goのライブラリーにはテストフレームワークが含まれますが、なかなか簡単です、今回 testify を使います。

Install testity

go get -u github.com/stretchr/testify##一番よく使えるパッケージがこれです。
github.com/stretchr/testify/assert

Create test case

Routers フォルダに新しいファイル test_routers.go を作ります。 InitRouter() のテストケースを作ってみましょう。コードは下記となります。

package routers

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

"github.com/stretchr/testify/assert"
)

func TestInitRouter(t *testing.T) {
router := InitRouter()

w := httptest.NewRecorder()

req, _ := http.NewRequest("GET", "/api/v1/tags", nil)
router.ServeHTTP(w, req)

assert.Equal(t, 200, w.Code)
}

Run test case

❯❯❯cd hello-gin/routersgo test
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /api/v1/tags --> github.com/gavinzhou/hello-gin/routers/api/v1.GetTags (3 handlers)
[GIN-debug] POST /api/v1/tags --> github.com/gavinzhou/hello-gin/routers/api/v1.AddTag (3 handlers)
[GIN-debug] PUT /api/v1/tags/:id --> github.com/gavinzhou/hello-gin/routers/api/v1.EditTag (3 handlers)
[GIN-debug] DELETE /api/v1/tags/:id --> github.com/gavinzhou/hello-gin/routers/api/v1.DeleteTag (3 handlers)
[GIN-debug] GET /api/v1/articles --> github.com/gavinzhou/hello-gin/routers/api/v1.GetArticles (3 handlers)
[GIN-debug] GET /api/v1/articles/:id --> github.com/gavinzhou/hello-gin/routers/api/v1.GetArticle (3 handlers)
[GIN-debug] POST /api/v1/articles --> github.com/gavinzhou/hello-gin/routers/api/v1.AddArticle (3 handlers)
[GIN-debug] PUT /api/v1/articles/:id --> github.com/gavinzhou/hello-gin/routers/api/v1.EditArticle (3 handlers)
[GIN-debug] DELETE /api/v1/articles/:id --> github.com/gavinzhou/hello-gin/routers/api/v1.DeleteArticle (3 handlers)
[GIN] 2018/11/15 - 21:46:46 | 200 | 9.456152ms | | GET /api/v1/tags
PASS
ok github.com/gavinzhou/hello-gin/routers 0.071s

すごく簡単なテストケースを作りました。 coverage の比率によって、コードのクオリティが違います。

Go module

go 1.11からパッケージ管理は Go module が出来ます。

Go module については、この記事をご参照ください

環境関数を GO111MODULE=no をすれば、良いです。サーバを再起動してみれば、下記となります。

export GO111MODULE=ongo run main.go
go: creating new go.mod: module github.com/gavinzhou/hello-gin
go: finding github.com/Unknwon/com latest
go: finding github.com/astaxie/beego/validation latest
go: finding github.com/jinzhu/gorm/dialects/postgres latest
go: finding github.com/jinzhu/gorm/dialects latest
go: finding github.com/lib/pq/hstore latest
go: finding github.com/gin-contrib/sse latest
go: finding github.com/ugorji/go/codec latest
go: finding github.com/golang/protobuf/proto latest
go: finding github.com/jinzhu/inflection latest
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /api/v1/tags --> github.com/gavinzhou/hello-gin/routers/api/v1.GetTags (3 handlers)
...

プロジェクトフォルダに自動的に go.modgo.sum ファイルを作りました。中身下記の様な感じです。

more go.mod
module github.com/gavinzhou/hello-gin
require (
github.com/Unknwon/com v0.0.0-20181010210213-41959bdd855f
github.com/astaxie/beego v1.10.1
github.com/gin-contrib/sse v0.0.0-20170109093832-22d885f9ecc7 // indirect
github.com/gin-gonic/gin v1.3.0
github.com/golang/protobuf v1.2.0 // indirect
github.com/jinzhu/gorm v1.9.1
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a // indirect
github.com/kelseyhightower/envconfig v1.3.0
github.com/lib/pq v1.0.0 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/ugorji/go/codec v0.0.0-20181022190402-e5e69e061d4f // indirect
gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
gopkg.in/yaml.v2 v2.2.1 // indirect
)

現在プロジェクトフォルダ構成は下記となります。

hello-gin
├── README.md
├── go.mod
├── go.sum
├── main.go
├── middleware
├── models
│ ├── article.go
│ ├── models.go
│ └── tag.go
├── pkg
│ ├── e
│ │ ├── code.go
│ │ └── msg.go
│ ├── setting
│ │ └── setting.go
│ └── util
│ └── pageination.go
├── routers
│ ├── api
│ │ └── v1
│ │ ├── article.go
│ │ └── tag.go
│ ├── routers.go
│ └── routers_test.go
└── runtime

Run testing in circle-ci

テストケースを作りましたが、 circle-ci でテストをしてみましょう。

プロジェクトフォルダに .circleci フォルダを作成、 config.yml フィアルを作ります。 config.yml の設定が下記となります。

version: 2
jobs:
build:
docker:
- image: circleci/golang:1.11
environment:
GO111MODULE: "ON"
working_directory: ~/hello-gin
steps:
- checkout
- run:
name: Run test
command: |
eval $(cat .env)
go test ./...

circle-ciのcacheを使うとテスト時間が短縮出来ます。具体的設定をsample codeにご参照ください。

ここまでテスト周りのものを作りました。次回、別の部分を作ってみましょう。

参考サイト

※問題があった場合は、githubの issue をお願いします。

--

--

No responses yet