Skip to content

fix: could not skip the error when given flag #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type runOption struct {

func newDefaultRunOption() *runOption {
return &runOption{
reporter: runner.NewmemoryTestReporter(),
reporter: runner.NewMemoryTestReporter(),
reportWriter: runner.NewResultWriter(os.Stdout),
}
}
Expand Down Expand Up @@ -206,6 +206,8 @@ func (o *runOption) runSuite(suite string, dataContext map[string]interface{}, c
simpleRunner.WithTestReporter(o.reporter)
if output, err = simpleRunner.RunTestCase(&testCase, dataContext, ctxWithTimeout); err != nil && !o.requestIgnoreError {
return
} else {
err = nil
}
}
dataContext[testCase.Name] = output
Expand Down
5 changes: 5 additions & 0 deletions pkg/runner/reporter_discard.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ func NewDiscardTestReporter() TestReporter {
return &discardTestReporter{}
}

// PutRecord does nothing
func (r *discardTestReporter) PutRecord(*ReportRecord) {}

// GetAllRecords does nothing
func (r *discardTestReporter) GetAllRecords() []*ReportRecord {
return nil
}

// ExportAllReportResults does nothing
func (r *discardTestReporter) ExportAllReportResults() (ReportResultSlice, error) {
return nil, nil
}
10 changes: 8 additions & 2 deletions pkg/runner/reporter_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,30 @@ type memoryTestReporter struct {
records []*ReportRecord
}

// NewmemoryTestReporter creates a memory based test reporter
func NewmemoryTestReporter() TestReporter {
// NewMemoryTestReporter creates a memory based test reporter
func NewMemoryTestReporter() TestReporter {
return &memoryTestReporter{
records: []*ReportRecord{},
}
}

// ReportResultWithTotal holds the total duration base on ReportResult
type ReportResultWithTotal struct {
ReportResult
Total time.Duration
}

// PutRecord puts the record to memory
func (r *memoryTestReporter) PutRecord(record *ReportRecord) {
r.records = append(r.records, record)
}

// GetAllRecords returns all the records
func (r *memoryTestReporter) GetAllRecords() []*ReportRecord {
return r.records
}

// ExportAllReportResults exports all the report results
func (r *memoryTestReporter) ExportAllReportResults() (result ReportResultSlice, err error) {
resultWithTotal := map[string]*ReportResultWithTotal{}
for _, record := range r.records {
Expand Down
91 changes: 91 additions & 0 deletions pkg/runner/reporter_memory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package runner_test

import (
"errors"
"net/http"
"testing"
"time"

"github.com/linuxsuren/api-testing/pkg/runner"
"github.com/stretchr/testify/assert"
)

func TestExportAllReportResults(t *testing.T) {
now := time.Now()

tests := []struct {
name string
records []*runner.ReportRecord
expect runner.ReportResultSlice
}{{
name: "no records",
records: []*runner.ReportRecord{},
expect: nil,
}, {
name: "normal",
records: []*runner.ReportRecord{{
API: "http://foo",
Method: http.MethodGet,
BeginTime: now,
EndTime: now.Add(time.Second * 3),
}, {
API: "http://foo",
Method: http.MethodGet,
BeginTime: now,
EndTime: now.Add(time.Second * 4),
Error: errors.New("fake"),
}, {
API: "http://foo",
Method: http.MethodGet,
BeginTime: now,
EndTime: now.Add(time.Second * 2),
}, {
API: "http://bar",
Method: http.MethodGet,
BeginTime: now,
EndTime: now.Add(time.Second),
}, {
API: "http://fake",
Method: http.MethodGet,
BeginTime: now,
EndTime: now.Add(time.Second * 5),
}},
expect: runner.ReportResultSlice{{
API: "GET http://fake",
Average: time.Second * 5,
Max: time.Second * 5,
Min: time.Second * 5,
Count: 1,
Error: 0,
}, {
API: "GET http://foo",
Average: time.Second * 3,
Max: time.Second * 4,
Min: time.Second * 2,
Count: 3,
Error: 1,
}, {
API: "GET http://bar",
Average: time.Second,
Max: time.Second,
Min: time.Second,
Count: 1,
Error: 0,
}},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reporter := runner.NewMemoryTestReporter()
assert.NotNil(t, reporter)

for i := range tt.records {
reporter.PutRecord(tt.records[i])
}
assert.Equal(t, tt.records, reporter.GetAllRecords())

result, err := reporter.ExportAllReportResults()
assert.Nil(t, err)
assert.Equal(t, tt.expect, result)
})
}
}
16 changes: 14 additions & 2 deletions pkg/runner/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type TestCaseRunner interface {
WithTestReporter(TestReporter) TestCaseRunner
}

// ReportRecord represents the raw data of a HTTP request
type ReportRecord struct {
Method string
API string
Expand All @@ -41,6 +42,7 @@ func (r *ReportRecord) Duration() time.Duration {
return r.EndTime.Sub(r.BeginTime)
}

// ErrorCount returns the count number of errors
func (r *ReportRecord) ErrorCount() int {
if r.Error == nil {
return 0
Expand All @@ -55,6 +57,7 @@ func NewReportRecord() *ReportRecord {
}
}

// ReportResult represents the report result of a set of the same API requests
type ReportResult struct {
API string
Count int
Expand All @@ -64,26 +67,32 @@ type ReportResult struct {
Error int
}

// ReportResultSlice is the alias type of ReportResult slice
type ReportResultSlice []ReportResult

// Len returns the count of slice items
func (r ReportResultSlice) Len() int {
return len(r)
}

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

// Swap swaps the items
func (r ReportResultSlice) Swap(i, j int) {
tmp := r[i]
r[i] = r[j]
r[j] = tmp
}

// ReportResultWriter is the interface of the report writer
type ReportResultWriter interface {
Output([]ReportResult) error
}

// TestReporter is the interface of the report
type TestReporter interface {
PutRecord(*ReportRecord)
GetAllRecords() []*ReportRecord
Expand All @@ -101,12 +110,15 @@ func NewSimpleTestCaseRunner() TestCaseRunner {
return runner.WithOutputWriter(io.Discard).WithTestReporter(NewDiscardTestReporter())
}

// RunTestCase is the main entry point of a test case
func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataContext interface{}, ctx context.Context) (output interface{}, err error) {
fmt.Fprintf(r.writer, "start to run: '%s'\n", testcase.Name)
record := NewReportRecord()
defer func(rr *ReportRecord) {
rr.EndTime = time.Now()
rr.Error = err
rr.API = testcase.Request.API
rr.Method = testcase.Request.Method
r.testReporter.PutRecord(rr)
}(record)

Expand Down Expand Up @@ -163,8 +175,6 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
if request, err = http.NewRequestWithContext(ctx, testcase.Request.Method, testcase.Request.API, requestBody); err != nil {
return
}
record.API = testcase.Request.API
record.Method = testcase.Request.Method

// set headers
for key, val := range testcase.Request.Header {
Expand Down Expand Up @@ -266,11 +276,13 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
return
}

// WithOutputWriter sets the io.Writer
func (r *simpleTestCaseRunner) WithOutputWriter(writer io.Writer) TestCaseRunner {
r.writer = writer
return r
}

// WithTestReporter sets the TestReporter
func (r *simpleTestCaseRunner) WithTestReporter(reporter TestReporter) TestCaseRunner {
r.testReporter = reporter
return r
Expand Down
8 changes: 4 additions & 4 deletions pkg/runner/writer_std.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
_ "embed"
"fmt"
"io"
"os"
"text/template"
)

type stdResultWriter struct {
writer io.Writer
}

// NewResultWriter creates a result writer with the specific io.Writer
func NewResultWriter(writer io.Writer) ReportResultWriter {
return &stdResultWriter{writer: writer}
}
Expand All @@ -22,6 +22,7 @@ func NewDiscardResultWriter() ReportResultWriter {
return &stdResultWriter{writer: io.Discard}
}

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

// NewMarkdownResultWriter creates the Markdown writer
func NewMarkdownResultWriter(writer io.Writer) ReportResultWriter {
if writer == nil {
writer = os.Stdout
}
return &markdownResultWriter{writer: writer}
}

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