feat: support to generate json format report (#98)
This commit is contained in:
parent
6b83420f23
commit
ca1a69cf3f
|
@ -78,7 +78,7 @@ See also https://github.com/LinuxSuRen/api-testing/tree/master/sample`,
|
|||
flags.DurationVarP(&opt.duration, "duration", "", 0, "Running duration")
|
||||
flags.DurationVarP(&opt.requestTimeout, "request-timeout", "", time.Minute, "Timeout for per request")
|
||||
flags.BoolVarP(&opt.requestIgnoreError, "request-ignore-error", "", false, "Indicate if ignore the request error")
|
||||
flags.StringVarP(&opt.report, "report", "", "", "The type of target report. Supported: markdown, md, html, discard, std")
|
||||
flags.StringVarP(&opt.report, "report", "", "", "The type of target report. Supported: markdown, md, html, json, discard, std")
|
||||
flags.StringVarP(&opt.reportFile, "report-file", "", "", "The file path of the report")
|
||||
flags.BoolVarP(&opt.reportIgnore, "report-ignore", "", false, "Indicate if ignore the report output")
|
||||
flags.StringVarP(&opt.swaggerURL, "swagger-url", "", "", "The URL of swagger")
|
||||
|
@ -105,6 +105,8 @@ func (o *runOption) preRunE(cmd *cobra.Command, args []string) (err error) {
|
|||
o.reportWriter = runner.NewMarkdownResultWriter(writer)
|
||||
case "html":
|
||||
o.reportWriter = runner.NewHTMLResultWriter(writer)
|
||||
case "json":
|
||||
o.reportWriter = runner.NewJSONResultWriter(writer)
|
||||
case "discard":
|
||||
o.reportWriter = runner.NewDiscardResultWriter()
|
||||
case "", "std":
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
package runner
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/linuxsuren/api-testing/pkg/apispec"
|
||||
"io"
|
||||
)
|
||||
|
||||
type jsonResultWriter struct {
|
||||
writer io.Writer
|
||||
}
|
||||
|
||||
// NewJSONResultWriter creates a new jsonResultWriter
|
||||
func NewJSONResultWriter(writer io.Writer) ReportResultWriter {
|
||||
return &jsonResultWriter{writer: writer}
|
||||
}
|
||||
|
||||
// Output writes the JSON base report to target writer
|
||||
func (w *jsonResultWriter) Output(result []ReportResult) (err error) {
|
||||
jsonData, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = fmt.Fprint(w.writer, string(jsonData))
|
||||
return
|
||||
}
|
||||
|
||||
// WithAPIConverage sets the api coverage
|
||||
func (w *jsonResultWriter) WithAPIConverage(apiConverage apispec.APIConverage) ReportResultWriter {
|
||||
return w
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package runner_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/linuxsuren/api-testing/pkg/runner"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestJSONResultWriter(t *testing.T) {
|
||||
buf := new(bytes.Buffer)
|
||||
writer := runner.NewJSONResultWriter(buf)
|
||||
writer.WithAPIConverage(nil)
|
||||
|
||||
err := writer.Output([]runner.ReportResult{{
|
||||
API: "api",
|
||||
Average: 3,
|
||||
Max: 4,
|
||||
Min: 2,
|
||||
Count: 3,
|
||||
Error: 0,
|
||||
}, {
|
||||
API: "api",
|
||||
Average: 3,
|
||||
Max: 4,
|
||||
Min: 2,
|
||||
Count: 3,
|
||||
Error: 0,
|
||||
}})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t,
|
||||
"[{\"API\":\"api\",\"Count\":3,\"Average\":3,\"Max\":4,\"Min\":2,\"QPS\":0,\"Error\":0,\"LastErrorMessage\":\"\"},{\"API\":\"api\",\"Count\":3,\"Average\":3,\"Max\":4,\"Min\":2,\"QPS\":0,\"Error\":0,\"LastErrorMessage\":\"\"}]",
|
||||
buf.String())
|
||||
}
|
Loading…
Reference in New Issue