Skip to content

Commit

Permalink
*: migrate test framework to testify (tikv#5193)
Browse files Browse the repository at this point in the history
ref tikv#4813

Signed-off-by: Ryan Leung <rleungx@gmail.com>

Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
  • Loading branch information
2 people authored and CabinfeverB committed Jul 14, 2022
1 parent 2c5e434 commit c0a6b11
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 157 deletions.
12 changes: 3 additions & 9 deletions pkg/assertutil/assertutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,14 @@ type Checker struct {
}

// NewChecker creates Checker with FailNow function.
func NewChecker(failNow func()) *Checker {
return &Checker{
FailNow: failNow,
}
}

func (c *Checker) failNow() {
c.FailNow()
func NewChecker() *Checker {
return &Checker{}
}

// AssertNil calls the injected IsNil assertion.
func (c *Checker) AssertNil(obtained interface{}) {
if c.IsNil == nil {
c.failNow()
c.FailNow()
return
}
c.IsNil(obtained)
Expand Down
5 changes: 3 additions & 2 deletions pkg/assertutil/assertutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ func TestNilFail(t *testing.T) {
t.Parallel()
re := require.New(t)
var failErr error
checker := NewChecker(func() {
checker := NewChecker()
checker.FailNow = func() {
failErr = errors.New("called assert func not exist")
})
}
re.Nil(checker.IsNil)
checker.AssertNil(nil)
re.NotNil(failErr)
Expand Down
5 changes: 4 additions & 1 deletion server/api/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ import (
var _ = Suite(&testVersionSuite{})

func checkerWithNilAssert(c *C) *assertutil.Checker {
checker := assertutil.NewChecker(c.FailNow)
checker := assertutil.NewChecker()
checker.FailNow = func() {
c.FailNow()
}
checker.IsNil = func(obtained interface{}) {
c.Assert(obtained, IsNil)
}
Expand Down
26 changes: 11 additions & 15 deletions server/join/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,28 @@ package join
import (
"testing"

. "github.com/pingcap/check"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/assertutil"
"github.com/tikv/pd/pkg/testutil"
"github.com/tikv/pd/server"
)

func TestJoin(t *testing.T) {
TestingT(t)
}

var _ = Suite(&testJoinServerSuite{})

type testJoinServerSuite struct{}

func checkerWithNilAssert(c *C) *assertutil.Checker {
checker := assertutil.NewChecker(c.FailNow)
func checkerWithNilAssert(re *require.Assertions) *assertutil.Checker {
checker := assertutil.NewChecker()
checker.FailNow = func() {
re.FailNow("")
}
checker.IsNil = func(obtained interface{}) {
c.Assert(obtained, IsNil)
re.Nil(obtained)
}
return checker
}

// A PD joins itself.
func (s *testJoinServerSuite) TestPDJoinsItself(c *C) {
cfg := server.NewTestSingleConfig(checkerWithNilAssert(c))
func TestPDJoinsItself(t *testing.T) {
re := require.New(t)
cfg := server.NewTestSingleConfig(checkerWithNilAssert(re))
defer testutil.CleanServer(cfg.DataDir)
cfg.Join = cfg.AdvertiseClientUrls
c.Assert(PrepareJoinCluster(cfg), NotNil)
re.Error(PrepareJoinCluster(cfg))
}
Loading

0 comments on commit c0a6b11

Please sign in to comment.