@@ -79,10 +79,13 @@ var (
7979 runLogger = logging .DefaultLogger (logging .LogLevelInfo ).WithName ("run" )
8080)
8181
82- func newDefaultRunOption () * runOption {
82+ func newDefaultRunOption (output io.Writer ) * runOption {
83+ if output == nil {
84+ output = os .Stdout
85+ }
8386 return & runOption {
8487 reporter : runner .NewMemoryTestReporter (nil , "" ),
85- reportWriter : runner .NewResultWriter (os . Stdout ),
88+ reportWriter : runner .NewResultWriter (output ),
8689 loader : testing .NewFileLoader (),
8790 githubReportOption : & runner.GithubPRCommentOption {},
8891 }
@@ -97,7 +100,7 @@ func newDiscardRunOption() *runOption {
97100
98101// createRunCommand returns the run command
99102func createRunCommand () (cmd * cobra.Command ) {
100- opt := newDefaultRunOption ()
103+ opt := newDefaultRunOption (nil )
101104 cmd = & cobra.Command {
102105 Use : "run" ,
103106 Aliases : []string {"r" },
@@ -111,29 +114,33 @@ See also https://github.com/LinuxSuRen/api-testing/tree/master/sample`,
111114
112115 // set flags
113116 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 )
131118 addGitHubReportFlags (flags , opt .githubReportOption )
132119 return
133120}
134121
135122const caseFilter = "case-filter"
136123
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+
137144func (o * runOption ) preRunE (cmd * cobra.Command , args []string ) (err error ) {
138145 ctx := cmd .Context ()
139146 if ctx == nil {
@@ -148,6 +155,9 @@ func (o *runOption) preRunE(cmd *cobra.Command, args []string) (err error) {
148155 return
149156 }
150157
158+ defer func () {
159+ _ = reportFile .Close ()
160+ }()
151161 writer = io .MultiWriter (writer , reportFile )
152162 }
153163
@@ -354,7 +364,7 @@ func (o *runOption) runSuite(loader testing.Loader, dataContext map[string]inter
354364 suiteRunner := runner .GetTestSuiteRunner (testSuite )
355365 suiteRunner .WithTestReporter (o .reporter )
356366 suiteRunner .WithSecure (testSuite .Spec .Secure )
357- suiteRunner .WithOutputWriter (os . Stdout )
367+ suiteRunner .WithOutputWriter (o . reportWriter . GetWriter () )
358368 suiteRunner .WithWriteLevel (o .level )
359369 suiteRunner .WithSuite (testSuite )
360370 var caseFilterObj interface {}
0 commit comments