Skip to content

Commit efb81b1

Browse files
committed
replace Skip with SkipUntil to prevent tests being forgotten forever
1 parent b62f1e6 commit efb81b1

File tree

6 files changed

+74
-70
lines changed

6 files changed

+74
-70
lines changed

Spec.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,6 @@ func (spec *Spec) Sequential() {
204204
sequential().setup(spec)
205205
}
206206

207-
// Skip is equivalent to Log followed by SkipNow on T for each test case.
208-
func (spec *Spec) Skip(args ...interface{}) {
209-
spec.testingTB.Helper()
210-
spec.Before(func(t *T) { t.TB.Skip(args...) })
211-
}
212-
213207
var acceptedConstKind = map[reflect.Kind]struct{}{
214208
reflect.String: {},
215209
reflect.Bool: {},
@@ -418,7 +412,7 @@ func (spec *Spec) runB(b *testing.B, blk func(*T)) {
418412
b.Helper()
419413
t := newT(b, spec)
420414
if _, ok := spec.lookupRetryFlaky(); ok {
421-
b.Skip(`skipping because retry`)
415+
b.Skip(`skipping because flaky flag`)
422416
}
423417
benchCase := func() {
424418
b.StopTimer()
@@ -428,7 +422,6 @@ func (spec *Spec) runB(b *testing.B, blk func(*T)) {
428422
defer b.StopTimer()
429423
blk(t)
430424
}
431-
432425
b.ResetTimer()
433426
for i := 0; i < b.N; i++ {
434427
benchCase()

Spec_test.go

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -830,41 +830,6 @@ func TestSpec_Sequential_callingItAfterContextDeclarationYieldPanic(t *testing.T
830830
assert.Must(t).Panic(func() { s.HasSideEffect() })
831831
}
832832

833-
func TestSpec_Skip(t *testing.T) {
834-
var out []int
835-
836-
t.Run(``, func(t *testing.T) {
837-
s := testcase.NewSpec(t)
838-
s.Sequential()
839-
840-
s.Context(`skipped ones`, func(s *testcase.Spec) {
841-
s.Skip(`WIP or something like that`)
842-
843-
s.Test(`will be skipped`, func(t *testcase.T) {
844-
out = append(out, 0)
845-
})
846-
847-
s.Test(`will be skipped as well`, func(t *testcase.T) {
848-
out = append(out, 1)
849-
})
850-
851-
s.Context(`skipped as well just like parent tests`, func(s *testcase.Spec) {
852-
853-
s.Test(`will be skipped`, func(t *testcase.T) {
854-
out = append(out, 0)
855-
})
856-
857-
})
858-
})
859-
860-
s.Test(`will run`, func(t *testcase.T) {
861-
out = append(out, 42)
862-
})
863-
})
864-
865-
assert.Must(t).Equal([]int{42}, out)
866-
}
867-
868833
func TestSpec_panicDoNotLeakOutFromTestingScope(t *testing.T) {
869834
var noPanic bool
870835
func() {
@@ -1184,7 +1149,7 @@ func TestSpec_Parallel_testPrepareActionsExecutedInParallel(t *testing.T) {
11841149
}
11851150

11861151
func TestSpec_executionOrder(t *testing.T) {
1187-
t.Skip(`WIP`)
1152+
t.Skip(`SkipUntil`)
11881153

11891154
t.Run(`Non parallel testCase will run in randomized order`, func(t *testing.T) {
11901155
rnd := random.New(random.CryptoSeed{})

T.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package testcase
22

33
import (
4+
"fmt"
45
"math/rand"
56
"testing"
67
"time"
@@ -192,3 +193,15 @@ func (t *T) pauseTimer() func() {
192193
btm.StartTimer()
193194
}
194195
}
196+
197+
// SkipUntil is equivalent to Log followed by SkipNow if the test is executing prior to the given deadline time.
198+
// SkipUntil is useful when you need to skip something temporarily, but you don't trust your memory enough to return to it on your own.
199+
func (t *T) SkipUntil(year int, month time.Month, day int) {
200+
t.TB.Helper()
201+
const skipTimeFormat = "2006-01-02"
202+
date := time.Date(year, month, day, 0, 0, 0, 0, time.Local)
203+
if date.Before(time.Now()) {
204+
t.TB.Fatalf("Skip expired on %s", date.Format(skipTimeFormat))
205+
}
206+
t.TB.Skip(fmt.Sprintf("Skip time %s", date.Format(skipTimeFormat)))
207+
}

T_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package testcase_test
22

33
import (
44
"context"
5+
"fmt"
56
"math/rand"
7+
"strings"
68
"sync"
79
"testing"
810
"time"
@@ -451,3 +453,39 @@ func BenchmarkT_varDoesNotCountTowardsRun(b *testing.B) {
451453
_ = bv.Get(t)
452454
})
453455
}
456+
457+
func TestT_SkipUntil(t *testing.T) {
458+
const timeLayout = "2006-01-02"
459+
const skipUntilFormat = "Skip time %s"
460+
const skipExpiredFormat = "Skip expired on %s"
461+
rnd := random.New(rand.NewSource(time.Now().UnixNano()))
462+
future := time.Now().AddDate(0, 0, 1)
463+
t.Run("before SkipUntil deadline, test is skipped", func(t *testing.T) {
464+
stubTB := &testcase.StubTB{}
465+
s := testcase.NewSpec(stubTB)
466+
var ran bool
467+
s.Test("", func(t *testcase.T) {
468+
t.SkipUntil(future.Year(), future.Month(), future.Day())
469+
ran = true
470+
})
471+
internal.Recover(func() { s.Finish() })
472+
assert.Must(t).False(ran)
473+
assert.Must(t).False(stubTB.IsFailed)
474+
assert.Must(t).True(stubTB.IsSkipped)
475+
assert.Must(t).Contain(strings.Join(stubTB.Logs, "\n"), fmt.Sprintf(skipUntilFormat, future.Format(timeLayout)))
476+
})
477+
t.Run("at or after SkipUntil deadline, test is failed", func(t *testing.T) {
478+
stubTB := &testcase.StubTB{}
479+
s := testcase.NewSpec(stubTB)
480+
today := time.Now().AddDate(0, 0, -1*rnd.IntN(3))
481+
var ran bool
482+
s.Test("", func(t *testcase.T) {
483+
t.SkipUntil(today.Year(), today.Month(), today.Day())
484+
ran = true
485+
})
486+
internal.Recover(func() { s.Finish() })
487+
assert.Must(t).False(ran)
488+
assert.Must(t).True(stubTB.IsFailed)
489+
assert.Must(t).Contain(strings.Join(stubTB.Logs, "\n"), fmt.Sprintf(skipExpiredFormat, today.Format(timeLayout)))
490+
})
491+
}

example_Spec_Skip_test.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

example_T_SkipUntil_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package testcase_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/adamluzsi/testcase"
7+
)
8+
9+
func ExampleT_SkipUntil() {
10+
var t *testing.T
11+
s := testcase.NewSpec(t)
12+
13+
s.Test(`will be skipped`, func(t *testcase.T) {
14+
// make tests skip until the given day is reached,
15+
// then make the tests fail.
16+
// This helps to commit code which still work in progress.
17+
t.SkipUntil(2020, 01, 01)
18+
})
19+
20+
s.Test(`will not be skipped`, func(t *testcase.T) {})
21+
}

0 commit comments

Comments
 (0)