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
Conversation
@ichiban The problem is you've overridden the |
Thank you for your reply, @devdinu! I know that My original problem was applying package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_original(t *testing.T) {
assert := assert.New(t)
// lots of assertions
assert.True(true)
assert.True(true)
assert.True(true)
{ // subtest A
assert.True(true)
}
{ // subtest B
assert.True(true)
}
}
func Test_rename(t *testing.T) {
asser := assert.New(t)
// lots of assertions
asser.True(true)
asser.True(true)
asser.True(true)
t.Run("subtest A", func(t *testing.T) {
assert := assert.New(t)
assert.True(true)
})
t.Run("subtest B", func(t *testing.T) {
assert := assert.New(t)
assert.True(true)
})
}
func Test_package(t *testing.T) {
// lots of assertions
assert.True(t, true)
assert.True(t, true)
assert.True(t, true)
t.Run("subtest A", func(t *testing.T) {
assert := assert.New(t)
assert.True(true)
})
t.Run("subtest B", func(t *testing.T) {
assert := assert.New(t)
assert.True(true)
})
}
func Test_new(t *testing.T) {
assert := assert.New(t)
// lots of assertions
assert.True(true)
assert.True(true)
assert.True(true)
t.Run("subtest A", func(t *testing.T) {
assert := assert.New(t)
assert.True(true)
})
t.Run("subtest B", func(t *testing.T) {
assert := assert.New(t)
assert.True(true)
})
} |
I'm curious why do you need to do |
@devdinu Good point! I'd do so if I wrote tests from scratch but I have existing assertions in the form of |
@@ -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 { |
There was a problem hiding this comment.
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)
})
}
There was a problem hiding this comment.
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.
👍 for I'm closing this PR and I have opened #1446. |
Currently, you can't redeclare
assert := assert.New(t)
in subtests.This PR enables it by defining constructor method.