LoginSignup
6
3

More than 5 years have passed since last update.

go1.10のtest2jsonを試してみた

Last updated at Posted at 2018-02-13

1行でまとめ

test2jsonは、テストレポートをjsonで出力してくれるやつだった。

go1.10でのtestingパッケージ新機能

go1.10のDraft Release Notesを読んでいたら「test2json」なるgo toolsが出るよと書かれていたので試してみた。

test2json試してみた。

リファレンスはこちら

@chimatter氏のgo get go1.10rc2 で気楽に 1.10 を試すを頼りに go1.10rc2をインストールしたあと、足し算と簡単なテストを書いてみる。

calc.go
package main

func Add(x int , y int) int {
    return x + y
}
calc_test.go
package main

import (
    "testing"
    "github.com/stretchr/testify/assert"
)

func TestAdd(t *testing.T) {
    testCases := []struct {
        x int
        y int
        ans int
    }{
        {1, 2, 3},
        {3, 4, 7},
    }
    for _, test := range testCases {
        assert.Equal(t, Add(test.x, test.y), test.ans, "test fail.")
    }
}

通常、goのテストを叩くときは

$ go test .

とコマンドを実行すればいいのだが、 test2json 経由で結果をjson出力したい場合はこのようになる。

$ go1.10rc2 test -json .
{"Time":"2018-02-13T21:29:12.168347168+09:00","Action":"run","Package":"_/Users/dproject21/rc_test","Test":"TestAdd"}
{"Time":"2018-02-13T21:29:12.168552203+09:00","Action":"output","Package":"_/Users/dproject21/rc_test","Test":"TestAdd","Output":"=== RUN   TestAdd\n"}
{"Time":"2018-02-13T21:29:12.168569401+09:00","Action":"output","Package":"_/Users/dproject21/rc_test","Test":"TestAdd","Output":"--- PASS: TestAdd (0.00s)\n"}
{"Time":"2018-02-13T21:29:12.168574805+09:00","Action":"pass","Package":"_/Users/dproject21/rc_test","Test":"TestAdd","Elapsed":0}
{"Time":"2018-02-13T21:29:12.168585837+09:00","Action":"output","Package":"_/Users/dproject21/rc_test","Output":"PASS\n"}
{"Time":"2018-02-13T21:29:12.168590911+09:00","Action":"output","Package":"_/Users/dproject21/rc_test","Output":"ok  \t_/Users/dproject21/rc_test\t(cached)\n"}
{"Time":"2018-02-13T21:29:12.168598287+09:00","Action":"pass","Package":"_/Users/dproject21/rc_test","Elapsed":0}

ちなみに、2018/2/13時点で

$ go test -json .

と叩くと、「そんなフラグねーよ」と怒られるのでお気をつけください。

6
3
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
6
3