Renamed before/after methods and interfaces to setup/teardown

pull/24/head
Samuel Nelson 2013-10-15 10:32:35 -06:00
parent a4c24896e3
commit 11dce72087
3 changed files with 44 additions and 44 deletions

View File

@ -9,26 +9,26 @@ type TestingSuite interface {
SetT(*testing.T)
}
// BeforeAllSuite has a BeforeSuite method, intended to be run before
// the entire suite is tested.
type BeforeAllSuite interface {
BeforeSuite()
// SetupAllSuite has a SetupSuite method, which will run before the
// tests in the suite are run.
type SetupAllSuite interface {
SetupSuite()
}
// BeforeTestSuite has a BeforeTest method, intended to be run before
// each test.
type BeforeTestSuite interface {
BeforeTest()
// SetupTestSuite has a SetupTest method, which will run before each
// test in the suite.
type SetupTestSuite interface {
SetupTest()
}
// AfterAllSuite has an AfterSuite method, intended to be run after the
// entire suite has been tested.
type AfterAllSuite interface {
AfterSuite()
// TearDownAllSuite has a TearDownSuite method, which will run after
// all the tests in the suite have been run.
type TearDownAllSuite interface {
TearDownSuite()
}
// AfterTestSuite has an AfterTest method, intended to be run after
// each test.
type AfterTestSuite interface {
AfterTest()
// TearDownTestSuite has a TearDownTest method, which will run after
// each test in the suite.
type TearDownTestSuite interface {
TearDownTest()
}

View File

@ -27,25 +27,25 @@ func (suite *Suite) SetT(t *testing.T) {
func Run(t *testing.T, suite TestingSuite) {
suite.SetT(t)
if beforeAllSuite, ok := suite.(BeforeAllSuite); ok {
beforeAllSuite.BeforeSuite()
}
if afterAllSuite, ok := suite.(AfterAllSuite); ok {
defer afterAllSuite.AfterSuite()
if setupAllSuite, ok := suite.(SetupAllSuite); ok {
setupAllSuite.SetupSuite()
}
methodFinder := reflect.TypeOf(suite)
for index := 0; index < methodFinder.NumMethod(); index++ {
method := methodFinder.Method(index)
if ok, _ := regexp.MatchString("^Test", method.Name); ok {
if beforeTestSuite, ok := suite.(BeforeTestSuite); ok {
beforeTestSuite.BeforeTest()
if setupTestSuite, ok := suite.(SetupTestSuite); ok {
setupTestSuite.SetupTest()
}
method.Func.Call([]reflect.Value{reflect.ValueOf(suite)})
if afterTestSuite, ok := suite.(AfterTestSuite); ok {
afterTestSuite.AfterTest()
if tearDownTestSuite, ok := suite.(TearDownTestSuite); ok {
tearDownTestSuite.TearDownTest()
}
}
}
if tearDownAllSuite, ok := suite.(TearDownAllSuite); ok {
defer tearDownAllSuite.TearDownSuite()
}
}

View File

@ -9,29 +9,29 @@ import (
// testing-suite-related methods are run.
type SuiteTester struct {
Suite
BeforeSuiteRunCount int
AfterSuiteRunCount int
BeforeTestRunCount int
AfterTestRunCount int
SetupSuiteRunCount int
TearDownSuiteRunCount int
SetupTestRunCount int
TearDownTestRunCount int
TestOneRunCount int
TestTwoRunCount int
NonTestMethodRunCount int
}
func (suite *SuiteTester) BeforeSuite() {
suite.BeforeSuiteRunCount++
func (suite *SuiteTester) SetupSuite() {
suite.SetupSuiteRunCount++
}
func (suite *SuiteTester) AfterSuite() {
suite.AfterSuiteRunCount++
func (suite *SuiteTester) TearDownSuite() {
suite.TearDownSuiteRunCount++
}
func (suite *SuiteTester) BeforeTest() {
suite.BeforeTestRunCount++
func (suite *SuiteTester) SetupTest() {
suite.SetupTestRunCount++
}
func (suite *SuiteTester) AfterTest() {
suite.AfterTestRunCount++
func (suite *SuiteTester) TearDownTest() {
suite.TearDownTestRunCount++
}
func (suite *SuiteTester) TestOne() {
@ -50,16 +50,16 @@ func TestSuiteLogic(t *testing.T) {
suiteTester := new(SuiteTester)
Run(t, suiteTester)
// The suite was only run once, so the BeforeSuite and AfterSuite
// The suite was only run once, so the SetupSuite and TearDownSuite
// methods should have each been run only once.
assert.Equal(t, suiteTester.BeforeSuiteRunCount, 1)
assert.Equal(t, suiteTester.AfterSuiteRunCount, 1)
assert.Equal(t, suiteTester.SetupSuiteRunCount, 1)
assert.Equal(t, suiteTester.TearDownSuiteRunCount, 1)
// There are two test methods (TestOne and TestTwo), so the
// BeforeTest and AfterTest methods (which should be run once for
// SetupTest and TearDownTest methods (which should be run once for
// each test) should have been run twice.
assert.Equal(t, suiteTester.BeforeTestRunCount, 2)
assert.Equal(t, suiteTester.AfterTestRunCount, 2)
assert.Equal(t, suiteTester.SetupTestRunCount, 2)
assert.Equal(t, suiteTester.TearDownTestRunCount, 2)
// Each test should have been run once.
assert.Equal(t, suiteTester.TestOneRunCount, 1)