github.com/getgauge/gauge@v1.6.9/execution/merge_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 execution 8 9 import ( 10 "testing" 11 12 "reflect" 13 14 gm "github.com/getgauge/gauge-proto/go/gauge_messages" 15 "github.com/getgauge/gauge/execution/result" 16 ) 17 18 type stat struct { 19 failed int 20 skipped int 21 total int 22 } 23 24 var statsTests = []struct { 25 status gm.ExecutionStatus 26 want stat 27 message string 28 }{ 29 {gm.ExecutionStatus_FAILED, stat{failed: 1, total: 1}, "Scenario Failure"}, 30 {gm.ExecutionStatus_SKIPPED, stat{skipped: 1, total: 1}, "Scenario Skipped"}, 31 {gm.ExecutionStatus_PASSED, stat{total: 1}, "Scenario Passed"}, 32 } 33 34 func TestModifySpecStats(t *testing.T) { 35 for _, test := range statsTests { 36 res := &result.SpecResult{} 37 38 modifySpecStats(&gm.ProtoScenario{ExecutionStatus: test.status}, res) 39 got := stat{failed: res.ScenarioFailedCount, skipped: res.ScenarioSkippedCount, total: res.ScenarioCount} 40 41 if !reflect.DeepEqual(got, test.want) { 42 t.Errorf("Modify spec stats failed for %s. Want: %v , Got: %v", test.message, test.want, got) 43 } 44 } 45 } 46 47 func TestAggregateDataTableScnStats(t *testing.T) { 48 res := &result.SpecResult{} 49 scns := map[string][]*gm.ProtoTableDrivenScenario{ 50 "heading1": { 51 {Scenario: &gm.ProtoScenario{ExecutionStatus: gm.ExecutionStatus_PASSED}}, 52 {Scenario: &gm.ProtoScenario{ExecutionStatus: gm.ExecutionStatus_FAILED}}, 53 {Scenario: &gm.ProtoScenario{ 54 ExecutionStatus: gm.ExecutionStatus_SKIPPED, 55 SkipErrors: []string{"--table-rows"}, 56 }}, 57 }, 58 "heading2": {{Scenario: &gm.ProtoScenario{ 59 ExecutionStatus: gm.ExecutionStatus_SKIPPED, 60 SkipErrors: []string{""}, 61 }}}, 62 "heading3": {{Scenario: &gm.ProtoScenario{ExecutionStatus: gm.ExecutionStatus_PASSED}}}, 63 "heading4": {{Scenario: &gm.ProtoScenario{ExecutionStatus: gm.ExecutionStatus_FAILED}}}, 64 } 65 66 aggregateDataTableScnStats(scns, res) 67 68 got := stat{failed: res.ScenarioFailedCount, skipped: res.ScenarioSkippedCount, total: res.ScenarioCount} 69 want := stat{failed: 2, skipped: 1, total: 5} 70 71 if !reflect.DeepEqual(got, want) { 72 t.Errorf("Aggregate data table scenario stats failed. Want: %v , Got: %v", want, got) 73 } 74 } 75 76 func TestMergeResults(t *testing.T) { 77 got := mergeResults([]*result.SpecResult{ 78 { 79 ProtoSpec: &gm.ProtoSpec{ 80 SpecHeading: "heading", FileName: "filename", Tags: []string{"tags"}, 81 Items: []*gm.ProtoItem{ 82 {ItemType: gm.ProtoItem_Table, Table: &gm.ProtoTable{Headers: &gm.ProtoTableRow{Cells: []string{"a"}}, Rows: []*gm.ProtoTableRow{{Cells: []string{"d"}}}}}, 83 {ItemType: gm.ProtoItem_Scenario, Scenario: &gm.ProtoScenario{ExecutionStatus: gm.ExecutionStatus_PASSED, ScenarioHeading: "scenario Heading1"}}, 84 { 85 ItemType: gm.ProtoItem_TableDrivenScenario, TableDrivenScenario: &gm.ProtoTableDrivenScenario{ 86 Scenario: &gm.ProtoScenario{ExecutionStatus: gm.ExecutionStatus_PASSED, ScenarioHeading: "scenario Heading2"}, 87 TableRowIndex: 2, 88 }, 89 }, 90 }, 91 }, ExecutionTime: int64(1), 92 }, 93 { 94 ProtoSpec: &gm.ProtoSpec{ 95 SpecHeading: "heading", FileName: "filename", Tags: []string{"tags"}, 96 Items: []*gm.ProtoItem{ 97 {ItemType: gm.ProtoItem_Table, Table: &gm.ProtoTable{Headers: &gm.ProtoTableRow{Cells: []string{"a"}}, Rows: []*gm.ProtoTableRow{{Cells: []string{"b"}}}}}, 98 { 99 ItemType: gm.ProtoItem_TableDrivenScenario, TableDrivenScenario: &gm.ProtoTableDrivenScenario{ 100 Scenario: &gm.ProtoScenario{ExecutionStatus: gm.ExecutionStatus_PASSED, ScenarioHeading: "scenario Heading2"}, 101 TableRowIndex: 0, 102 }, 103 }, 104 }, 105 }, ExecutionTime: int64(2), 106 }, 107 { 108 ProtoSpec: &gm.ProtoSpec{ 109 SpecHeading: "heading", FileName: "filename", Tags: []string{"tags"}, 110 Items: []*gm.ProtoItem{ 111 {ItemType: gm.ProtoItem_Table, Table: &gm.ProtoTable{Headers: &gm.ProtoTableRow{Cells: []string{"a"}}, Rows: []*gm.ProtoTableRow{{Cells: []string{"c"}}}}}, 112 { 113 ItemType: gm.ProtoItem_TableDrivenScenario, TableDrivenScenario: &gm.ProtoTableDrivenScenario{ 114 Scenario: &gm.ProtoScenario{ExecutionStatus: gm.ExecutionStatus_PASSED, ScenarioHeading: "scenario Heading2"}, 115 TableRowIndex: 1, 116 }, 117 }, 118 }, 119 }, ExecutionTime: int64(2), 120 }, 121 }) 122 want := &result.SpecResult{ 123 ProtoSpec: &gm.ProtoSpec{ 124 SpecHeading: "heading", FileName: "filename", Tags: []string{"tags"}, 125 Items: []*gm.ProtoItem{ 126 {ItemType: gm.ProtoItem_Table, Table: &gm.ProtoTable{Headers: &gm.ProtoTableRow{Cells: []string{"a"}}, Rows: []*gm.ProtoTableRow{{Cells: []string{"d"}}, {Cells: []string{"b"}}, {Cells: []string{"c"}}}}}, 127 {ItemType: gm.ProtoItem_Scenario, Scenario: &gm.ProtoScenario{ExecutionStatus: gm.ExecutionStatus_PASSED, ScenarioHeading: "scenario Heading1"}}, 128 { 129 ItemType: gm.ProtoItem_TableDrivenScenario, TableDrivenScenario: &gm.ProtoTableDrivenScenario{ 130 Scenario: &gm.ProtoScenario{ExecutionStatus: gm.ExecutionStatus_PASSED, ScenarioHeading: "scenario Heading2"}, 131 TableRowIndex: 0, 132 }, 133 }, 134 { 135 ItemType: gm.ProtoItem_TableDrivenScenario, TableDrivenScenario: &gm.ProtoTableDrivenScenario{ 136 Scenario: &gm.ProtoScenario{ExecutionStatus: gm.ExecutionStatus_PASSED, ScenarioHeading: "scenario Heading2"}, 137 TableRowIndex: 1, 138 }, 139 }, 140 { 141 ItemType: gm.ProtoItem_TableDrivenScenario, TableDrivenScenario: &gm.ProtoTableDrivenScenario{ 142 Scenario: &gm.ProtoScenario{ExecutionStatus: gm.ExecutionStatus_PASSED, ScenarioHeading: "scenario Heading2"}, 143 TableRowIndex: 2, 144 }, 145 }, 146 }, IsTableDriven: false, 147 }, 148 ScenarioCount: 4, ScenarioSkippedCount: 0, ScenarioFailedCount: 0, IsFailed: false, Skipped: false, ExecutionTime: int64(5), 149 } 150 151 if !reflect.DeepEqual(got, want) { 152 t.Errorf("Merge data table spec results failed.\n\tWant: %v\n\tGot: %v", want, got) 153 } 154 } 155 156 func TestMergeResultsWithPreHookFailure(t *testing.T) { 157 got := mergeResults([]*result.SpecResult{ 158 { 159 ProtoSpec: &gm.ProtoSpec{ 160 PreHookFailures: []*gm.ProtoHookFailure{{StackTrace: "stacktrace"}}, 161 SpecHeading: "heading", FileName: "filename", Tags: []string{"tags"}, 162 Items: []*gm.ProtoItem{ 163 {ItemType: gm.ProtoItem_Table, Table: &gm.ProtoTable{Headers: &gm.ProtoTableRow{Cells: []string{"a"}}, Rows: []*gm.ProtoTableRow{{Cells: []string{"b"}}}}}, 164 }, 165 }, ExecutionTime: int64(1), 166 }, 167 { 168 ProtoSpec: &gm.ProtoSpec{ 169 PreHookFailures: []*gm.ProtoHookFailure{{StackTrace: "stacktrace1"}}, 170 SpecHeading: "heading", FileName: "filename", Tags: []string{"tags"}, 171 Items: []*gm.ProtoItem{ 172 {ItemType: gm.ProtoItem_Table, Table: &gm.ProtoTable{Headers: &gm.ProtoTableRow{Cells: []string{"a"}}, Rows: []*gm.ProtoTableRow{{Cells: []string{"d"}}}}}, 173 }, 174 }, ExecutionTime: int64(2), 175 }, 176 { 177 ProtoSpec: &gm.ProtoSpec{ 178 PreHookFailures: []*gm.ProtoHookFailure{{StackTrace: "stacktrace2"}}, 179 SpecHeading: "heading", FileName: "filename", Tags: []string{"tags"}, 180 Items: []*gm.ProtoItem{ 181 {ItemType: gm.ProtoItem_Table, Table: &gm.ProtoTable{Headers: &gm.ProtoTableRow{Cells: []string{"a"}}, Rows: []*gm.ProtoTableRow{{Cells: []string{"c"}}}}}, 182 }, 183 }, ExecutionTime: int64(2), 184 }, 185 }) 186 want := &result.SpecResult{ 187 ProtoSpec: &gm.ProtoSpec{ 188 PreHookFailures: []*gm.ProtoHookFailure{{StackTrace: "stacktrace"}, {StackTrace: "stacktrace1", TableRowIndex: 1}, {StackTrace: "stacktrace2", TableRowIndex: 2}}, 189 SpecHeading: "heading", FileName: "filename", Tags: []string{"tags"}, 190 Items: []*gm.ProtoItem{ 191 {ItemType: gm.ProtoItem_Table, Table: &gm.ProtoTable{Headers: &gm.ProtoTableRow{Cells: []string{"a"}}, Rows: []*gm.ProtoTableRow{{Cells: []string{"b"}}, {Cells: []string{"d"}}, {Cells: []string{"c"}}}}}, 192 }, IsTableDriven: false, 193 }, 194 ScenarioCount: 0, ScenarioSkippedCount: 0, ScenarioFailedCount: 0, IsFailed: false, Skipped: false, ExecutionTime: int64(5), 195 } 196 197 if !reflect.DeepEqual(got, want) { 198 t.Errorf("Merge data table spec results failed.\n\tWant: %v\n\tGot: %v", want, got) 199 } 200 } 201 202 func TestMergeSkippedResults(t *testing.T) { 203 got := mergeResults([]*result.SpecResult{ 204 { 205 ProtoSpec: &gm.ProtoSpec{ 206 SpecHeading: "heading", FileName: "filename", Tags: []string{"tags"}, 207 Items: []*gm.ProtoItem{ 208 {ItemType: gm.ProtoItem_Table, Table: &gm.ProtoTable{Headers: &gm.ProtoTableRow{Cells: []string{"a"}}, Rows: []*gm.ProtoTableRow{{Cells: []string{"b"}}}}}, 209 {ItemType: gm.ProtoItem_Scenario, Scenario: &gm.ProtoScenario{ExecutionStatus: gm.ExecutionStatus_SKIPPED, ScenarioHeading: "scenario Heading1", SkipErrors: []string{"error"}}}, 210 { 211 ItemType: gm.ProtoItem_TableDrivenScenario, TableDrivenScenario: &gm.ProtoTableDrivenScenario{ 212 Scenario: &gm.ProtoScenario{ExecutionStatus: gm.ExecutionStatus_SKIPPED, ScenarioHeading: "scenario Heading2", SkipErrors: []string{"error"}}, 213 TableRowIndex: 0, 214 }, 215 }, 216 }, 217 }, ExecutionTime: int64(1), 218 Skipped: true, 219 }, 220 { 221 ProtoSpec: &gm.ProtoSpec{ 222 SpecHeading: "heading", FileName: "filename", Tags: []string{"tags"}, 223 Items: []*gm.ProtoItem{ 224 {ItemType: gm.ProtoItem_Table, Table: &gm.ProtoTable{Headers: &gm.ProtoTableRow{Cells: []string{"a"}}, Rows: []*gm.ProtoTableRow{{Cells: []string{"c"}}}}}, 225 { 226 ItemType: gm.ProtoItem_TableDrivenScenario, TableDrivenScenario: &gm.ProtoTableDrivenScenario{ 227 Scenario: &gm.ProtoScenario{ExecutionStatus: gm.ExecutionStatus_SKIPPED, ScenarioHeading: "scenario Heading2", SkipErrors: []string{"error"}}, 228 TableRowIndex: 1, 229 }, 230 }, 231 }, 232 }, ExecutionTime: int64(2), 233 Skipped: true, 234 }, 235 }) 236 want := &result.SpecResult{ 237 ProtoSpec: &gm.ProtoSpec{ 238 SpecHeading: "heading", FileName: "filename", Tags: []string{"tags"}, 239 Items: []*gm.ProtoItem{ 240 {ItemType: gm.ProtoItem_Table, Table: &gm.ProtoTable{Headers: &gm.ProtoTableRow{Cells: []string{"a"}}, Rows: []*gm.ProtoTableRow{{Cells: []string{"b"}}, {Cells: []string{"c"}}}}}, 241 {ItemType: gm.ProtoItem_Scenario, Scenario: &gm.ProtoScenario{ExecutionStatus: gm.ExecutionStatus_SKIPPED, SkipErrors: []string{"error"}, ScenarioHeading: "scenario Heading1"}}, 242 { 243 ItemType: gm.ProtoItem_TableDrivenScenario, TableDrivenScenario: &gm.ProtoTableDrivenScenario{ 244 Scenario: &gm.ProtoScenario{ExecutionStatus: gm.ExecutionStatus_SKIPPED, SkipErrors: []string{"error"}, ScenarioHeading: "scenario Heading2"}, 245 TableRowIndex: 0, 246 }, 247 }, 248 { 249 ItemType: gm.ProtoItem_TableDrivenScenario, TableDrivenScenario: &gm.ProtoTableDrivenScenario{ 250 Scenario: &gm.ProtoScenario{ExecutionStatus: gm.ExecutionStatus_SKIPPED, SkipErrors: []string{"error"}, ScenarioHeading: "scenario Heading2"}, 251 TableRowIndex: 1, 252 }, 253 }, 254 }, IsTableDriven: false, 255 }, 256 ScenarioCount: 3, ScenarioSkippedCount: 3, ScenarioFailedCount: 0, IsFailed: false, Skipped: true, ExecutionTime: int64(3), 257 } 258 259 if !reflect.DeepEqual(got, want) { 260 t.Errorf("Merge data table spec results failed.\n\tWant: %v\n\tGot: %v", want, got) 261 } 262 } 263 264 func TestMergeResultsExecutionTimeInParallel(t *testing.T) { 265 InParallel = true 266 267 got := mergeResults([]*result.SpecResult{ 268 { 269 ProtoSpec: &gm.ProtoSpec{ 270 SpecHeading: "heading", FileName: "filename", Tags: []string{"tags"}, 271 }, ExecutionTime: int64(1), 272 }, 273 { 274 ProtoSpec: &gm.ProtoSpec{ 275 SpecHeading: "heading", FileName: "filename", Tags: []string{"tags"}, 276 }, ExecutionTime: int64(2), 277 }, 278 }) 279 280 want := int64(2) 281 InParallel = false 282 283 if !reflect.DeepEqual(got.ExecutionTime, want) { 284 t.Errorf("Execution time in parallel data table spec results.\n\tWant: %v\n\tGot: %v", want, got.ExecutionTime) 285 } 286 } 287 288 func TestMergeDataTableSpecResults(t *testing.T) { 289 res := &result.SuiteResult{ 290 Environment: "env", 291 ProjectName: "name", 292 Tags: "tags", 293 SpecResults: []*result.SpecResult{ 294 { 295 ProtoSpec: &gm.ProtoSpec{ 296 SpecHeading: "heading", FileName: "filename", Tags: []string{"tags"}, 297 Items: []*gm.ProtoItem{ 298 {ItemType: gm.ProtoItem_Scenario, Scenario: &gm.ProtoScenario{ExecutionStatus: gm.ExecutionStatus_PASSED, ScenarioHeading: "scenario Heading1"}}, 299 }, 300 }, 301 }, 302 }, 303 } 304 got := mergeDataTableSpecResults(res) 305 306 want := &result.SuiteResult{ 307 Environment: "env", 308 ProjectName: "name", 309 Tags: "tags", 310 SpecResults: []*result.SpecResult{ 311 { 312 ProtoSpec: &gm.ProtoSpec{ 313 SpecHeading: "heading", FileName: "filename", Tags: []string{"tags"}, 314 Items: []*gm.ProtoItem{ 315 {ItemType: gm.ProtoItem_Scenario, Scenario: &gm.ProtoScenario{ExecutionStatus: gm.ExecutionStatus_PASSED, ScenarioHeading: "scenario Heading1"}}, 316 }, 317 }, 318 }, 319 }, 320 } 321 if !reflect.DeepEqual(got, want) { 322 t.Errorf("Merge data table spec results failed.\n\tWant: %v\n\tGot: %v", want, got) 323 } 324 } 325 326 func TestGetItems(t *testing.T) { 327 table := &gm.ProtoTable{Headers: &gm.ProtoTableRow{Cells: []string{"a"}}} 328 res := []*result.SpecResult{{ 329 ProtoSpec: &gm.ProtoSpec{ 330 Items: []*gm.ProtoItem{ 331 {ItemType: gm.ProtoItem_Table}, 332 {ItemType: gm.ProtoItem_Scenario}, 333 {ItemType: gm.ProtoItem_TableDrivenScenario}, 334 }, 335 }, 336 }} 337 scnRes := []*gm.ProtoItem{ 338 {ItemType: gm.ProtoItem_Scenario}, {ItemType: gm.ProtoItem_TableDrivenScenario}, {ItemType: gm.ProtoItem_Scenario}, 339 } 340 got := getItems(table, scnRes, res) 341 342 want := []*gm.ProtoItem{{ItemType: gm.ProtoItem_Table, Table: table}, {ItemType: gm.ProtoItem_Scenario}, {ItemType: gm.ProtoItem_TableDrivenScenario}, {ItemType: gm.ProtoItem_Scenario}} 343 344 if !reflect.DeepEqual(got, want) { 345 t.Errorf("Merge data table spec results failed.\n\tWant: %v\n\tGot: %v", want, got) 346 } 347 } 348 349 func TestHasTableDrivenSpec(t *testing.T) { 350 type testcase struct { 351 results []*result.SpecResult 352 want bool 353 } 354 355 cases := []testcase{ 356 { 357 results: []*result.SpecResult{ 358 { 359 ProtoSpec: &gm.ProtoSpec{ 360 IsTableDriven: false, 361 }, 362 }, 363 { 364 ProtoSpec: &gm.ProtoSpec{ 365 IsTableDriven: true, 366 }, 367 }, 368 }, 369 want: true, 370 }, 371 { 372 results: []*result.SpecResult{ 373 { 374 ProtoSpec: &gm.ProtoSpec{ 375 IsTableDriven: false, 376 }, 377 }, 378 { 379 ProtoSpec: &gm.ProtoSpec{ 380 IsTableDriven: false, 381 }, 382 }, 383 }, 384 want: false, 385 }, 386 } 387 388 for _, c := range cases { 389 got := hasTableDrivenSpec(c.results) 390 if got != c.want { 391 t.Errorf("Expected hasTableDrivenSpec to be %t, got %t", c.want, got) 392 } 393 } 394 } 395 396 func TestMergeResultWithMesages(t *testing.T) { 397 got := mergeResults([]*result.SpecResult{ 398 { 399 ProtoSpec: &gm.ProtoSpec{ 400 PreHookFailures: []*gm.ProtoHookFailure{{StackTrace: "stacktrace"}}, 401 SpecHeading: "heading", FileName: "filename", Tags: []string{"tags"}, 402 Items: []*gm.ProtoItem{ 403 {ItemType: gm.ProtoItem_Table, Table: &gm.ProtoTable{Headers: &gm.ProtoTableRow{Cells: []string{"a"}}, Rows: []*gm.ProtoTableRow{{Cells: []string{"b"}}}}}, 404 }, 405 PreHookMessages: []string{"Hello"}, 406 }, ExecutionTime: int64(1), 407 }, 408 { 409 ProtoSpec: &gm.ProtoSpec{ 410 PreHookFailures: []*gm.ProtoHookFailure{{StackTrace: "stacktrace1"}}, 411 SpecHeading: "heading", FileName: "filename", Tags: []string{"tags"}, 412 Items: []*gm.ProtoItem{ 413 {ItemType: gm.ProtoItem_Table, Table: &gm.ProtoTable{Headers: &gm.ProtoTableRow{Cells: []string{"a"}}, Rows: []*gm.ProtoTableRow{{Cells: []string{"d"}}}}}, 414 }, 415 }, ExecutionTime: int64(2), 416 }, 417 { 418 ProtoSpec: &gm.ProtoSpec{ 419 PreHookFailures: []*gm.ProtoHookFailure{{StackTrace: "stacktrace2"}}, 420 SpecHeading: "heading", FileName: "filename", Tags: []string{"tags"}, 421 Items: []*gm.ProtoItem{ 422 {ItemType: gm.ProtoItem_Table, Table: &gm.ProtoTable{Headers: &gm.ProtoTableRow{Cells: []string{"a"}}, Rows: []*gm.ProtoTableRow{{Cells: []string{"c"}}}}}, 423 }, 424 PostHookMessages: []string{"Bye"}, 425 }, ExecutionTime: int64(2), 426 }, 427 }) 428 want := &result.SpecResult{ 429 ProtoSpec: &gm.ProtoSpec{ 430 PreHookFailures: []*gm.ProtoHookFailure{{StackTrace: "stacktrace"}, {StackTrace: "stacktrace1", TableRowIndex: 1}, {StackTrace: "stacktrace2", TableRowIndex: 2}}, 431 SpecHeading: "heading", FileName: "filename", Tags: []string{"tags"}, 432 Items: []*gm.ProtoItem{ 433 {ItemType: gm.ProtoItem_Table, Table: &gm.ProtoTable{Headers: &gm.ProtoTableRow{Cells: []string{"a"}}, Rows: []*gm.ProtoTableRow{{Cells: []string{"b"}}, {Cells: []string{"d"}}, {Cells: []string{"c"}}}}}, 434 }, 435 PreHookMessages: []string{"Hello"}, 436 PostHookMessages: []string{"Bye"}, 437 IsTableDriven: false, 438 }, 439 ScenarioCount: 0, ScenarioSkippedCount: 0, ScenarioFailedCount: 0, IsFailed: false, Skipped: false, ExecutionTime: int64(5), 440 } 441 442 if !reflect.DeepEqual(got, want) { 443 t.Errorf("Merge data table spec results failed.\n\tWant: %v\n\tGot: %v", want, got) 444 } 445 }