Skip to content

Commit 60e2a1d

Browse files
committed
feat: support saving markdown report to a file
1 parent 54b1194 commit 60e2a1d

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

cmd/run.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"context"
55
"fmt"
6+
"io"
67
"os"
78
"path"
89
"path/filepath"
@@ -30,6 +31,7 @@ type runOption struct {
3031
limiter limit.RateLimiter
3132
startTime time.Time
3233
reporter runner.TestReporter
34+
reportFile string
3335
reportWriter runner.ReportResultWriter
3436
report string
3537
reportIgnore bool
@@ -72,17 +74,27 @@ See also https://github.com/LinuxSuRen/api-testing/tree/master/sample`,
7274
flags.DurationVarP(&opt.duration, "duration", "", 0, "Running duration")
7375
flags.DurationVarP(&opt.requestTimeout, "request-timeout", "", time.Minute, "Timeout for per request")
7476
flags.BoolVarP(&opt.requestIgnoreError, "request-ignore-error", "", false, "Indicate if ignore the request error")
77+
flags.StringVarP(&opt.report, "report", "", "", "The type of target report. Supported: markdown, md, discard, std")
78+
flags.StringVarP(&opt.reportFile, "report-file", "", "", "The file path of the report")
7579
flags.BoolVarP(&opt.reportIgnore, "report-ignore", "", false, "Indicate if ignore the report output")
7680
flags.Int64VarP(&opt.thread, "thread", "", 1, "Threads of the execution")
7781
flags.Int32VarP(&opt.qps, "qps", "", 5, "QPS")
7882
flags.Int32VarP(&opt.burst, "burst", "", 5, "burst")
79-
flags.StringVarP(&opt.report, "report", "", "", "The type of target report. Supported: markdown, md, discard, std")
8083
return
8184
}
8285

8386
func (o *runOption) preRunE(cmd *cobra.Command, args []string) (err error) {
8487
writer := cmd.OutOrStdout()
8588

89+
if o.reportFile != "" {
90+
var reportFile *os.File
91+
if reportFile, err = os.Create(o.reportFile); err != nil {
92+
return
93+
}
94+
95+
writer = io.MultiWriter(writer, reportFile)
96+
}
97+
8698
switch o.report {
8799
case "markdown", "md":
88100
o.reportWriter = runner.NewMarkdownResultWriter(writer)

cmd/run_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"context"
66
"errors"
77
"net/http"
8+
"os"
9+
"path"
810
"testing"
911
"time"
1012

@@ -66,6 +68,13 @@ func TestRunCommand(t *testing.T) {
6668
fooPrepare := func() {
6769
gock.New(urlFoo).Get("/bar").Reply(http.StatusOK).JSON("{}")
6870
}
71+
tmpFile, err := os.CreateTemp(os.TempDir(), "api-testing")
72+
if !assert.Nil(t, err) {
73+
return
74+
}
75+
defer func() {
76+
_ = os.Remove(tmpFile.Name())
77+
}()
6978

7079
tests := []struct {
7180
name string
@@ -101,12 +110,23 @@ func TestRunCommand(t *testing.T) {
101110
name: "invalid schema",
102111
args: []string{"-p", "testdata/invalid-schema.yaml"},
103112
hasErr: true,
113+
}, {
114+
name: "report file",
115+
prepare: fooPrepare,
116+
args: []string{"-p", simpleSuite, "--report", "md", "--report-file", tmpFile.Name()},
117+
hasErr: false,
118+
}, {
119+
name: "report file with error",
120+
prepare: fooPrepare,
121+
args: []string{"-p", simpleSuite, "--report", "md", "--report-file", path.Join(tmpFile.Name(), "fake")},
122+
hasErr: true,
104123
}}
105124
for _, tt := range tests {
106125
t.Run(tt.name, func(t *testing.T) {
107126
defer gock.Clean()
108127
util.MakeSureNotNil(tt.prepare)()
109128
root := &cobra.Command{Use: "root"}
129+
root.SetOut(&bytes.Buffer{})
110130
root.AddCommand(createRunCommand())
111131

112132
root.SetArgs(append([]string{"run"}, tt.args...))

0 commit comments

Comments
 (0)