@@ -28,6 +28,7 @@ type TestCaseRunner interface {
2828	WithTestReporter (TestReporter ) TestCaseRunner 
2929}
3030
31+ // ReportRecord represents the raw data of a HTTP request 
3132type  ReportRecord  struct  {
3233	Method     string 
3334	API        string 
@@ -41,6 +42,7 @@ func (r *ReportRecord) Duration() time.Duration {
4142	return  r .EndTime .Sub (r .BeginTime )
4243}
4344
45+ // ErrorCount returns the count number of errors 
4446func  (r  * ReportRecord ) ErrorCount () int  {
4547	if  r .Error  ==  nil  {
4648		return  0 
@@ -55,6 +57,7 @@ func NewReportRecord() *ReportRecord {
5557	}
5658}
5759
60+ // ReportResult represents the report result of a set of the same API requests 
5861type  ReportResult  struct  {
5962	API      string 
6063	Count    int 
@@ -64,26 +67,32 @@ type ReportResult struct {
6467	Error    int 
6568}
6669
70+ // ReportResultSlice is the alias type of ReportResult slice 
6771type  ReportResultSlice  []ReportResult 
6872
73+ // Len returns the count of slice items 
6974func  (r  ReportResultSlice ) Len () int  {
7075	return  len (r )
7176}
7277
78+ // Less returns if i bigger than j 
7379func  (r  ReportResultSlice ) Less (i , j  int ) bool  {
7480	return  r [i ].Average  >  r [j ].Average 
7581}
7682
83+ // Swap swaps the items 
7784func  (r  ReportResultSlice ) Swap (i , j  int ) {
7885	tmp  :=  r [i ]
7986	r [i ] =  r [j ]
8087	r [j ] =  tmp 
8188}
8289
90+ // ReportResultWriter is the interface of the report writer 
8391type  ReportResultWriter  interface  {
8492	Output ([]ReportResult ) error 
8593}
8694
95+ // TestReporter is the interface of the report 
8796type  TestReporter  interface  {
8897	PutRecord (* ReportRecord )
8998	GetAllRecords () []* ReportRecord 
@@ -101,12 +110,15 @@ func NewSimpleTestCaseRunner() TestCaseRunner {
101110	return  runner .WithOutputWriter (io .Discard ).WithTestReporter (NewDiscardTestReporter ())
102111}
103112
113+ // RunTestCase is the main entry point of a test case 
104114func  (r  * simpleTestCaseRunner ) RunTestCase (testcase  * testing.TestCase , dataContext  interface {}, ctx  context.Context ) (output  interface {}, err  error ) {
105115	fmt .Fprintf (r .writer , "start to run: '%s'\n " , testcase .Name )
106116	record  :=  NewReportRecord ()
107117	defer  func (rr  * ReportRecord ) {
108118		rr .EndTime  =  time .Now ()
109119		rr .Error  =  err 
120+ 		rr .API  =  testcase .Request .API 
121+ 		rr .Method  =  testcase .Request .Method 
110122		r .testReporter .PutRecord (rr )
111123	}(record )
112124
@@ -163,8 +175,6 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
163175	if  request , err  =  http .NewRequestWithContext (ctx , testcase .Request .Method , testcase .Request .API , requestBody ); err  !=  nil  {
164176		return 
165177	}
166- 	record .API  =  testcase .Request .API 
167- 	record .Method  =  testcase .Request .Method 
168178
169179	// set headers 
170180	for  key , val  :=  range  testcase .Request .Header  {
@@ -266,11 +276,13 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
266276	return 
267277}
268278
279+ // WithOutputWriter sets the io.Writer 
269280func  (r  * simpleTestCaseRunner ) WithOutputWriter (writer  io.Writer ) TestCaseRunner  {
270281	r .writer  =  writer 
271282	return  r 
272283}
273284
285+ // WithTestReporter sets the TestReporter 
274286func  (r  * simpleTestCaseRunner ) WithTestReporter (reporter  TestReporter ) TestCaseRunner  {
275287	r .testReporter  =  reporter 
276288	return  r 
0 commit comments