0
0

Go テスト時の構造体の比較方法

Last updated at Posted at 2024-06-11

Goの構造体の比較の方法についてメモ

結論

go-cmp便利
シンプルな比較であればassert.Equalで十分。

ちょっと複雑なことをやるならgo-cmpを使うとよい。
例えば以下はよくあるケースだと思う。

  • 日付など特定のカラムを除外して比較したい
  • 構造体の配列の比較の際に並び順は無視して比較したい

調べた経緯

比較するのはいくつか方法があるのでどれが良いのか気になったため。

go-cmp使い方例

  • 比較時に配列の場合に並び順を気にせず、比較したい
  • 特定のカラムを比較対象外にしたい

以下は結果はokとなる

package xxxx

import (
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
)

func Test_xxxx(t *testing.T) {

	type StructA struct {
		ID        int
		Name      string
		CreatedAt time.Time
	}

	expected := []StructA{
		{
			ID:        1,
			Name:      "A",
			CreatedAt: time.Now(),
		},
		{
			ID:        2,
			Name:      "B",
			CreatedAt: time.Now(),
		},
	}

	result := []StructA{
		{
			ID:        2,
			Name:      "B",
			CreatedAt: time.Now(),
		},
		{
			ID:        1,
			Name:      "A",
			CreatedAt: time.Now(),
		},
	}

	// 比較時は並び順は考慮しない・created_atは比較対象外にする
	opt := cmp.Options{
		cmpopts.SortSlices(func(a, b StructA) bool {
			return a.ID < b.ID
		}),
		cmpopts.IgnoreFields(StructA{}, "CreatedAt"),
	}

    // オプション(opt)を用いて比較
	diff := cmp.Diff(expected, result, opt)
	if diff != "" {
		t.Errorf("(-expected, +result)\n%s", diff)
	}

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