@@ -8,6 +8,12 @@ type TestingT interface {
88 FailNow ()
99}
1010
11+ // Go's testing.T and other compatible packages provide an additional method, Helper(),
12+ // which indicates that whatever function called it should not be included in stacktraces.
13+ type helperT interface {
14+ Helper ()
15+ }
16+
1117// AssertionScope is a context for executing assertions.
1218type AssertionScope struct {
1319 t TestingT
@@ -54,6 +60,9 @@ func (a AssertionScope) For(name string) AssertionScope {
5460// scope's Errorf method. This logs a failure but does not stop the test.
5561func (a AssertionScope ) Assert (value interface {}, matcher Matcher ) bool {
5662 if pass , desc := matcher .Test (value ); ! pass {
63+ if h , ok := a .t .(helperT ); ok {
64+ h .Helper ()
65+ }
5766 a .fail (desc )
5867 return false
5968 }
@@ -66,6 +75,9 @@ func (a AssertionScope) Assert(value interface{}, matcher Matcher) bool {
6675// the test.
6776func (a AssertionScope ) Require (value interface {}, matcher Matcher ) bool {
6877 if pass , desc := matcher .Test (value ); ! pass {
78+ if h , ok := a .t .(helperT ); ok {
79+ h .Helper ()
80+ }
6981 a .fail (desc )
7082 a .t .FailNow ()
7183 return false // does not return since FailNow() will force an early exit
@@ -74,5 +86,8 @@ func (a AssertionScope) Require(value interface{}, matcher Matcher) bool {
7486}
7587
7688func (a AssertionScope ) fail (desc string ) {
89+ if h , ok := a .t .(helperT ); ok {
90+ h .Helper ()
91+ }
7792 a .t .Errorf ("%s%s" , a .prefix , desc )
7893}
0 commit comments