0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Testify の suite まとめ

Last updated at Posted at 2021-03-08

環境

$ go version
go version go1.15.7 linux/amd64
$ go mod graph
mymodule github.com/davecgh/go-spew@v1.1.1
mymodule github.com/stretchr/testify@v1.7.0
mymodule gopkg.in/yaml.v3@v3.0.0-20210107192922-496545a6307b

suite の作り方

suite.Suiteを持つ構造体を定義し、任意に次の6つのメソッドを実装します。

  • SetupSuite()
  • SetupTest()
  • BeforeTest(suiteName, testName string)
  • AfterTest(suiteName, testName string)
  • TearDownTest()
  • TearDownSuite()

メソッド名にSuiteを含むものは一度しか実行されず、その他のメソッドはテストの数だけ実行されます。

まとめ

次のコードは、github.com/stretchr/testifyモジュールを事前に入れておくと、単体でコンパイル可能なコードです。

package main

import (
	"testing"

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

type MySuite struct {
	suite.Suite
}

func (s *MySuite) SetupSuite() {
}

func (s *MySuite) SetupTest() {
}

func (s *MySuite) BeforeTest(_, _ string) {
}

func (s *MySuite) AfterTest(_, _ string) {
}

func (s *MySuite) TearDownTest() {
}

func (s *MySuite) TearDownSuite() {
}

func (s *MySuite) TestOne() {
	assert.Equal(s.T(), 1, 1)
}

func (s *MySuite) TestTwo() {
	assert.Equal(s.T(), 2, 2)
}

func (s *MySuite) TestThree() {
	assert.Equal(s.T(), 3, 3)
}

func TestMySuite(t *testing.T) {
	suite.Run(t, new(MySuite))
}
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?