github.com/getgauge/gauge@v1.6.9/reporter/simpleConsole_test.go (about) 1 /*---------------------------------------------------------------- 2 * Copyright (c) ThoughtWorks, Inc. 3 * Licensed under the Apache License, Version 2.0 4 * See LICENSE in the project root for license information. 5 *----------------------------------------------------------------*/ 6 7 package reporter 8 9 import ( 10 "fmt" 11 "time" 12 13 "github.com/getgauge/gauge-proto/go/gauge_messages" 14 "github.com/getgauge/gauge/execution/result" 15 "github.com/getgauge/gauge/gauge" 16 17 . "gopkg.in/check.v1" 18 ) 19 20 type dummyWriter struct { 21 output string 22 } 23 24 func (dw *dummyWriter) Write(b []byte) (int, error) { 25 dw.output += string(b) 26 return len(b), nil 27 } 28 29 func newDummyWriter() *dummyWriter { 30 return &dummyWriter{} 31 } 32 33 func setupSimpleConsole() (*dummyWriter, *simpleConsole) { 34 dw := newDummyWriter() 35 sc := newSimpleConsole(dw) 36 return dw, sc 37 } 38 39 func (s *MySuite) TestSpecStart_SimpleConsole(c *C) { 40 dw, sc := setupSimpleConsole() 41 specRes := &result.SpecResult{Skipped: false} 42 43 sc.SpecStart(&gauge.Specification{Heading: &gauge.Heading{Value: "Specification heading"}}, specRes) 44 c.Assert(dw.output, Equals, "# Specification heading\n") 45 } 46 47 func (s *MySuite) TestSpecEnd_SimpleConsole(c *C) { 48 dw, sc := setupSimpleConsole() 49 50 sc.SpecEnd(&gauge.Specification{}, &result.SpecResult{Skipped: false, ProtoSpec: &gauge_messages.ProtoSpec{}}) 51 c.Assert(dw.output, Equals, "\n") 52 } 53 54 func (s *MySuite) TestScenarioStart_SimpleConsole(c *C) { 55 dw, sc := setupSimpleConsole() 56 scnRes := result.NewScenarioResult(&gauge_messages.ProtoScenario{ExecutionStatus: gauge_messages.ExecutionStatus_PASSED}) 57 58 sc.ScenarioStart(&gauge.Scenario{Heading: &gauge.Heading{Value: "First Scenario"}}, &gauge_messages.ExecutionInfo{}, scnRes) 59 c.Assert(dw.output, Equals, " ## First Scenario\n") 60 } 61 62 func (s *MySuite) TestScenarioEnd_SimpleConsole(c *C) { 63 _, sc := setupSimpleConsole() 64 sc.indentation = 2 65 res := result.NewScenarioResult(&gauge_messages.ProtoScenario{ExecutionStatus: gauge_messages.ExecutionStatus_FAILED, Failed: true}) 66 67 sc.ScenarioEnd(nil, res, &gauge_messages.ExecutionInfo{}) 68 69 c.Assert(sc.indentation, Equals, 0) 70 } 71 72 func (s *MySuite) TestStepStartInVerboseMode_SimpleConsole(c *C) { 73 dw, sc := setupSimpleConsole() 74 sc.indentation = 2 75 Verbose = true 76 77 sc.StepStart("* Say hello to gauge") 78 79 c.Assert(dw.output, Equals, " * Say hello to gauge\n") 80 } 81 82 func (s *MySuite) TestStepStartInNonVerboseMode_SimpleConsole(c *C) { 83 dw, sc := setupSimpleConsole() 84 sc.indentation = 2 85 Verbose = false 86 87 sc.StepStart("* Say hello to gauge") 88 89 c.Assert(dw.output, Equals, "") 90 } 91 92 func (s *MySuite) TestStepEnd_SimpleConsole(c *C) { 93 _, sc := setupSimpleConsole() 94 sc.indentation = 6 95 specInfo := &gauge_messages.ExecutionInfo{CurrentSpec: &gauge_messages.SpecInfo{Name: "hello.spec"}} 96 stepRes := result.NewStepResult(&gauge_messages.ProtoStep{StepExecutionResult: &gauge_messages.ProtoStepExecutionResult{}}) 97 sc.StepEnd(gauge.Step{LineText: ""}, stepRes, specInfo) 98 99 c.Assert(sc.indentation, Equals, 2) 100 } 101 102 func (s *MySuite) TestSingleConceptStartInVerboseMode_SimpleConsole(c *C) { 103 dw, sc := setupSimpleConsole() 104 sc.indentation = 2 105 Verbose = true 106 107 sc.ConceptStart("* my first concept") 108 109 c.Assert(dw.output, Equals, fmt.Sprintf("%s* my first concept\n", spaces(6))) 110 } 111 112 func (s *MySuite) TestNestedConceptStartInVerboseMode_SimpleConsole(c *C) { 113 dw, sc := setupSimpleConsole() 114 sc.indentation = 2 115 Verbose = true 116 117 sc.ConceptStart("* my first concept") 118 dw.output = "" 119 sc.ConceptStart("* my second concept") 120 121 c.Assert(dw.output, Equals, fmt.Sprintf("%s* my second concept\n", spaces(10))) 122 } 123 124 func (s *MySuite) TestNestedConceptStartInVerboseMode_case2(c *C) { 125 dw, sc := setupSimpleConsole() 126 sc.indentation = 2 127 Verbose = true 128 129 sc.ConceptStart("* my first concept") 130 dw.output = "" 131 sc.StepStart("* do foo bar") 132 133 c.Assert(dw.output, Equals, fmt.Sprintf("%s* do foo bar\n", spaces(10))) 134 } 135 136 func (s *MySuite) TestNestedConceptStartInVerboseMode_case3(c *C) { 137 dw, sc := setupSimpleConsole() 138 sc.indentation = 2 139 Verbose = true 140 141 sc.ConceptStart("* my first concept") 142 sc.ConceptStart("* my second concept") 143 dw.output = "" 144 sc.StepStart("* do foo bar") 145 146 c.Assert(dw.output, Equals, fmt.Sprintf("%s* do foo bar\n", spaces(14))) 147 } 148 149 func (s *MySuite) TestConceptEnd_SimpleConsole(c *C) { 150 _, sc := setupSimpleConsole() 151 sc.indentation = 6 152 Verbose = true 153 res := &DummyResult{IsFailed: false} 154 155 sc.ConceptEnd(res) 156 157 c.Assert(sc.indentation, Equals, 2) 158 } 159 160 func (s *MySuite) TestDataTable_SimpleConsole(c *C) { 161 dw, sc := setupSimpleConsole() 162 sc.indentation = 2 163 Verbose = true 164 table := `|Product|Description | 165 |-------|-----------------------------| 166 |Gauge |Test automation with ease |` 167 168 want := `|Product|Description | 169 |-------|-----------------------------| 170 |Gauge |Test automation with ease |` 171 172 sc.DataTable(table) 173 174 c.Assert(dw.output, Equals, want) 175 } 176 177 func (s *MySuite) TestError_SimpleConsole(c *C) { 178 dw, sc := setupSimpleConsole() 179 sc.indentation = 6 180 Verbose = true 181 182 sc.Errorf("Failed %s", "network error") 183 184 c.Assert(dw.output, Equals, fmt.Sprintf("%sFailed network error\n", spaces(sc.indentation+errorIndentation))) 185 } 186 187 func (s *MySuite) TestWrite_VerboseSimpleConsole(c *C) { 188 dw, sc := setupSimpleConsole() 189 sc.indentation = 6 190 Verbose = true 191 input := "hello, gauge" 192 193 _, err := sc.Write([]byte(input)) 194 195 c.Assert(err, Equals, nil) 196 c.Assert(dw.output, Equals, input) 197 } 198 199 func (s *MySuite) TestWrite_SimpleConsole(c *C) { 200 dw, sc := setupSimpleConsole() 201 sc.indentation = 6 202 Verbose = false 203 input := "hello, gauge" 204 205 _, err := sc.Write([]byte(input)) 206 207 c.Assert(err, Equals, nil) 208 c.Assert(dw.output, Equals, input) 209 } 210 211 func (s *MySuite) TestSpecReporting_SimpleConsole(c *C) { 212 dw, sc := setupSimpleConsole() 213 Verbose = true 214 215 scnRes := result.NewScenarioResult(&gauge_messages.ProtoScenario{ExecutionStatus: gauge_messages.ExecutionStatus_PASSED}) 216 sc.SpecStart(&gauge.Specification{Heading: &gauge.Heading{Value: "Specification heading"}}, &result.SpecResult{Skipped: false}) 217 sc.ScenarioStart(&gauge.Scenario{Heading: &gauge.Heading{Value: "My First scenario"}}, &gauge_messages.ExecutionInfo{}, scnRes) 218 sc.StepStart("* do foo bar") 219 _, err := sc.Write([]byte("doing foo bar")) 220 c.Assert(err, IsNil) 221 res := result.NewScenarioResult(&gauge_messages.ProtoScenario{ExecutionStatus: gauge_messages.ExecutionStatus_PASSED, Failed: false}) 222 specInfo := &gauge_messages.ExecutionInfo{CurrentSpec: &gauge_messages.SpecInfo{Name: "hello.spec"}} 223 stepExeRes := &gauge_messages.ProtoStepExecutionResult{ExecutionResult: &gauge_messages.ProtoExecutionResult{Failed: false}} 224 stepRes := result.NewStepResult(&gauge_messages.ProtoStep{StepExecutionResult: stepExeRes}) 225 226 sc.StepEnd(gauge.Step{LineText: "* do foo bar"}, stepRes, specInfo) 227 sc.ScenarioEnd(nil, res, &gauge_messages.ExecutionInfo{}) 228 specRes := &result.SpecResult{Skipped: false, ProtoSpec: &gauge_messages.ProtoSpec{}} 229 sc.SpecEnd(&gauge.Specification{}, specRes) 230 231 want := `# Specification heading 232 ## My First scenario 233 * do foo bar 234 doing foo bar 235 ` 236 237 c.Assert(dw.output, Equals, want) 238 } 239 240 func (s *MySuite) TestStepEndWithPreHookFailure_SimpleConsole(c *C) { 241 dw, sc := setupSimpleConsole() 242 sc.indentation = 6 243 errMsg := "pre hook failure message" 244 stackTrace := "my stacktrace" 245 specInfo := &gauge_messages.ExecutionInfo{CurrentSpec: &gauge_messages.SpecInfo{Name: "hello.spec"}} 246 preHookFailure := &gauge_messages.ProtoHookFailure{ErrorMessage: errMsg, StackTrace: stackTrace} 247 stepExeRes := &gauge_messages.ProtoStepExecutionResult{PreHookFailure: preHookFailure} 248 stepRes := result.NewStepResult(&gauge_messages.ProtoStep{StepExecutionResult: stepExeRes}) 249 250 sc.StepEnd(gauge.Step{LineText: "* my step"}, stepRes, specInfo) 251 252 c.Assert(sc.indentation, Equals, 2) 253 c.Assert(dw.output, Equals, fmt.Sprintf("%sError Message: %s\n%sStacktrace: \n%s%s\n", spaces(8), errMsg, spaces(8), spaces(8), stackTrace)) 254 } 255 256 func (s *MySuite) TestStepEndWithPostHookFailure_SimpleConsole(c *C) { 257 dw, sc := setupSimpleConsole() 258 sc.indentation = 6 259 errMsg := "post hook failure message" 260 stackTrace := "my stacktrace" 261 specInfo := &gauge_messages.ExecutionInfo{CurrentSpec: &gauge_messages.SpecInfo{Name: "hello.spec"}} 262 postHookFailure := &gauge_messages.ProtoHookFailure{ErrorMessage: errMsg, StackTrace: stackTrace} 263 stepExeRes := &gauge_messages.ProtoStepExecutionResult{PostHookFailure: postHookFailure} 264 stepRes := result.NewStepResult(&gauge_messages.ProtoStep{StepExecutionResult: stepExeRes}) 265 266 sc.StepEnd(gauge.Step{LineText: "* my step"}, stepRes, specInfo) 267 268 c.Assert(sc.indentation, Equals, 2) 269 c.Assert(dw.output, Equals, fmt.Sprintf("%sError Message: %s\n%sStacktrace: \n%s%s\n", spaces(8), errMsg, spaces(8), spaces(8), stackTrace)) 270 } 271 272 func (s *MySuite) TestStepEndWithPreAndPostHookFailure_SimpleConsole(c *C) { 273 dw, sc := setupSimpleConsole() 274 sc.indentation = 6 275 preHookErrMsg := "pre hook failure message" 276 postHookErrMsg := "post hook failure message" 277 stackTrace := "my stacktrace" 278 specInfo := &gauge_messages.ExecutionInfo{CurrentSpec: &gauge_messages.SpecInfo{Name: "hello.spec"}} 279 preHookFailure := &gauge_messages.ProtoHookFailure{ErrorMessage: preHookErrMsg, StackTrace: stackTrace} 280 postHookFailure := &gauge_messages.ProtoHookFailure{ErrorMessage: postHookErrMsg, StackTrace: stackTrace} 281 stepExeRes := &gauge_messages.ProtoStepExecutionResult{PostHookFailure: postHookFailure, PreHookFailure: preHookFailure} 282 stepRes := result.NewStepResult(&gauge_messages.ProtoStep{StepExecutionResult: stepExeRes}) 283 284 sc.StepEnd(gauge.Step{LineText: "* my step"}, stepRes, specInfo) 285 286 c.Assert(sc.indentation, Equals, 2) 287 err1 := fmt.Sprintf("%sError Message: %s\n%sStacktrace: \n%s%s\n", spaces(8), preHookErrMsg, spaces(8), spaces(8), stackTrace) 288 err2 := fmt.Sprintf("%sError Message: %s\n%sStacktrace: \n%s%s\n", spaces(8), postHookErrMsg, spaces(8), spaces(8), stackTrace) 289 c.Assert(dw.output, Equals, err1+err2) 290 } 291 292 func (s *MySuite) TestSubscribeScenarioEndPreHookFailure(c *C) { 293 dw, sc := setupSimpleConsole() 294 sc.indentation = scenarioIndentation 295 currentReporter = sc 296 preHookErrMsg := "pre hook failure message" 297 stackTrace := "my stacktrace" 298 preHookFailure := &gauge_messages.ProtoHookFailure{ErrorMessage: preHookErrMsg, StackTrace: stackTrace} 299 sceRes := result.NewScenarioResult(&gauge_messages.ProtoScenario{ 300 ExecutionStatus: gauge_messages.ExecutionStatus_PASSED, 301 PreHookFailure: preHookFailure, 302 }) 303 304 sc.ScenarioEnd(nil, sceRes, &gauge_messages.ExecutionInfo{}) 305 306 ind := spaces(scenarioIndentation + errorIndentation) 307 want := ind + "Error Message: " + preHookErrMsg + newline + ind + "Stacktrace: \n" + ind + stackTrace + newline 308 c.Assert(dw.output, Equals, want) 309 c.Assert(sc.indentation, Equals, 0) 310 } 311 312 func (s *MySuite) TestSpecEndWithPostHookFailure_SimpleConsole(c *C) { 313 dw, sc := setupSimpleConsole() 314 sc.indentation = 0 315 errMsg := "post hook failure message" 316 stackTrace := "my stacktrace" 317 postHookFailure := &gauge_messages.ProtoHookFailure{ErrorMessage: errMsg, StackTrace: stackTrace} 318 res := &result.SpecResult{Skipped: false, ProtoSpec: &gauge_messages.ProtoSpec{PostHookFailures: []*gauge_messages.ProtoHookFailure{postHookFailure}}, IsFailed: true} 319 320 sc.SpecEnd(&gauge.Specification{}, res) 321 322 c.Assert(sc.indentation, Equals, 0) 323 ind := spaces(errorIndentation) 324 want := ind + "Error Message: " + errMsg + newline + ind + "Stacktrace: \n" + ind + stackTrace + newline + newline 325 c.Assert(dw.output, Equals, want) 326 } 327 328 func (s *MySuite) TestSuiteEndWithPostHookFailure_SimpleConsole(c *C) { 329 dw, sc := setupSimpleConsole() 330 sc.indentation = 0 331 errMsg := "post hook failure message" 332 stackTrace := "my stacktrace" 333 res := result.NewSuiteResult("", time.Now()) 334 postHookFailure := &gauge_messages.ProtoHookFailure{ErrorMessage: errMsg, StackTrace: stackTrace} 335 res.PostSuite = postHookFailure 336 337 sc.SuiteEnd(res) 338 339 c.Assert(sc.indentation, Equals, 0) 340 ind := spaces(errorIndentation) 341 want := ind + "Error Message: " + errMsg + newline + ind + "Stacktrace: \n" + ind + stackTrace + newline 342 c.Assert(dw.output, Equals, want) 343 } 344 345 func (s *MySuite) TestExcludeLineNoForFailedStepInConcept(c *C) { 346 dw, sc := setupSimpleConsole() 347 sc.indentation = 4 348 errMsg := "failure message" 349 stackTrace := "my stacktrace" 350 failed := true 351 specName := "hello.spec" 352 stepText := "* my Step" 353 parentStep := gauge.Step{LineText: "* parent step"} 354 exeInfo := &gauge_messages.ExecutionInfo{CurrentSpec: &gauge_messages.SpecInfo{FileName: specName}} 355 stepExeRes := &gauge_messages.ProtoStepExecutionResult{ExecutionResult: &gauge_messages.ProtoExecutionResult{Failed: failed, StackTrace: stackTrace, ErrorMessage: errMsg}} 356 stepRes := result.NewStepResult(&gauge_messages.ProtoStep{StepExecutionResult: stepExeRes}) 357 stepRes.SetStepFailure() 358 359 sc.StepEnd(gauge.Step{LineText: stepText, Parent: &parentStep}, stepRes, exeInfo) 360 361 c.Assert(sc.indentation, Equals, 0) 362 ind := spaces(errorIndentation + 4) 363 want := ind + newline + ind + "Failed Step: " + stepText + newline + ind + "Specification: " + specName + newline + ind + "Error Message: " + errMsg + newline + ind + "Stacktrace: \n" + ind + stackTrace + newline 364 c.Assert(dw.output, Equals, want) 365 } 366 367 func (s *MySuite) TestIncludeLineNoForFailedStep(c *C) { 368 dw, sc := setupSimpleConsole() 369 sc.indentation = 4 370 errMsg := "failure message" 371 stackTrace := "my stacktrace" 372 failed := true 373 specName := "hello.spec" 374 stepText := "* my Step" 375 exeInfo := &gauge_messages.ExecutionInfo{CurrentSpec: &gauge_messages.SpecInfo{FileName: specName}} 376 stepExeRes := &gauge_messages.ProtoStepExecutionResult{ExecutionResult: &gauge_messages.ProtoExecutionResult{Failed: failed, StackTrace: stackTrace, ErrorMessage: errMsg}} 377 stepRes := result.NewStepResult(&gauge_messages.ProtoStep{StepExecutionResult: stepExeRes}) 378 stepRes.SetStepFailure() 379 380 sc.StepEnd(gauge.Step{LineText: stepText, LineNo: 3}, stepRes, exeInfo) 381 382 c.Assert(sc.indentation, Equals, 0) 383 ind := spaces(errorIndentation + 4) 384 want := ind + newline + ind + "Failed Step: " + stepText + newline + ind + "Specification: " + specName + ":3" + newline + ind + "Error Message: " + errMsg + newline + ind + "Stacktrace: \n" + ind + stackTrace + newline 385 c.Assert(dw.output, Equals, want) 386 }