diff --git a/Spec.go b/Spec.go index fbf2147..04d5c3c 100644 --- a/Spec.go +++ b/Spec.go @@ -204,12 +204,6 @@ func (spec *Spec) Sequential() { sequential().setup(spec) } -// Skip is equivalent to Log followed by SkipNow on T for each test case. -func (spec *Spec) Skip(args ...interface{}) { - spec.testingTB.Helper() - spec.Before(func(t *T) { t.TB.Skip(args...) }) -} - var acceptedConstKind = map[reflect.Kind]struct{}{ reflect.String: {}, reflect.Bool: {}, @@ -418,7 +412,7 @@ func (spec *Spec) runB(b *testing.B, blk func(*T)) { b.Helper() t := newT(b, spec) if _, ok := spec.lookupRetryFlaky(); ok { - b.Skip(`skipping because retry`) + b.Skip(`skipping because flaky flag`) } benchCase := func() { b.StopTimer() @@ -428,7 +422,6 @@ func (spec *Spec) runB(b *testing.B, blk func(*T)) { defer b.StopTimer() blk(t) } - b.ResetTimer() for i := 0; i < b.N; i++ { benchCase() diff --git a/Spec_test.go b/Spec_test.go index 9e8e038..2a68b52 100644 --- a/Spec_test.go +++ b/Spec_test.go @@ -830,41 +830,6 @@ func TestSpec_Sequential_callingItAfterContextDeclarationYieldPanic(t *testing.T assert.Must(t).Panic(func() { s.HasSideEffect() }) } -func TestSpec_Skip(t *testing.T) { - var out []int - - t.Run(``, func(t *testing.T) { - s := testcase.NewSpec(t) - s.Sequential() - - s.Context(`skipped ones`, func(s *testcase.Spec) { - s.Skip(`WIP or something like that`) - - s.Test(`will be skipped`, func(t *testcase.T) { - out = append(out, 0) - }) - - s.Test(`will be skipped as well`, func(t *testcase.T) { - out = append(out, 1) - }) - - s.Context(`skipped as well just like parent tests`, func(s *testcase.Spec) { - - s.Test(`will be skipped`, func(t *testcase.T) { - out = append(out, 0) - }) - - }) - }) - - s.Test(`will run`, func(t *testcase.T) { - out = append(out, 42) - }) - }) - - assert.Must(t).Equal([]int{42}, out) -} - func TestSpec_panicDoNotLeakOutFromTestingScope(t *testing.T) { var noPanic bool func() { @@ -1184,7 +1149,7 @@ func TestSpec_Parallel_testPrepareActionsExecutedInParallel(t *testing.T) { } func TestSpec_executionOrder(t *testing.T) { - t.Skip(`WIP`) + t.Skip(`SkipUntil`) t.Run(`Non parallel testCase will run in randomized order`, func(t *testing.T) { rnd := random.New(random.CryptoSeed{}) diff --git a/T.go b/T.go index 0ed9d97..2e6dc66 100644 --- a/T.go +++ b/T.go @@ -1,6 +1,7 @@ package testcase import ( + "fmt" "math/rand" "testing" "time" @@ -192,3 +193,15 @@ func (t *T) pauseTimer() func() { btm.StartTimer() } } + +// SkipUntil is equivalent to Log followed by SkipNow if the test is executing prior to the given deadline time. +// 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. +func (t *T) SkipUntil(year int, month time.Month, day int) { + t.TB.Helper() + const skipTimeFormat = "2006-01-02" + date := time.Date(year, month, day, 0, 0, 0, 0, time.Local) + if date.Before(time.Now()) { + t.TB.Fatalf("Skip expired on %s", date.Format(skipTimeFormat)) + } + t.TB.Skip(fmt.Sprintf("Skip time %s", date.Format(skipTimeFormat))) +} diff --git a/T_test.go b/T_test.go index 136a36d..49465ef 100644 --- a/T_test.go +++ b/T_test.go @@ -2,7 +2,9 @@ package testcase_test import ( "context" + "fmt" "math/rand" + "strings" "sync" "testing" "time" @@ -451,3 +453,39 @@ func BenchmarkT_varDoesNotCountTowardsRun(b *testing.B) { _ = bv.Get(t) }) } + +func TestT_SkipUntil(t *testing.T) { + const timeLayout = "2006-01-02" + const skipUntilFormat = "Skip time %s" + const skipExpiredFormat = "Skip expired on %s" + rnd := random.New(rand.NewSource(time.Now().UnixNano())) + future := time.Now().AddDate(0, 0, 1) + t.Run("before SkipUntil deadline, test is skipped", func(t *testing.T) { + stubTB := &testcase.StubTB{} + s := testcase.NewSpec(stubTB) + var ran bool + s.Test("", func(t *testcase.T) { + t.SkipUntil(future.Year(), future.Month(), future.Day()) + ran = true + }) + internal.Recover(func() { s.Finish() }) + assert.Must(t).False(ran) + assert.Must(t).False(stubTB.IsFailed) + assert.Must(t).True(stubTB.IsSkipped) + assert.Must(t).Contain(strings.Join(stubTB.Logs, "\n"), fmt.Sprintf(skipUntilFormat, future.Format(timeLayout))) + }) + t.Run("at or after SkipUntil deadline, test is failed", func(t *testing.T) { + stubTB := &testcase.StubTB{} + s := testcase.NewSpec(stubTB) + today := time.Now().AddDate(0, 0, -1*rnd.IntN(3)) + var ran bool + s.Test("", func(t *testcase.T) { + t.SkipUntil(today.Year(), today.Month(), today.Day()) + ran = true + }) + internal.Recover(func() { s.Finish() }) + assert.Must(t).False(ran) + assert.Must(t).True(stubTB.IsFailed) + assert.Must(t).Contain(strings.Join(stubTB.Logs, "\n"), fmt.Sprintf(skipExpiredFormat, today.Format(timeLayout))) + }) +} diff --git a/example_Spec_Skip_test.go b/example_Spec_Skip_test.go deleted file mode 100644 index 90ccddd..0000000 --- a/example_Spec_Skip_test.go +++ /dev/null @@ -1,26 +0,0 @@ -package testcase_test - -import ( - "testing" - - "github.com/adamluzsi/testcase" -) - -func ExampleSpec_Skip() { - var t *testing.T - s := testcase.NewSpec(t) - - s.Context(`sub spec`, func(s *testcase.Spec) { - s.Skip(`WIP`) - - s.Test(`will be skipped`, func(t *testcase.T) {}) - - s.Test(`will be skipped as well`, func(t *testcase.T) {}) - - s.Context(`skipped as well just like the tests of the parent`, func(s *testcase.Spec) { - s.Test(`will be skipped`, func(t *testcase.T) {}) - }) - }) - - s.Test(`this will still run since it is not part of the scope where Spec#Skip was called`, func(t *testcase.T) {}) -} diff --git a/example_T_SkipUntil_test.go b/example_T_SkipUntil_test.go new file mode 100644 index 0000000..2d349d4 --- /dev/null +++ b/example_T_SkipUntil_test.go @@ -0,0 +1,21 @@ +package testcase_test + +import ( + "testing" + + "github.com/adamluzsi/testcase" +) + +func ExampleT_SkipUntil() { + var t *testing.T + s := testcase.NewSpec(t) + + s.Test(`will be skipped`, func(t *testcase.T) { + // make tests skip until the given day is reached, + // then make the tests fail. + // This helps to commit code which still work in progress. + t.SkipUntil(2020, 01, 01) + }) + + s.Test(`will not be skipped`, func(t *testcase.T) {}) +}