Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests run by definition order #258

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions suite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"reflect"
"regexp"
"sort"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -16,6 +17,25 @@ var matchMethod = flag.String("m", "", "regular expression to select tests of th

// Suite is a basic testing suite with methods for storing and
// retrieving the current *testing.T context.
type addr struct {
Addr uintptr
Method string
}

type addrList []addr

func (a addrList) Len() int {
return len(a)
}

func (a addrList) Less(i, j int) bool {
return a[i].Addr < a[j].Addr
}
func (a addrList) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}

// Suite is the struct to embed in your testing suites
type Suite struct {
*assert.Assertions
require *require.Assertions
Expand Down Expand Up @@ -70,8 +90,18 @@ func Run(t *testing.T, suite TestingSuite) {

methodFinder := reflect.TypeOf(suite)
tests := []testing.InternalTest{}
for index := 0; index < methodFinder.NumMethod(); index++ {
method := methodFinder.Method(index)
methods := addrList{}

// find methods by order in memory
for i := 0; i < methodFinder.NumMethod(); i++ {
method := methodFinder.Method(i)
methods = append(methods, addr{method.Func.Pointer(), method.Name})
}

sort.Sort(methods)

for _, m := range methods {
method, _ := methodFinder.MethodByName(m.Method)
ok, err := methodFilter(method.Name)
if err != nil {
fmt.Fprintf(os.Stderr, "testify: invalid regexp for -m: %s\n", err)
Expand Down
8 changes: 4 additions & 4 deletions suite/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func (suite *SuiteTester) TestOne() {
suite.Equal(suite.TestOneRunCount, beforeCount+1)
}

func (suite *SuiteTester) TestSkip() {
suite.T().Skip()
}

// TestTwo is another example of a test.
func (suite *SuiteTester) TestTwo() {
beforeCount := suite.TestTwoRunCount
Expand All @@ -119,10 +123,6 @@ func (suite *SuiteTester) TestTwo() {
suite.NotEqual(suite.TestTwoRunCount, beforeCount)
}

func (suite *SuiteTester) TestSkip() {
suite.T().Skip()
}

// NonTestMethod does not begin with "Test", so it will not be run by
// testify as a test in the suite. This is useful for creating helper
// methods for your tests.
Expand Down