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 constructor to assertions object to smooth out subtests #603

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions assert/forward_assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ func New(t TestingT) *Assertions {
}
}

// New makes a new Assertions object for the subtest TestingT.
func (a *Assertions) New(t TestingT) *Assertions {
Copy link
Member

Choose a reason for hiding this comment

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

This seems confusing to me.

how about:

func (a *Assertions) Run(name string, fn func(*Assertions)) {
 	a.t.Run(name, func(t *testing.T) {
 	 	a := New(t)
 	 	fn(a)
 	})
}

Copy link
Author

Choose a reason for hiding this comment

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

Hi @ernesto-jimenez, thank you for the great idea! It looks compelling and I can easily imagine myself writing like:

func Test_run(t *testing.T) {
	assert := assert.New(t)

	assert.Run("subtest", func(assert *assert.Assertions) { // Wait, package `assert` is shadowed
		assert.True(true)
	})
}

Oh wait, *assert.Assertions will be shadowed if I keep declaring assert := assert.New(t). Also, a.t is a TestingT instead of *testing.T which doesn't have Run() method.

return New(t)
}

//go:generate go run ../_codegen/main.go -output-package=assert -template=assertion_forward.go.tmpl -include-format-funcs