OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Package testing provides support for automated testing of Go packages. | 5 // Package testing provides support for automated testing of Go packages. |
6 // It is intended to be used in concert with the ``go test'' command, which auto
mates | 6 // It is intended to be used in concert with the ``go test'' command, which auto
mates |
7 // execution of any function of the form | 7 // execution of any function of the form |
8 // func TestXxx(*testing.T) | 8 // func TestXxx(*testing.T) |
9 // where Xxx can be any alphanumeric string (but the first letter must not be in | 9 // where Xxx can be any alphanumeric string (but the first letter must not be in |
10 // [a-z]) and serves to identify the test routine. | 10 // [a-z]) and serves to identify the test routine. |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 type InternalTest struct { | 369 type InternalTest struct { |
370 Name string | 370 Name string |
371 F func(*T) | 371 F func(*T) |
372 } | 372 } |
373 | 373 |
374 func tRunner(t *T, test *InternalTest) { | 374 func tRunner(t *T, test *InternalTest) { |
375 // When this goroutine is done, either because test.F(t) | 375 // When this goroutine is done, either because test.F(t) |
376 // returned normally or because a test failure triggered | 376 // returned normally or because a test failure triggered |
377 // a call to runtime.Goexit, record the duration and send | 377 // a call to runtime.Goexit, record the duration and send |
378 // a signal saying that the test is done. | 378 // a signal saying that the test is done. |
| 379 var finished bool |
379 defer func() { | 380 defer func() { |
380 t.duration = time.Now().Sub(t.start) | 381 t.duration = time.Now().Sub(t.start) |
381 // If the test panicked, print any test output before dying. | 382 // If the test panicked, print any test output before dying. |
382 » » if err := recover(); err != nil { | 383 » » err := recover() |
| 384 » » if !finished && err == nil { |
| 385 » » » err = fmt.Errorf("test executed panic(nil)") |
| 386 » » } |
| 387 » » if err != nil { |
383 t.Fail() | 388 t.Fail() |
384 t.report() | 389 t.report() |
385 panic(err) | 390 panic(err) |
386 } | 391 } |
387 t.signal <- t | 392 t.signal <- t |
388 }() | 393 }() |
389 | 394 |
390 t.start = time.Now() | 395 t.start = time.Now() |
391 test.F(t) | 396 test.F(t) |
| 397 finished = true |
392 } | 398 } |
393 | 399 |
394 // An internal function but exported because it is cross-package; part of the im
plementation | 400 // An internal function but exported because it is cross-package; part of the im
plementation |
395 // of the "go test" command. | 401 // of the "go test" command. |
396 func Main(matchString func(pat, str string) (bool, error), tests []InternalTest,
benchmarks []InternalBenchmark, examples []InternalExample) { | 402 func Main(matchString func(pat, str string) (bool, error), tests []InternalTest,
benchmarks []InternalBenchmark, examples []InternalExample) { |
397 flag.Parse() | 403 flag.Parse() |
398 parseCpuList() | 404 parseCpuList() |
399 | 405 |
400 before() | 406 before() |
401 startAlarm() | 407 startAlarm() |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
615 if err != nil || cpu <= 0 { | 621 if err != nil || cpu <= 0 { |
616 fmt.Fprintf(os.Stderr, "testing: invalid value %q for -t
est.cpu\n", val) | 622 fmt.Fprintf(os.Stderr, "testing: invalid value %q for -t
est.cpu\n", val) |
617 os.Exit(1) | 623 os.Exit(1) |
618 } | 624 } |
619 cpuList = append(cpuList, cpu) | 625 cpuList = append(cpuList, cpu) |
620 } | 626 } |
621 if cpuList == nil { | 627 if cpuList == nil { |
622 cpuList = append(cpuList, runtime.GOMAXPROCS(-1)) | 628 cpuList = append(cpuList, runtime.GOMAXPROCS(-1)) |
623 } | 629 } |
624 } | 630 } |
OLD | NEW |