Skip to content
Open
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
21 changes: 9 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,9 @@ import (
"github.com/stretchr/testify/suite"
)

// Define the suite, and absorb the built-in basic suite
// functionality from testify - including a T() method which
// returns the current testing context
// Define the suite, which is simply a struct with all
// fields that tests need.
type ExampleTestSuite struct {
suite.Suite
VariableThatShouldStartAtFive int
}

Expand All @@ -227,8 +225,8 @@ func (suite *ExampleTestSuite) SetupTest() {

// All methods that begin with "Test" are run as tests within a
// suite.
func (suite *ExampleTestSuite) TestExample() {
assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive)
func (suite *ExampleTestSuite) TestExample(t *suite.T) {
assert.Equal(t, 5, suite.VariableThatShouldStartAtFive)
}

// In order for 'go test' to run this suite, we need to create
Expand All @@ -251,23 +249,22 @@ import (
"github.com/stretchr/testify/suite"
)

// Define the suite, and absorb the built-in basic suite
// functionality from testify - including assertion methods.
// Define the suite, which is simply a struct with all
// fields that tests need.
type ExampleTestSuite struct {
suite.Suite
VariableThatShouldStartAtFive int
}

// Make sure that VariableThatShouldStartAtFive is set to five
// before each test
func (suite *ExampleTestSuite) SetupTest() {
func (suite *ExampleTestSuite) SetupTest(t *suite.T) {
suite.VariableThatShouldStartAtFive = 5
}

// All methods that begin with "Test" are run as tests within a
// suite.
func (suite *ExampleTestSuite) TestExample() {
suite.Equal(suite.VariableThatShouldStartAtFive, 5)
func (suite *ExampleTestSuite) TestExample(t *suite.T) {
t.Equal(5, suite.VariableThatShouldStartAtFive)
}

// In order for 'go test' to run this suite, we need to create
Expand Down
24 changes: 11 additions & 13 deletions suite/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
// or individual tests (depending on which interface(s) you
// implement).
//
// A testing suite is usually built by first extending the built-in
// suite functionality from suite.Suite in testify. Alternatively,
// you could reproduce that logic on your own if you wanted (you
// just need to implement the TestingSuite interface from
// suite/interfaces.go).
// A testing suite is usually built by defining a Suite struct
// that includes all fields that tests need.
//
// After that, you can implement any of the interfaces in
// suite/interfaces.go to add setup/teardown functionality to your
// suite, and add any methods that start with "Test" to add tests.
// Test methods must match signature: `func(*suite.T)`. The suite.T
// object passed may be used to run sub-tests, verify assertions
// and control test execution.
// Methods that do not match any suite interfaces and do not begin
// with "Test" will not be run by testify, and can safely be used as
// helper methods.
Expand All @@ -36,25 +36,23 @@
// "github.com/stretchr/testify/suite"
// )
//
// // Define the suite, and absorb the built-in basic suite
// // functionality from testify - including a T() method which
// // returns the current testing context
// // Define the suite, which is simply a struct with all
// // fields that tests need.
// type ExampleTestSuite struct {
// suite.Suite
// VariableThatShouldStartAtFive int
// }
//
// // Make sure that VariableThatShouldStartAtFive is set to five
// // before each test
// func (suite *ExampleTestSuite) SetupTest() {
// func (suite *ExampleTestSuite) SetupTest(t *suite.T) {
// suite.VariableThatShouldStartAtFive = 5
// }
//
// // All methods that begin with "Test" are run as tests within a
// // suite.
// func (suite *ExampleTestSuite) TestExample() {
// assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive)
// suite.Equal(5, suite.VariableThatShouldStartAtFive)
// func (suite *ExampleTestSuite) TestExample(t *suite.T) {
// assert.Equal(t, 5, suite.VariableThatShouldStartAtFive)
// t.Equal(5, suite.VariableThatShouldStartAtFive)
// }
//
// // In order for 'go test' to run this suite, we need to create
Expand Down
41 changes: 28 additions & 13 deletions suite/interfaces.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,68 @@
package suite

import "testing"
import (
"testing"
"time"
)

// TestingSuite can store and return the current *testing.T context
// generated by 'go test'.
type TestingSuite interface {
T() *testing.T
SetT(*testing.T)
// *testing.T interface for < go1.15
type testingT interface {
testing.TB

Parallel()
Run(name string, f func(t *testing.T)) bool
}

// *testing.T interface for go1.15
type testingT115 interface {
TempDir() string
Deadline() (deadline time.Time, ok bool)
}

// *testing.T interface for go1.17
type testingT117 interface {
Setenv(key, value string)
}

// SetupAllSuite has a SetupSuite method, which will run before the
// tests in the suite are run.
type SetupAllSuite interface {
SetupSuite()
SetupSuite(t *T)
}

// SetupTestSuite has a SetupTest method, which will run before each
// test in the suite.
type SetupTestSuite interface {
SetupTest()
SetupTest(t *T)
}

// TearDownAllSuite has a TearDownSuite method, which will run after
// all the tests in the suite have been run.
type TearDownAllSuite interface {
TearDownSuite()
TearDownSuite(t *T)
}

// TearDownTestSuite has a TearDownTest method, which will run after
// each test in the suite.
type TearDownTestSuite interface {
TearDownTest()
TearDownTest(t *T)
}

// BeforeTest has a function to be executed right before the test
// starts and receives the suite and test names as input
type BeforeTest interface {
BeforeTest(suiteName, testName string)
BeforeTest(t *T, suiteName, testName string)
}

// AfterTest has a function to be executed right after the test
// finishes and receives the suite and test names as input
type AfterTest interface {
AfterTest(suiteName, testName string)
AfterTest(t *T, suiteName, testName string)
}

// WithStats implements HandleStats, a function that will be executed
// when a test suite is finished. The stats contain information about
// the execution of that suite and its tests.
type WithStats interface {
HandleStats(suiteName string, stats *SuiteInformation)
HandleStats(t *T, suiteName string, stats *SuiteInformation)
}
Loading