This is a fork of testify/suite for the Parallel tests support.
You should use the testify if you don't need the parallel tests because this work is not meant to have continuous improvement. It's as it is now, and I am satisfied with the current module.
- testify contributors for a nice and lightweight test helper tools.
- Marcus Loo maroux for the contribution Add support for parallel sub-tests and remove suite.Suite pseudo-interitance #1109
Go code (golang) set of packages that provide many tools for testifying that your code will behave as you intend.
Features include:
Get started:
- Install testify with one line of code, or update it with another
- For an introduction to writing test code in Go, see http://golang.org/doc/code.html#Testing
- Check out the API Documentation http://godoc.org/github.com/denist-huma/testify/v2
- A little about Test-Driven Development (TDD)
suite
package
The suite
package provides functionality that you might be used to from more common object-oriented languages. With it, you can build a testing suite as a struct, build setup/teardown methods and testing methods on your struct, and run them with 'go test' as per normal.
An example suite is shown below:
// Basic imports
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/denist-huma/testify/v2/suite"
)
// Define the suite, which is simply a struct with all
// fields that tests need.
type ExampleTestSuite struct {
VariableThatShouldStartAtFive int
}
// Make sure that VariableThatShouldStartAtFive is set to five
// before each test
func (suite *ExampleTestSuite) SetupTest() {
suite.VariableThatShouldStartAtFive = 5
}
// All methods that begin with "Test" are run as tests within a
// suite.
func (suite *ExampleTestSuite) TestExample(t *suite.T) {
assert.Equal(t, 5, suite.VariableThatShouldStartAtFive)
}
// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestExampleTestSuite(t *testing.T) {
suite.Run(t, new(ExampleTestSuite))
}
For a more complete example, using all of the functionality provided by the suite package, look at our example testing suite
For more information on writing suites, check out the API documentation for the suite
package.
This example TestSuiteParallelSubTests shows that the issue with the parallel tests is solved. You should see that TearDownTest waits until all parallel sub-tests finished.
import (
"testing"
"github.com/denist-huma/testify/v2/suite"
)
func TestSuiteParallelSubTests(t *testing.T) {
suite.Run(t, &Suite{})
}
type Suite struct{}
func (s *Suite) TearDownSuite(t *suite.T) {
t.Log(">> suite tear down")
}
func (s *Suite) TearDownTest(t *suite.T) {
t.Log(">> single test tear down")
}
func (s *Suite) TestOne(t *suite.T) {
for _, v := range []string{"sub1", "sub2", "sub3"} {
t.Run(v, func(t *suite.T) {
t.Parallel()
})
}
}
func (s *Suite) TestTwo(t *suite.T) {
for _, v := range []string{"sub1", "sub2", "sub3"} {
t.Run(v, func(t *suite.T) {
t.Parallel()
})
}
}
The expected test output is below:
$ go test -v .
=== RUN TestSuiteParallelSubTests
=== RUN TestSuiteParallelSubTests/All
=== RUN TestSuiteParallelSubTests/All/TestOne
=== RUN TestSuiteParallelSubTests/All/TestOne/sub1
=== PAUSE TestSuiteParallelSubTests/All/TestOne/sub1
=== RUN TestSuiteParallelSubTests/All/TestOne/sub2
=== PAUSE TestSuiteParallelSubTests/All/TestOne/sub2
=== RUN TestSuiteParallelSubTests/All/TestOne/sub3
=== PAUSE TestSuiteParallelSubTests/All/TestOne/sub3
=== NAME TestSuiteParallelSubTests/All/TestOne
suite.go:64: >> single test tear down
=== CONT TestSuiteParallelSubTests/All/TestOne/sub1
=== CONT TestSuiteParallelSubTests/All/TestOne/sub3
=== CONT TestSuiteParallelSubTests/All/TestOne/sub2
=== RUN TestSuiteParallelSubTests/All/TestTwo
=== RUN TestSuiteParallelSubTests/All/TestTwo/sub1
=== PAUSE TestSuiteParallelSubTests/All/TestTwo/sub1
=== RUN TestSuiteParallelSubTests/All/TestTwo/sub2
=== PAUSE TestSuiteParallelSubTests/All/TestTwo/sub2
=== RUN TestSuiteParallelSubTests/All/TestTwo/sub3
=== PAUSE TestSuiteParallelSubTests/All/TestTwo/sub3
=== NAME TestSuiteParallelSubTests/All/TestTwo
suite.go:64: >> single test tear down
=== CONT TestSuiteParallelSubTests/All/TestTwo/sub1
=== CONT TestSuiteParallelSubTests/All/TestTwo/sub3
=== CONT TestSuiteParallelSubTests/All/TestTwo/sub2
=== NAME TestSuiteParallelSubTests
suite.go:64: >> suite tear down
--- PASS: TestSuiteParallelSubTests (0.00s)
--- PASS: TestSuiteParallelSubTests/All (0.00s)
--- PASS: TestSuiteParallelSubTests/All/TestOne (0.00s)
--- PASS: TestSuiteParallelSubTests/All/TestOne/sub1 (0.00s)
--- PASS: TestSuiteParallelSubTests/All/TestOne/sub3 (0.00s)
--- PASS: TestSuiteParallelSubTests/All/TestOne/sub2 (0.00s)
--- PASS: TestSuiteParallelSubTests/All/TestTwo (0.00s)
--- PASS: TestSuiteParallelSubTests/All/TestTwo/sub1 (0.00s)
--- PASS: TestSuiteParallelSubTests/All/TestTwo/sub3 (0.00s)
--- PASS: TestSuiteParallelSubTests/All/TestTwo/sub2 (0.00s)
PASS
ok github.com/denist-huma/testify/v2 0.005s
To install Testify, use go get
: go get github.com/denist-huma/testify/v2
This will then make the following packages available to you: github.com/denist-huma/testify/v2/suite
To update Testify to the latest version, use go get -u github.com/denist-huma/testify/v2
.
We currently support the most recent major Go versions from 1.13 onward.
there is a problem that
Please feel free to submit issues, fork the repository and send pull requests!
When submitting an issue, we ask that you please include a complete test function that demonstrates the issue. Extra credit for those using Testify to write the test code that demonstrates it.
This project is licensed under the terms of the MIT license.