// [a-z]) and serves to identify the test routine.
// These TestXxx routines should be declared within the package they are testing.
//
-// Tests may be skipped if not applicable like this:
+// Tests and benchmarks may be skipped if not applicable like this:
// func TestTimeConsuming(t *testing.T) {
// if testing.Short() {
// t.Skip("skipping test in short mode.")
// common holds the elements common between T and B and
// captures common methods such as Errorf.
type common struct {
- mu sync.RWMutex // guards output and failed
- output []byte // Output generated by test or benchmark.
- failed bool // Test or benchmark has failed.
+ mu sync.RWMutex // guards output and failed
+ output []byte // Output generated by test or benchmark.
+ failed bool // Test or benchmark has failed.
+ skipped bool // Test of benchmark has been skipped.
start time.Time // Time test or benchmark started
duration time.Duration
common
name string // Name of test.
startParallel chan bool // Parallel tests will wait on this.
- skipped bool // Test has been skipped.
}
// Fail marks the function as having failed but continues execution.
c.FailNow()
}
+// Skip is equivalent to Log followed by SkipNow.
+func (c *common) Skip(args ...interface{}) {
+ c.log(fmt.Sprintln(args...))
+ c.SkipNow()
+}
+
+// Skipf is equivalent to Logf followed by SkipNow.
+func (c *common) Skipf(format string, args ...interface{}) {
+ c.log(fmt.Sprintf(format, args...))
+ c.SkipNow()
+}
+
+// SkipNow marks the test as having been skipped and stops its execution.
+// Execution will continue at the next test or benchmark. See also FailNow.
+// SkipNow must be called from the goroutine running the test, not from
+// other goroutines created during the test. Calling SkipNow does not stop
+// those other goroutines.
+func (c *common) SkipNow() {
+ c.skip()
+ runtime.Goexit()
+}
+
+func (c *common) skip() {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ c.skipped = true
+}
+
+// Skipped reports whether the test was skipped.
+func (c *common) Skipped() bool {
+ c.mu.RLock()
+ defer c.mu.RUnlock()
+ return c.skipped
+}
+
// Parallel signals that this test is to be run in parallel with (and only with)
// other parallel tests.
func (t *T) Parallel() {
}
}
-// Skip is equivalent to Log followed by SkipNow.
-func (t *T) Skip(args ...interface{}) {
- t.log(fmt.Sprintln(args...))
- t.SkipNow()
-}
-
-// Skipf is equivalent to Logf followed by SkipNow.
-func (t *T) Skipf(format string, args ...interface{}) {
- t.log(fmt.Sprintf(format, args...))
- t.SkipNow()
-}
-
-// SkipNow marks the test as having been skipped and stops its execution.
-// Execution will continue at the next test or benchmark. See also FailNow.
-// SkipNow must be called from the goroutine running the test, not from
-// other goroutines created during the test. Calling SkipNow does not stop
-// those other goroutines.
-func (t *T) SkipNow() {
- t.skip()
- runtime.Goexit()
-}
-
-func (t *T) skip() {
- t.mu.Lock()
- defer t.mu.Unlock()
- t.skipped = true
-}
-
-// Skipped reports whether the test was skipped.
-func (t *T) Skipped() bool {
- t.mu.RLock()
- defer t.mu.RUnlock()
- return t.skipped
-}
-
func RunTests(matchString func(pat, str string) (bool, error), tests []InternalTest) (ok bool) {
ok = true
if len(tests) == 0 && !haveExamples {