Skip to content

Commit

Permalink
replace Skip with SkipUntil to prevent tests being forgotten forever
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed May 25, 2022
1 parent b62f1e6 commit efb81b1
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 70 deletions.
9 changes: 1 addition & 8 deletions Spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
37 changes: 1 addition & 36 deletions Spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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{})
Expand Down
13 changes: 13 additions & 0 deletions T.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testcase

import (
"fmt"
"math/rand"
"testing"
"time"
Expand Down Expand Up @@ -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)))
}
38 changes: 38 additions & 0 deletions T_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package testcase_test

import (
"context"
"fmt"
"math/rand"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -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)))
})
}
26 changes: 0 additions & 26 deletions example_Spec_Skip_test.go

This file was deleted.

21 changes: 21 additions & 0 deletions example_T_SkipUntil_test.go
Original file line number Diff line number Diff line change
@@ -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) {})
}

0 comments on commit efb81b1

Please sign in to comment.