Skip to content

Commit 5ebb0e9

Browse files
authored
fix: could not skip the error when given flag (#19)
1 parent 078d4c8 commit 5ebb0e9

File tree

6 files changed

+125
-9
lines changed

6 files changed

+125
-9
lines changed

cmd/run.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type runOption struct {
3636

3737
func newDefaultRunOption() *runOption {
3838
return &runOption{
39-
reporter: runner.NewmemoryTestReporter(),
39+
reporter: runner.NewMemoryTestReporter(),
4040
reportWriter: runner.NewResultWriter(os.Stdout),
4141
}
4242
}
@@ -206,6 +206,8 @@ func (o *runOption) runSuite(suite string, dataContext map[string]interface{}, c
206206
simpleRunner.WithTestReporter(o.reporter)
207207
if output, err = simpleRunner.RunTestCase(&testCase, dataContext, ctxWithTimeout); err != nil && !o.requestIgnoreError {
208208
return
209+
} else {
210+
err = nil
209211
}
210212
}
211213
dataContext[testCase.Name] = output

pkg/runner/reporter_discard.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ func NewDiscardTestReporter() TestReporter {
88
return &discardTestReporter{}
99
}
1010

11+
// PutRecord does nothing
1112
func (r *discardTestReporter) PutRecord(*ReportRecord) {}
13+
14+
// GetAllRecords does nothing
1215
func (r *discardTestReporter) GetAllRecords() []*ReportRecord {
1316
return nil
1417
}
18+
19+
// ExportAllReportResults does nothing
1520
func (r *discardTestReporter) ExportAllReportResults() (ReportResultSlice, error) {
1621
return nil, nil
1722
}

pkg/runner/reporter_memory.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,30 @@ type memoryTestReporter struct {
99
records []*ReportRecord
1010
}
1111

12-
// NewmemoryTestReporter creates a memory based test reporter
13-
func NewmemoryTestReporter() TestReporter {
12+
// NewMemoryTestReporter creates a memory based test reporter
13+
func NewMemoryTestReporter() TestReporter {
1414
return &memoryTestReporter{
1515
records: []*ReportRecord{},
1616
}
1717
}
1818

19+
// ReportResultWithTotal holds the total duration base on ReportResult
1920
type ReportResultWithTotal struct {
2021
ReportResult
2122
Total time.Duration
2223
}
2324

25+
// PutRecord puts the record to memory
2426
func (r *memoryTestReporter) PutRecord(record *ReportRecord) {
2527
r.records = append(r.records, record)
2628
}
29+
30+
// GetAllRecords returns all the records
2731
func (r *memoryTestReporter) GetAllRecords() []*ReportRecord {
2832
return r.records
2933
}
34+
35+
// ExportAllReportResults exports all the report results
3036
func (r *memoryTestReporter) ExportAllReportResults() (result ReportResultSlice, err error) {
3137
resultWithTotal := map[string]*ReportResultWithTotal{}
3238
for _, record := range r.records {

pkg/runner/reporter_memory_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package runner_test
2+
3+
import (
4+
"errors"
5+
"net/http"
6+
"testing"
7+
"time"
8+
9+
"github.com/linuxsuren/api-testing/pkg/runner"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestExportAllReportResults(t *testing.T) {
14+
now := time.Now()
15+
16+
tests := []struct {
17+
name string
18+
records []*runner.ReportRecord
19+
expect runner.ReportResultSlice
20+
}{{
21+
name: "no records",
22+
records: []*runner.ReportRecord{},
23+
expect: nil,
24+
}, {
25+
name: "normal",
26+
records: []*runner.ReportRecord{{
27+
API: "http://foo",
28+
Method: http.MethodGet,
29+
BeginTime: now,
30+
EndTime: now.Add(time.Second * 3),
31+
}, {
32+
API: "http://foo",
33+
Method: http.MethodGet,
34+
BeginTime: now,
35+
EndTime: now.Add(time.Second * 4),
36+
Error: errors.New("fake"),
37+
}, {
38+
API: "http://foo",
39+
Method: http.MethodGet,
40+
BeginTime: now,
41+
EndTime: now.Add(time.Second * 2),
42+
}, {
43+
API: "http://bar",
44+
Method: http.MethodGet,
45+
BeginTime: now,
46+
EndTime: now.Add(time.Second),
47+
}, {
48+
API: "http://fake",
49+
Method: http.MethodGet,
50+
BeginTime: now,
51+
EndTime: now.Add(time.Second * 5),
52+
}},
53+
expect: runner.ReportResultSlice{{
54+
API: "GET http://fake",
55+
Average: time.Second * 5,
56+
Max: time.Second * 5,
57+
Min: time.Second * 5,
58+
Count: 1,
59+
Error: 0,
60+
}, {
61+
API: "GET http://foo",
62+
Average: time.Second * 3,
63+
Max: time.Second * 4,
64+
Min: time.Second * 2,
65+
Count: 3,
66+
Error: 1,
67+
}, {
68+
API: "GET http://bar",
69+
Average: time.Second,
70+
Max: time.Second,
71+
Min: time.Second,
72+
Count: 1,
73+
Error: 0,
74+
}},
75+
}}
76+
for _, tt := range tests {
77+
t.Run(tt.name, func(t *testing.T) {
78+
reporter := runner.NewMemoryTestReporter()
79+
assert.NotNil(t, reporter)
80+
81+
for i := range tt.records {
82+
reporter.PutRecord(tt.records[i])
83+
}
84+
assert.Equal(t, tt.records, reporter.GetAllRecords())
85+
86+
result, err := reporter.ExportAllReportResults()
87+
assert.Nil(t, err)
88+
assert.Equal(t, tt.expect, result)
89+
})
90+
}
91+
}

pkg/runner/simple.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type TestCaseRunner interface {
2828
WithTestReporter(TestReporter) TestCaseRunner
2929
}
3030

31+
// ReportRecord represents the raw data of a HTTP request
3132
type 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
4446
func (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
5861
type 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
6771
type ReportResultSlice []ReportResult
6872

73+
// Len returns the count of slice items
6974
func (r ReportResultSlice) Len() int {
7075
return len(r)
7176
}
7277

78+
// Less returns if i bigger than j
7379
func (r ReportResultSlice) Less(i, j int) bool {
7480
return r[i].Average > r[j].Average
7581
}
7682

83+
// Swap swaps the items
7784
func (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
8391
type ReportResultWriter interface {
8492
Output([]ReportResult) error
8593
}
8694

95+
// TestReporter is the interface of the report
8796
type 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
104114
func (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
269280
func (r *simpleTestCaseRunner) WithOutputWriter(writer io.Writer) TestCaseRunner {
270281
r.writer = writer
271282
return r
272283
}
273284

285+
// WithTestReporter sets the TestReporter
274286
func (r *simpleTestCaseRunner) WithTestReporter(reporter TestReporter) TestCaseRunner {
275287
r.testReporter = reporter
276288
return r

pkg/runner/writer_std.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import (
55
_ "embed"
66
"fmt"
77
"io"
8-
"os"
98
"text/template"
109
)
1110

1211
type stdResultWriter struct {
1312
writer io.Writer
1413
}
1514

15+
// NewResultWriter creates a result writer with the specific io.Writer
1616
func NewResultWriter(writer io.Writer) ReportResultWriter {
1717
return &stdResultWriter{writer: writer}
1818
}
@@ -22,6 +22,7 @@ func NewDiscardResultWriter() ReportResultWriter {
2222
return &stdResultWriter{writer: io.Discard}
2323
}
2424

25+
// Output writer the report to target writer
2526
func (w *stdResultWriter) Output(result []ReportResult) error {
2627
fmt.Fprintf(w.writer, "API Average Max Min Count Error\n")
2728
for _, r := range result {
@@ -35,13 +36,12 @@ type markdownResultWriter struct {
3536
writer io.Writer
3637
}
3738

39+
// NewMarkdownResultWriter creates the Markdown writer
3840
func NewMarkdownResultWriter(writer io.Writer) ReportResultWriter {
39-
if writer == nil {
40-
writer = os.Stdout
41-
}
4241
return &markdownResultWriter{writer: writer}
4342
}
4443

44+
// Output writer the Markdown based report to target writer
4545
func (w *markdownResultWriter) Output(result []ReportResult) (err error) {
4646
var tpl *template.Template
4747
if tpl, err = template.New("report").Parse(markDownReport); err == nil {

0 commit comments

Comments
 (0)