Skip to content

This is a fork of testify/suite for the parallel test suites support.

License

Notifications You must be signed in to change notification settings

huma-engineering/testify

Repository files navigation

Testify Fork For The Parallel Test Suites

Go Report Card PkgGoDev

This is a fork of testify/suite for the parallel test suites support.

You should use the testify if you don't need the parallel test suites because this work is not meant to have continuous improvement. It's as it is now, and I am satisfied with the current module.

Thanks

Go code (golang) set of packages that provide many tools for testifying that your code will behave as you intend.

Features include:

Get started:

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/huma-engineering/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.

Parallel Test Suites

This example TestSuiteParallelSubTests shows that the issue with the parallel test suite is solved. You should see that suite's TearDownTest waits until all suite's parallel sub-tests finished.

import (
  "testing"

  "github.com/huma-engineering/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/huma-engineering/testify/v2       0.005s

Installation

To install Testify, use go get: go get github.com/huma-engineering/testify/v2

This will then make the following packages available to you: github.com/huma-engineering/testify/v2/suite

Staying up-to-date

To update Testify to the latest version, use go get -u github.com/huma-engineering/testify/v2.

Supported go versions

We currently support the most recent major Go versions from 1.13 onward.

Contributing

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.

License

This project is licensed under the terms of the MIT license.