How to run SetupTest in table tests using test suite?
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 32/100
Research direction
Start at the testify suite package and the TestUnitTestSuite entry point shown in the issue; reproduce the table-test lifecycle with the provided example. Trace how suite.Run and s.Run invoke setup hooks, then document the supported behavior or identify the needed test coverage so the per-case lifecycle is unambiguous.
Written by the indexing model from the issue text.
Description
I've set up a suite for my tests. However, I'm having trouble using the set up and tear down features when using table tests. Is this by design?
package workflows
import (
"testing"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/suite"
)
type UnitTestSuite struct {
suite.Suite
}
func (s *UnitTestSuite) SetupTest() {
log.Info("setup")
}
func (s *UnitTestSuite) BeforeTest(suiteName, testName string) {
log.Info("before test")
}
func (s *UnitTestSuite) AfterTest(suiteName, testName string) {
log.Info("After test")
}
func (s *UnitTestSuite) Test_TableTest() {
type testCase struct {
name string
}
testCases := []testCase{
{
name: "1",
},
{
name: "2",
},
}
for _, testCase := range testCases {
s.Run(testCase.name, func() {
// logic ...
// NOTE that the SetupTest and BeforeTest do not get called for each test here
})
}
}
func TestUnitTestSuite(t *testing.T) {
suite.Run(t, new(UnitTestSuite))
}
When I run the TestUnitTestSuite I get the following output:
=== RUN TestUnitTestSuite
--- PASS: TestUnitTestSuite (0.00s)
=== RUN TestUnitTestSuite/Test_TableTest
time="2021-04-17T07:49:28-04:00" level=info msg=setup
time="2021-04-17T07:49:28-04:00" level=info msg="before test"
--- PASS: TestUnitTestSuite/Test_TableTest (0.00s)
=== RUN TestUnitTestSuite/Test_TableTest/1
--- PASS: TestUnitTestSuite/Test_TableTest/1 (0.00s)
=== RUN TestUnitTestSuite/Test_TableTest/2
time="2021-04-17T07:49:28-04:00" level=info msg="After test"
--- PASS: TestUnitTestSuite/Test_TableTest/2 (0.00s)
PASS
Note that setup and before test appear only once in the output even though there are two tests being run.
Is there a way for me to automatically run SetupTest (or some alternative) prior to each of my table tests?
- Dominant language
- Go
- Stars
- 26.2k
- Forks
- 1.9k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 2
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from stretchr/testify
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 88/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
internal/cleanup
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
All issues in stretchr/testify
Similar issues
-
optimization optimization:agents-md-curator
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
githubnext/gh-aw-cao#13143 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
blinklabs-io/bursa#904 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
yanet-platform/ipfw-go#129 ·
-
bug confmap/provider/googlesecretmanagerprovider needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
open-telemetry/opentelemetry-collector-contrib#51273 · 2 comments ·
-
bug: AI Gateway client filter lists "Unknown" twice when NULL and literal Unknown clients coexist Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 90/100