Skip to content

Commit

Permalink
Added TestSuiteWithFailfast
Browse files Browse the repository at this point in the history
  • Loading branch information
AdRiley authored and boyan-soubachov committed May 4, 2020
1 parent cd58006 commit 93bea66
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ script:
- ./.travis.gofmt.sh
- ./.travis.govet.sh
- go test -v -race ./...
- go test -v -race -failfast -run TestSuiteWithFailfast
59 changes: 59 additions & 0 deletions suite/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package suite

import (
"errors"
"flag"
"io/ioutil"
"math/rand"
"os"
Expand Down Expand Up @@ -512,3 +513,61 @@ func TestSuiteWithStats(t *testing.T) {
assert.NotZero(t, testStats.End)
assert.True(t, testStats.Passed)
}

// Suite to test behavior when running with the failfast flag
// It logs calls in the callOrder slice which we then use to assert the correct calls were made
type FailfastSuite struct {
Suite
callOrder []string
}

func (s *FailfastSuite) call(method string) {
s.callOrder = append(s.callOrder, method)
}

func TestSuiteWithFailfast(t *testing.T) {
// This test suite is run twice by travis. Once normally and once with the -failfast flag
failFast := flag.Lookup("test.failfast").Value.(flag.Getter).Get().(bool)
s := new(FailfastSuite)
ok := testing.RunTests(
allTestsFilter,
[]testing.InternalTest{{
Name: "TestSuiteWithFailfast",
F: func(t *testing.T) {
Run(t, s)
},
}},
)
assert.Equal(t, false, ok)
if failFast {
// Test A Fails and because we are running with failfast Test B never runs and we proceed staright to TearDownSuite
assert.Equal(t, "SetupSuite;SetupTest;Test A Fails;TearDownTest;TearDownSuite", strings.Join(s.callOrder, ";"))
} else {
// Test A Fails and because we are running without failfast we continue and run Test B and then proceed to TearDownSuite
assert.Equal(t, "SetupSuite;SetupTest;Test A Fails;TearDownTest;SetupTest;Test B Passes;TearDownTest;TearDownSuite", strings.Join(s.callOrder, ";"))
}
}
func (s *FailfastSuite) SetupSuite() {
s.call("SetupSuite")
}

func (s *FailfastSuite) TearDownSuite() {
s.call("TearDownSuite")
}
func (s *FailfastSuite) SetupTest() {
s.call("SetupTest")
}

func (s *FailfastSuite) TearDownTest() {
s.call("TearDownTest")
}

func (s *FailfastSuite) Test_A_Fails() {
s.call("Test A Fails")
s.Require().True(false)
}

func (s *FailfastSuite) Test_B_Passes() {
s.call("Test B Passes")
s.Require().True(true)
}

0 comments on commit 93bea66

Please sign in to comment.