Help us understand the problem. What is going on with this article?

go1.10のtest2jsonを試してみた

More than 1 year has passed since last update.

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 .

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした