@@ -300,14 +300,15 @@ const maxStackLen = 50
300
300
// common holds the elements common between T and B and
301
301
// captures common methods such as Errorf.
302
302
type common struct {
303
- mu sync.RWMutex // guards this group of fields
304
- output []byte // Output generated by test or benchmark.
305
- w io.Writer // For flushToParent.
306
- ran bool // Test or benchmark (or one of its subtests) was executed.
307
- failed bool // Test or benchmark has failed.
308
- skipped bool // Test of benchmark has been skipped.
309
- done bool // Test is finished and all subtests have completed.
310
- helpers map [string ]struct {} // functions to be skipped when writing file/line info
303
+ mu sync.RWMutex // guards this group of fields
304
+ output []byte // Output generated by test or benchmark.
305
+ w io.Writer // For flushToParent.
306
+ ran bool // Test or benchmark (or one of its subtests) was executed.
307
+ failed bool // Test or benchmark has failed.
308
+ expectFail bool // Test is expected to fail.
309
+ skipped bool // Test or benchmark has been skipped.
310
+ done bool // Test is finished and all subtests have completed.
311
+ helpers map [string ]struct {} // functions to be skipped when writing file/line info
311
312
312
313
chatty bool // A copy of the chatty flag.
313
314
finished bool // Test function has completed.
@@ -526,8 +527,8 @@ var _ TB = (*B)(nil)
526
527
//
527
528
// A test ends when its Test function returns or calls any of the methods
528
529
// FailNow, Fatal, Fatalf, SkipNow, Skip, or Skipf. Those methods, as well as
529
- // the Parallel method , must be called only from the goroutine running the
530
- // Test function.
530
+ // the Parallel and ExpectFail methods , must be called only from the goroutine
531
+ // running the Test function.
531
532
//
532
533
// The other reporting methods, such as the variations of Log and Error,
533
534
// may be called simultaneously from multiple goroutines.
@@ -555,7 +556,8 @@ func (c *common) setRan() {
555
556
556
557
// Fail marks the function as having failed but continues execution.
557
558
func (c * common ) Fail () {
558
- if c .parent != nil {
559
+ // Fail the parent if the failure is unexpected.
560
+ if c .parent != nil && ! c .expectFail {
559
561
c .parent .Fail ()
560
562
}
561
563
c .mu .Lock ()
@@ -784,6 +786,22 @@ func (t *T) Parallel() {
784
786
t .raceErrors += - race .Errors ()
785
787
}
786
788
789
+ // ExpectFail signals that this test is expected to fail. The PASS/FAIL result
790
+ // is inverted; passing tests fail, failing tests pass. Use for bug reproduction
791
+ // or documentation of unimplemented features.
792
+ func (t * T ) ExpectFail () {
793
+ t .mu .Lock ()
794
+ defer t .mu .Unlock ()
795
+ t .expectFail = true
796
+ }
797
+
798
+ // unfail resets a test failure. Used when the failure was expected.
799
+ func (t * T ) unfail () {
800
+ t .mu .Lock ()
801
+ defer t .mu .Unlock ()
802
+ t .failed = false
803
+ }
804
+
787
805
// An internal type but exported because it is cross-package; part of the implementation
788
806
// of the "go test" command.
789
807
type InternalTest struct {
@@ -825,9 +843,31 @@ func tRunner(t *T, fn func(t *T)) {
825
843
}
826
844
}
827
845
if err != nil {
828
- t .Fail ()
829
- t .report ()
830
- panic (err )
846
+ if t .expectFail {
847
+ // Set failure, but do not die.
848
+ t .Errorf ("panic: %s" , err )
849
+ } else {
850
+ t .Fail ()
851
+ t .report ()
852
+ panic (err )
853
+ }
854
+ }
855
+
856
+ if t .expectFail {
857
+ if t .failed {
858
+ // Test passes
859
+ t .Log ("expected failure; test failed" )
860
+ t .unfail ()
861
+ } else {
862
+ t .Error ("expected failure; test did not fail" )
863
+ // Fail parent because it was skipped earlier.
864
+ if t .parent != nil {
865
+ t .parent .Fail ()
866
+ }
867
+
868
+ // Increment because it was skipped.
869
+ atomic .AddUint32 (& numFailed , 1 )
870
+ }
831
871
}
832
872
833
873
if len (t .sub ) > 0 {
0 commit comments