go.undefinedlabs.com/scopeagent@v0.4.2/runner/runner_test.go (about)

     1  package runner
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  var (
    10  	okCount     = 0
    11  	failCount   = 0
    12  	errorCount  = 0
    13  	flakyCount  = 0
    14  	failSubTest = 0
    15  )
    16  
    17  func TestMain(m *testing.M) {
    18  	Run(m, Options{
    19  		FailRetries: 4,
    20  		PanicAsFail: true,
    21  		Logger:      nil,
    22  		OnPanic: func(t *testing.T, err interface{}) {
    23  			fmt.Printf("the test '%s' has paniked with error: %s", t.Name(), err)
    24  		},
    25  	})
    26  	fmt.Println(okCount, failCount, errorCount, flakyCount, failSubTest)
    27  	if okCount != 1 {
    28  		panic("TestOk ran an unexpected number of times")
    29  	}
    30  	if failCount != 5 {
    31  		panic("TestFail ran an unexpected number of times")
    32  	}
    33  	if errorCount != 5 {
    34  		panic("TestError ran an unexpected number of times")
    35  	}
    36  	if flakyCount != 3 {
    37  		panic("TestFlaky ran an unexpected number of times")
    38  	}
    39  	if failCount != 5 {
    40  		panic("TestFailSubTest ran an unexpected number of times")
    41  	}
    42  }
    43  
    44  func TestOk(t *testing.T) {
    45  	if GetOriginalTestName(t.Name()) != "TestOk" {
    46  		t.Fatal("test name is invalid.")
    47  	}
    48  	okCount++
    49  	t.Log("Ok")
    50  }
    51  
    52  func TestFail(t *testing.T) {
    53  	if GetOriginalTestName(t.Name()) != "TestFail" {
    54  		t.Fatal("test name is invalid.")
    55  	}
    56  	failCount++
    57  	t.Fatal("Fail")
    58  }
    59  
    60  func TestError(t *testing.T) {
    61  	if GetOriginalTestName(t.Name()) != "TestError" {
    62  		t.Fatal("test name is invalid.")
    63  	}
    64  	errorCount++
    65  	panic("this is a panic")
    66  }
    67  
    68  func TestFlaky(t *testing.T) {
    69  	if GetOriginalTestName(t.Name()) != "TestFlaky" {
    70  		t.Fatal("test name is invalid.")
    71  	}
    72  	flakyCount++
    73  	if flakyCount != 3 {
    74  		t.Fatal("this is flaky")
    75  	}
    76  }
    77  
    78  func TestFailSubTest(t *testing.T) {
    79  	t.Run("SubTest", func(t *testing.T) {
    80  		if GetOriginalTestName(t.Name()) != "TestFailSubTest/SubTest" {
    81  			t.Fatal("test name is invalid.")
    82  		}
    83  
    84  		t.Run("SubSub", func(t *testing.T) {
    85  			if GetOriginalTestName(t.Name()) != "TestFailSubTest/SubTest/SubSub" {
    86  				t.Fatal("test name is invalid.")
    87  			}
    88  		})
    89  
    90  		failSubTest++
    91  		t.Fatal("Subtest fail")
    92  	})
    93  }
    94  
    95  func TestParallelPass(t *testing.T) {
    96  	t.Parallel()
    97  
    98  	for i := 0; i < 10; i++ {
    99  		t.Run("child", func(t *testing.T) {
   100  			t.Parallel()
   101  		})
   102  	}
   103  
   104  	<-time.After(1 * time.Second)
   105  }
   106  
   107  func TestParallelFail(t *testing.T) {
   108  	t.Parallel()
   109  
   110  	for i := 0; i < 10; i++ {
   111  		t.Run("child", func(t *testing.T) {
   112  			t.Parallel()
   113  		})
   114  	}
   115  
   116  	<-time.After(1 * time.Second)
   117  	t.FailNow()
   118  }
   119  
   120  func TestParallelPanic(t *testing.T) {
   121  	t.Parallel()
   122  
   123  	for i := 0; i < 10; i++ {
   124  		t.Run("child", func(t *testing.T) {
   125  			t.Parallel()
   126  		})
   127  	}
   128  
   129  	<-time.After(1 * time.Second)
   130  	panic("forced parallel test panic")
   131  }