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

Add a Run helper method #510

Closed
wants to merge 2 commits into from
Closed

Conversation

JelteF
Copy link

@JelteF JelteF commented Oct 12, 2017

I love table tests, especially when using Run. And this adds that functionality to the suite library.

Copy link
Member

@ernesto-jimenez ernesto-jimenez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might have some race conditions.

It would be good to have some accompanying tests too.

@@ -55,6 +55,21 @@ func (suite *Suite) Assert() *assert.Assertions {
return suite.Assertions
}

// Run is a helper method to do a simple subtest. It sets `T()` to the
// `*testing.T` of the subtest.
func (suite *Suite) Run(name string, f func()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JelteF could the name be confusing considering we already have suite.Run?


defer func() {
suite.SetT(oldT)
}()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this result in race conditions if somebody called suite.T().Parallel() within the subtest?

suite.SetT(oldT)
}()

f()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than receiving an empty func(), maybe we should receive func(*Suite) and send a test suite with the new *testingT?

Would that prevent race conditions?

@JelteF
Copy link
Author

JelteF commented Dec 30, 2017 via email

@ernesto-jimenez
Copy link
Member

@JelteF parallel suite tests are currently unsupported because, but I'm unsure what the situation would be with subtests and whether we would be able to support parallel suite tests in the future.

The following might help with that:

func (s *Suite) RunSubtest(name string, fn fun(*Suite))

Where the suite sent is a different one with the new *testing.T.

What do you think?

@JelteF
Copy link
Author

JelteF commented Dec 30, 2017

First off, did you mean to close the PR?

Regarding the proposed change, I don't think I really like it. Mainly because I wouldn't have access to the helper methods I have on my custom Suite type.

Renaming it to RunSubtest is fine by me though if you think that causes less confusion.

@ernesto-jimenez
Copy link
Member

Hey, sorry, clicked the wrong button.

You could implement this:

// RunSubtest receives the name of the subtest and a function taking a single argument of a struct that embeds `Suite`. The subtest will be called with a new instance of the type provided in the argument with the subtest’s `*testing.T`
func (s *Suite) RunSubtest(name string, fn interface{})

Then, using reflection you would set things up. We do something similar in the mock.MatchedBy.

Even if running tests in parallel is currently broken, it would be good to fix that bug rather than relying on the bug.

@JelteF
Copy link
Author

JelteF commented Feb 1, 2018

Would it be okay if I add a RunSubtest and RunSubtestParallel, where the RunSubtestParallel also calls T().Parallel(). Because I especially like the implementation I made for non parallel test, because it's very straightforward to use.

@Antonboom
Copy link

@brackendawson
Copy link
Collaborator

Apparently not.

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

Successfully merging this pull request may close these issues.

4 participants