Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to run SetupTest in table tests using test suite? #1069

Open
paymog opened this issue Apr 17, 2021 · 2 comments
Open

How to run SetupTest in table tests using test suite? #1069

paymog opened this issue Apr 17, 2021 · 2 comments

Comments

@paymog
Copy link

paymog commented Apr 17, 2021

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?

@viteksafronov
Copy link

I think that changing the current behavior of SetupTest/AfterTest/BeforeTest may (and will) lead to some unpredictable problems. So if someone really needs hooks to set something up in the subtest lifecycle, it's better to add new types with interfaces like SetupSubTest, AfterSubTest, BeforeSubTest.
At the same time, I don't get how it may be profitable.

@qerdcv
Copy link
Contributor

qerdcv commented Jan 11, 2023

I think it can be closed after release

#1246

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants