@@ -79,10 +79,13 @@ var (
79
79
runLogger = logging .DefaultLogger (logging .LogLevelInfo ).WithName ("run" )
80
80
)
81
81
82
- func newDefaultRunOption () * runOption {
82
+ func newDefaultRunOption (output io.Writer ) * runOption {
83
+ if output == nil {
84
+ output = os .Stdout
85
+ }
83
86
return & runOption {
84
87
reporter : runner .NewMemoryTestReporter (nil , "" ),
85
- reportWriter : runner .NewResultWriter (os . Stdout ),
88
+ reportWriter : runner .NewResultWriter (output ),
86
89
loader : testing .NewFileLoader (),
87
90
githubReportOption : & runner.GithubPRCommentOption {},
88
91
}
@@ -97,7 +100,7 @@ func newDiscardRunOption() *runOption {
97
100
98
101
// createRunCommand returns the run command
99
102
func createRunCommand () (cmd * cobra.Command ) {
100
- opt := newDefaultRunOption ()
103
+ opt := newDefaultRunOption (nil )
101
104
cmd = & cobra.Command {
102
105
Use : "run" ,
103
106
Aliases : []string {"r" },
@@ -111,29 +114,33 @@ See also https://github.com/LinuxSuRen/api-testing/tree/master/sample`,
111
114
112
115
// set flags
113
116
flags := cmd .Flags ()
114
- flags .StringVarP (& opt .pattern , "pattern" , "p" , "test-suite-*.yaml" ,
115
- "The file pattern which try to execute the test cases. Brace expansion is supported, such as: test-suite-{1,2}.yaml" )
116
- flags .StringVarP (& opt .level , "level" , "l" , "info" , "Set the output log level" )
117
- flags .DurationVarP (& opt .duration , "duration" , "" , 0 , "Running duration" )
118
- flags .DurationVarP (& opt .requestTimeout , "request-timeout" , "" , time .Minute , "Timeout for per request" )
119
- flags .BoolVarP (& opt .requestIgnoreError , "request-ignore-error" , "" , false , "Indicate if ignore the request error" )
120
- flags .StringArrayVarP (& opt .caseFilter , "case-filter" , "" , nil , "The filter of the test case" )
121
- flags .StringVarP (& opt .report , "report" , "" , "" , "The type of target report. Supported: markdown, md, html, json, discard, std, prometheus, http, grpc" )
122
- flags .StringVarP (& opt .reportFile , "report-file" , "" , "" , "The file path of the report" )
123
- flags .BoolVarP (& opt .reportIgnore , "report-ignore" , "" , false , "Indicate if ignore the report output" )
124
- flags .StringVarP (& opt .reportTemplate , "report-template" , "" , "" , "The template used to render the report" )
125
- flags .StringVarP (& opt .reportDest , "report-dest" , "" , "" , "The server url where you want to send the report" )
126
- flags .StringVarP (& opt .swaggerURL , "swagger-url" , "" , "" , "The URL of swagger" )
127
- flags .Int64VarP (& opt .thread , "thread" , "" , 1 , "Threads of the execution" )
128
- flags .Int32VarP (& opt .qps , "qps" , "" , 5 , "QPS" )
129
- flags .Int32VarP (& opt .burst , "burst" , "" , 5 , "burst" )
130
- flags .StringVarP (& opt .monitorDocker , "monitor-docker" , "" , "" , "The docker container name to monitor" )
117
+ opt .addFlags (flags )
131
118
addGitHubReportFlags (flags , opt .githubReportOption )
132
119
return
133
120
}
134
121
135
122
const caseFilter = "case-filter"
136
123
124
+ func (o * runOption ) addFlags (flags * pflag.FlagSet ) {
125
+ flags .StringVarP (& o .pattern , "pattern" , "p" , "test-suite-*.yaml" ,
126
+ "The file pattern which try to execute the test cases. Brace expansion is supported, such as: test-suite-{1,2}.yaml" )
127
+ flags .StringVarP (& o .level , "level" , "l" , "info" , "Set the output log level" )
128
+ flags .DurationVarP (& o .duration , "duration" , "" , 0 , "Running duration" )
129
+ flags .DurationVarP (& o .requestTimeout , "request-timeout" , "" , time .Minute , "Timeout for per request" )
130
+ flags .BoolVarP (& o .requestIgnoreError , "request-ignore-error" , "" , false , "Indicate if ignore the request error" )
131
+ flags .StringArrayVarP (& o .caseFilter , "case-filter" , "" , nil , "The filter of the test case" )
132
+ flags .StringVarP (& o .report , "report" , "" , "" , "The type of target report. Supported: markdown, md, html, json, discard, std, prometheus, http, grpc" )
133
+ flags .StringVarP (& o .reportFile , "report-file" , "" , "" , "The file path of the report" )
134
+ flags .BoolVarP (& o .reportIgnore , "report-ignore" , "" , false , "Indicate if ignore the report output" )
135
+ flags .StringVarP (& o .reportTemplate , "report-template" , "" , "" , "The template used to render the report" )
136
+ flags .StringVarP (& o .reportDest , "report-dest" , "" , "" , "The server url where you want to send the report" )
137
+ flags .StringVarP (& o .swaggerURL , "swagger-url" , "" , "" , "The URL of swagger" )
138
+ flags .Int64VarP (& o .thread , "thread" , "" , 1 , "Threads of the execution" )
139
+ flags .Int32VarP (& o .qps , "qps" , "" , 5 , "QPS" )
140
+ flags .Int32VarP (& o .burst , "burst" , "" , 5 , "burst" )
141
+ flags .StringVarP (& o .monitorDocker , "monitor-docker" , "" , "" , "The docker container name to monitor" )
142
+ }
143
+
137
144
func (o * runOption ) preRunE (cmd * cobra.Command , args []string ) (err error ) {
138
145
ctx := cmd .Context ()
139
146
if ctx == nil {
@@ -148,6 +155,9 @@ func (o *runOption) preRunE(cmd *cobra.Command, args []string) (err error) {
148
155
return
149
156
}
150
157
158
+ defer func () {
159
+ _ = reportFile .Close ()
160
+ }()
151
161
writer = io .MultiWriter (writer , reportFile )
152
162
}
153
163
@@ -354,7 +364,7 @@ func (o *runOption) runSuite(loader testing.Loader, dataContext map[string]inter
354
364
suiteRunner := runner .GetTestSuiteRunner (testSuite )
355
365
suiteRunner .WithTestReporter (o .reporter )
356
366
suiteRunner .WithSecure (testSuite .Spec .Secure )
357
- suiteRunner .WithOutputWriter (os . Stdout )
367
+ suiteRunner .WithOutputWriter (o . reportWriter . GetWriter () )
358
368
suiteRunner .WithWriteLevel (o .level )
359
369
suiteRunner .WithSuite (testSuite )
360
370
var caseFilterObj interface {}
0 commit comments