Kaynağa Gözat

Use testify/suite and testify/require

Djordje Lukic 5 yıl önce
ebeveyn
işleme
756836ffab
3 değiştirilmiş dosya ile 56 ekleme ve 46 silme
  1. 53 46
      context/store/store_test.go
  2. 1 0
      go.mod
  3. 2 0
      go.sum

+ 53 - 46
context/store/store_test.go

@@ -34,67 +34,74 @@ import (
 	"testing"
 	"testing"
 
 
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
+	"github.com/stretchr/testify/suite"
 )
 )
 
 
-func setup(t *testing.T, cb func(*testing.T, Store)) {
+type StoreTestSuite struct {
+	suite.Suite
+	store Store
+	dir   string
+}
+
+func (suite *StoreTestSuite) BeforeTest(suiteName, testName string) {
 	dir, err := ioutil.TempDir("", "store")
 	dir, err := ioutil.TempDir("", "store")
-	assert.Nil(t, err)
-	defer os.RemoveAll(dir)
+	require.Nil(suite.T(), err)
 
 
 	store, err := New(dir)
 	store, err := New(dir)
-	assert.Nil(t, err)
+	require.Nil(suite.T(), err)
 
 
-	cb(t, store)
+	suite.dir = dir
+	suite.store = store
 }
 }
 
 
-func TestGetUnknown(t *testing.T) {
-	setup(t, func(t *testing.T, store Store) {
-		meta, err := store.Get("unknown")
-		assert.Nil(t, meta)
-		assert.Error(t, err)
-	})
+func (suite *StoreTestSuite) AfterTest(suiteName, testName string) {
+	os.RemoveAll(suite.dir)
 }
 }
 
 
-func TestCreate(t *testing.T) {
-	setup(t, func(t *testing.T, store Store) {
-		err := store.Create("test", nil, nil)
-		assert.Nil(t, err)
-	})
+func (suite *StoreTestSuite) TestCreate() {
+	err := suite.store.Create("test", nil, nil)
+	assert.Nil(suite.T(), err)
 }
 }
 
 
-func TestGet(t *testing.T) {
-	setup(t, func(t *testing.T, store Store) {
-		err := store.Create("test", TypeContext{
-			Type:        "type",
-			Description: "description",
-		}, nil)
-		assert.Nil(t, err)
-
-		meta, err := store.Get("test")
-		assert.Nil(t, err)
-		assert.NotNil(t, meta)
-		assert.Equal(t, "test", meta.Name)
-
-		m, ok := meta.Metadata.(TypeContext)
-		assert.Equal(t, ok, true)
-		assert.Equal(t, "description", m.Description)
-		assert.Equal(t, "type", m.Type)
-	})
+func (suite *StoreTestSuite) TestGetUnknown() {
+	meta, err := suite.store.Get("unknown")
+	assert.Nil(suite.T(), meta)
+	assert.Error(suite.T(), err)
 }
 }
 
 
-func TestList(t *testing.T) {
-	setup(t, func(t *testing.T, store Store) {
-		err := store.Create("test1", TypeContext{}, nil)
-		assert.Nil(t, err)
+func (suite *StoreTestSuite) TestGet() {
+	err := suite.store.Create("test", TypeContext{
+		Type:        "type",
+		Description: "description",
+	}, nil)
+	assert.Nil(suite.T(), err)
+
+	meta, err := suite.store.Get("test")
+	assert.Nil(suite.T(), err)
+	assert.NotNil(suite.T(), meta)
+	assert.Equal(suite.T(), "test", meta.Name)
+
+	m, ok := meta.Metadata.(TypeContext)
+	assert.Equal(suite.T(), ok, true)
+	assert.Equal(suite.T(), "description", m.Description)
+	assert.Equal(suite.T(), "type", m.Type)
+}
+func (suite *StoreTestSuite) TestList() {
+	err := suite.store.Create("test1", TypeContext{}, nil)
+	assert.Nil(suite.T(), err)
 
 
-		err = store.Create("test2", TypeContext{}, nil)
-		assert.Nil(t, err)
+	err = suite.store.Create("test2", TypeContext{}, nil)
+	assert.Nil(suite.T(), err)
 
 
-		contexts, err := store.List()
-		assert.Nil(t, err)
+	contexts, err := suite.store.List()
+	assert.Nil(suite.T(), err)
+
+	require.Equal(suite.T(), len(contexts), 2)
+	assert.Equal(suite.T(), contexts[0].Name, "test1")
+	assert.Equal(suite.T(), contexts[1].Name, "test2")
+}
 
 
-		assert.Equal(t, len(contexts), 2)
-		assert.Equal(t, contexts[0].Name, "test1")
-		assert.Equal(t, contexts[1].Name, "test2")
-	})
+func TestExampleTestSuite(t *testing.T) {
+	suite.Run(t, new(StoreTestSuite))
 }
 }

+ 1 - 0
go.mod

@@ -3,6 +3,7 @@ module github.com/docker/api
 go 1.13
 go 1.13
 
 
 require (
 require (
+	github.com/coreos/etcd v3.3.10+incompatible
 	github.com/golang/protobuf v1.4.0
 	github.com/golang/protobuf v1.4.0
 	github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
 	github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
 	github.com/mitchellh/go-homedir v1.1.0
 	github.com/mitchellh/go-homedir v1.1.0

+ 2 - 0
go.sum

@@ -18,6 +18,7 @@ github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL
 github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
 github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
 github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
 github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
 github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
 github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
+github.com/coreos/etcd v3.3.10+incompatible h1:jFneRYjIvLMLhDLCzuTuU4rSJUjRplcJQ7pD7MnhC04=
 github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
 github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
 github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
 github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
 github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
 github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
@@ -73,6 +74,7 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
 github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
 github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
 github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
 github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
 github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
 github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
+github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo=
 github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
 github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
 github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
 github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
 github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
 github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=