gotest

module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 25, 2024 License: MIT

README

gotest

Unit Testing Utilities for Go

Installation

go get github.com/ebi-yade/gotest/cases

Usage

Error Check

Works as a wrapper for testify/require in table-driven test cases.

package foo_test

import (
	"testing"

	"foo" // your package

	"github.com/ebi-yade/gotest/cases"
)

func TestFoo(t *testing.T) {
	tests := []struct {
		name     string
		input    foo.Input
		errCheck cases.ErrorCheck // func(*testing.T, error)
	}{
		{
			name: "case1",
			input: foo.Input{
				Amout: 1,
			},
			errCheck: cases.NoError,
		},
		{
			name: "case2",
			input: foo.Input{
				Amout: 999,
			},
			errCheck: cases.ErrorIs(foo.ErrTooLargeAmount),
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			err := foo.DoSomething(tt.input)
			tt.errCheck(t, err)
		})
	}
}
Memo
ここまで書いて気づいたこと
  • assert.NoError は bool を返すが、 require.NoError は何も返さない
  • t.Run するテーブル駆動テストであれば require にわたる t は無名関数のスコープ
  • エラーチェックをする場合は基本的に require でいいはず(暗黙直和)
  • ErrorIs などはシグニチャが異なるのでラッパーの有り難みがある

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL