gotest.tools/gotestsum@v1.11.0/cmd/testdata/e2e/flaky/flaky_test.go (about) 1 // +build testdata 2 3 package flaky 4 5 import ( 6 "fmt" 7 "io/ioutil" 8 "os" 9 "strconv" 10 "sync" 11 "testing" 12 ) 13 14 var seed int 15 var seedfile = seedFile() 16 var once = new(sync.Once) 17 18 func setup(t *testing.T) { 19 once.Do(func() { 20 raw, err := ioutil.ReadFile(seedfile) 21 if err != nil { 22 t.Fatalf("failed to read seed: %v", err) 23 } 24 n, err := strconv.ParseInt(string(raw), 10, 64) 25 if err != nil { 26 t.Fatalf("failed to parse seed: %v", err) 27 } 28 seed = int(n) 29 30 err = ioutil.WriteFile(seedfile, []byte(strconv.Itoa(seed+1)), 0644) 31 if err != nil { 32 t.Fatalf("failed to write seed: %v", err) 33 } 34 }) 35 fmt.Fprintln(os.Stderr, "SEED: ", seed) 36 } 37 38 func seedFile() string { 39 if name, ok := os.LookupEnv("TEST_SEEDFILE"); ok { 40 return name 41 } 42 return "/tmp/gotestsum-flaky-seedfile" 43 } 44 45 func TestAlwaysPasses(t *testing.T) { 46 } 47 48 func TestFailsRarely(t *testing.T) { 49 setup(t) 50 if seed%20 != 1 { 51 t.Fatal("not this time") 52 } 53 } 54 55 func TestFailsSometimes(t *testing.T) { 56 setup(t) 57 if seed%4 != 2 { 58 t.Fatal("not this time") 59 } 60 } 61 62 func TestFailsOften(t *testing.T) { 63 setup(t) 64 65 t.Run("subtest always passes", func(t *testing.T) {}) 66 t.Run("subtest may fail", func(t *testing.T) { 67 if seed%20 != 6 { 68 t.Fatal("not this time") 69 } 70 }) 71 } 72 73 func TestFailsOftenDoesNotPrefixMatch(t *testing.T) {} 74 75 func TestFailsSometimesDoesNotPrefixMatch(t *testing.T) {}