Skip to content

Commit

Permalink
make sure that tests in a spec suite are always grouped together
Browse files Browse the repository at this point in the history
This improves readability and reduces the need for repetitive code
when integrating a suite into a top-level spec.
  • Loading branch information
adamluzsi committed Oct 13, 2024
1 parent 7a6962a commit 031cb25
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -833,15 +833,18 @@ type SpecSuite struct {
}

func (suite SpecSuite) Name() string { return suite.N }
func (suite SpecSuite) Spec(s *Spec) { suite.S.Spec(s) }

func (suite SpecSuite) Spec(s *Spec) {
s.Context(suite.N, suite.S.Spec, Group(suite.N))
}

func (suite SpecSuite) Test(t *testing.T) { suite.run(t) }
func (suite SpecSuite) Benchmark(b *testing.B) { suite.run(b) }

func (suite SpecSuite) run(tb testing.TB) {
s := NewSpec(tb)
defer s.Finish()
s.Context(suite.N, suite.Spec, Group(suite.N))
suite.Spec(s)
}

func helper(tb testingHelper) testingHelper {
Expand Down
57 changes: 57 additions & 0 deletions Spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -1730,3 +1731,59 @@ func TestSpecSuite(t *testing.T) {
assert.Equal(t, ran, ir+1)
})
}

func TestSpecSuite_name(t *testing.T) {
t.Run("when suite used directly", func(t *testing.T) {
var ran int32
t.Run("begin", func(t *testing.T) {
suite := func() testcase.Suite {
s := testcase.NewSpec(nil)
s.Describe("#ok", func(s *testcase.Spec) {
s.Test("nok", func(t *testcase.T) {
atomic.AddInt32(&ran, 1)
assert.Contain(t, t.Name(), "/begin/suite1/#ok/nok")
})
})
return s.AsSuite("suite1")
}()
s := testcase.NewSpec(t)
s.Test("foo", func(t *testcase.T) {
atomic.AddInt32(&ran, 1)
assert.Contain(t, t.Name(), "/begin/foo")
})
suite.Spec(s)
s.Test("bar", func(t *testcase.T) {
atomic.AddInt32(&ran, 1)
assert.Contain(t, t.Name(), "/begin/bar")
})
})
assert.Equal(t, atomic.LoadInt32(&ran), 3)
})

t.Run("when suite used as context block", func(t *testing.T) {
var ran int32
t.Run("begin", func(t *testing.T) {
suite := func() testcase.Suite {
s := testcase.NewSpec(nil)
s.Describe("#ok", func(s *testcase.Spec) {
s.Test("nok", func(t *testcase.T) {
atomic.AddInt32(&ran, 1)
assert.Contain(t, t.Name(), "/begin/2suite/suite2/#ok/nok")
})
})
return s.AsSuite("suite2")
}()
s := testcase.NewSpec(t)
s.Test("foo", func(t *testcase.T) {
atomic.AddInt32(&ran, 1)
assert.Contain(t, t.Name(), "/begin/foo")
})
s.Describe("2suite", suite.Spec)
s.Test("bar", func(t *testcase.T) {
atomic.AddInt32(&ran, 1)
assert.Contain(t, t.Name(), "/begin/bar")
})
})
assert.Equal(t, atomic.LoadInt32(&ran), 3)
})
}

0 comments on commit 031cb25

Please sign in to comment.