Skip to content

Commit

Permalink
Add function to return if all tests in a suite passed
Browse files Browse the repository at this point in the history
  • Loading branch information
esdrasbeleza authored and boyan-soubachov committed Mar 11, 2020
1 parent f37e428 commit 961bfee
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
12 changes: 10 additions & 2 deletions suite/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import "time"
// SuiteInformation stats stores stats for the whole suite execution.
type SuiteInformation struct {
Start, End time.Time
Passed bool
TestStats map[string]*TestInformation
}

Expand All @@ -21,7 +20,6 @@ func newSuiteInformation() *SuiteInformation {

return &SuiteInformation{
TestStats: testStats,
Passed: true,
}
}

Expand All @@ -36,3 +34,13 @@ func (s SuiteInformation) end(testName string, passed bool) {
s.TestStats[testName].End = time.Now()
s.TestStats[testName].Passed = passed
}

func (s SuiteInformation) Passed() bool {
for _, stats := range s.TestStats {
if !stats.Passed {
return false
}
}

return true
}
29 changes: 29 additions & 0 deletions suite/stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package suite

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestPassedReturnsTrueWhenAllTestsPass(t *testing.T) {
sinfo := newSuiteInformation()
sinfo.TestStats = map[string]*TestInformation{
"Test1": {TestName: "Test1", Passed: true},
"Test2": {TestName: "Test2", Passed: true},
"Test3": {TestName: "Test3", Passed: true},
}

assert.True(t, sinfo.Passed())
}

func TestPassedReturnsFalseWhenSomeTestFails(t *testing.T) {
sinfo := newSuiteInformation()
sinfo.TestStats = map[string]*TestInformation{
"Test1": {TestName: "Test1", Passed: true},
"Test2": {TestName: "Test2", Passed: false},
"Test3": {TestName: "Test3", Passed: true},
}

assert.False(t, sinfo.Passed())
}
5 changes: 0 additions & 5 deletions suite/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,7 @@ func Run(t *testing.T, suite TestingSuite) {
defer func() {
if stats != nil {
passed := !t.Failed()

stats.end(method.Name, passed)

if !passed {
stats.Passed = false
}
}

if afterTestSuite, ok := suite.(AfterTest); ok {
Expand Down
2 changes: 1 addition & 1 deletion suite/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ func TestSuiteWithStats(t *testing.T) {
assert.True(t, suiteWithStats.wasCalled)
assert.NotZero(t, suiteWithStats.stats.Start)
assert.NotZero(t, suiteWithStats.stats.End)
assert.True(t, suiteWithStats.stats.Passed)
assert.True(t, suiteWithStats.stats.Passed())

testStats := suiteWithStats.stats.TestStats["TestSomething"]
assert.NotZero(t, testStats.Start)
Expand Down

0 comments on commit 961bfee

Please sign in to comment.