github.com/getgauge/gauge@v1.6.9/execution/simpleExecution_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  	"github.com/getgauge/gauge/execution/result"
    13  	"github.com/getgauge/gauge/gauge"
    14  
    15  	"github.com/getgauge/gauge-proto/go/gauge_messages"
    16  )
    17  
    18  func TestNotifyBeforeSuiteShouldAddsBeforeSuiteHookMessages(t *testing.T) {
    19  	r := &mockRunner{}
    20  	h := &mockPluginHandler{NotifyPluginsfunc: func(m *gauge_messages.Message) {}, GracefullyKillPluginsfunc: func() {}}
    21  	r.ExecuteAndGetStatusFunc = func(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult {
    22  		if m.MessageType == gauge_messages.Message_ExecutionStarting {
    23  			return &gauge_messages.ProtoExecutionResult{
    24  				Message:       []string{"Before Suite Called"},
    25  				Failed:        false,
    26  				ExecutionTime: 10,
    27  			}
    28  		}
    29  		return &gauge_messages.ProtoExecutionResult{}
    30  	}
    31  	ei := &executionInfo{runner: r, pluginHandler: h}
    32  	simpleExecution := newSimpleExecution(ei, false, false)
    33  	simpleExecution.suiteResult = result.NewSuiteResult(ExecuteTags, simpleExecution.startTime)
    34  	simpleExecution.notifyBeforeSuite()
    35  
    36  	gotMessages := simpleExecution.suiteResult.PreHookMessages
    37  
    38  	if len(gotMessages) != 1 {
    39  		t.Errorf("Expected 1 message, got : %d", len(gotMessages))
    40  	}
    41  	if gotMessages[0] != "Before Suite Called" {
    42  		t.Errorf("Expected `Before Suite Called` message, got : %s", gotMessages[0])
    43  	}
    44  }
    45  
    46  func TestNotifyAfterSuiteShouldAddsAfterSuiteHookMessages(t *testing.T) {
    47  	r := &mockRunner{}
    48  	h := &mockPluginHandler{NotifyPluginsfunc: func(m *gauge_messages.Message) {}, GracefullyKillPluginsfunc: func() {}}
    49  	r.ExecuteAndGetStatusFunc = func(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult {
    50  		if m.MessageType == gauge_messages.Message_ExecutionEnding {
    51  			return &gauge_messages.ProtoExecutionResult{
    52  				Message:       []string{"After Suite Called"},
    53  				Failed:        false,
    54  				ExecutionTime: 10,
    55  			}
    56  		}
    57  		return &gauge_messages.ProtoExecutionResult{}
    58  	}
    59  	ei := &executionInfo{runner: r, pluginHandler: h}
    60  	simpleExecution := newSimpleExecution(ei, false, false)
    61  	simpleExecution.suiteResult = result.NewSuiteResult(ExecuteTags, simpleExecution.startTime)
    62  	simpleExecution.notifyAfterSuite()
    63  
    64  	gotMessages := simpleExecution.suiteResult.PostHookMessages
    65  
    66  	if len(gotMessages) != 1 {
    67  		t.Errorf("Expected 1 message, got : %d", len(gotMessages))
    68  	}
    69  	if gotMessages[0] != "After Suite Called" {
    70  		t.Errorf("Expected `After Suite Called` message, got : %s", gotMessages[0])
    71  	}
    72  }
    73  
    74  func TestNotifyBeforeSuiteShouldAddsBeforeSuiteHookScreenshots(t *testing.T) {
    75  	r := &mockRunner{}
    76  	h := &mockPluginHandler{NotifyPluginsfunc: func(m *gauge_messages.Message) {}, GracefullyKillPluginsfunc: func() {}}
    77  	r.ExecuteAndGetStatusFunc = func(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult {
    78  		if m.MessageType == gauge_messages.Message_ExecutionStarting {
    79  			return &gauge_messages.ProtoExecutionResult{
    80  				ScreenshotFiles: []string{"screenshot1.png", "screenshot2.png"},
    81  				Failed:          false,
    82  				ExecutionTime:   10,
    83  			}
    84  		}
    85  		return &gauge_messages.ProtoExecutionResult{}
    86  	}
    87  	ei := &executionInfo{runner: r, pluginHandler: h}
    88  	simpleExecution := newSimpleExecution(ei, false, false)
    89  	simpleExecution.suiteResult = result.NewSuiteResult(ExecuteTags, simpleExecution.startTime)
    90  	simpleExecution.notifyBeforeSuite()
    91  
    92  	beforeSuiteScreenshots := simpleExecution.suiteResult.PreHookScreenshotFiles
    93  	expected := []string{"screenshot1.png", "screenshot2.png"}
    94  
    95  	if len(beforeSuiteScreenshots) != len(expected) {
    96  		t.Errorf("Expected 2 screenshots, got : %d", len(beforeSuiteScreenshots))
    97  	}
    98  	for i, e := range expected {
    99  		if string(beforeSuiteScreenshots[i]) != e {
   100  			t.Errorf("Expected `%s` screenshot, got : %s", e, beforeSuiteScreenshots[i])
   101  		}
   102  	}
   103  }
   104  
   105  func TestNotifyAfterSuiteShouldAddsAfterSuiteHookScreenshots(t *testing.T) {
   106  	r := &mockRunner{}
   107  	h := &mockPluginHandler{NotifyPluginsfunc: func(m *gauge_messages.Message) {}, GracefullyKillPluginsfunc: func() {}}
   108  	r.ExecuteAndGetStatusFunc = func(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult {
   109  		if m.MessageType == gauge_messages.Message_ExecutionEnding {
   110  			return &gauge_messages.ProtoExecutionResult{
   111  				ScreenshotFiles: []string{"screenshot1.png", "screenshot2.png"},
   112  				Failed:          false,
   113  				ExecutionTime:   10,
   114  			}
   115  		}
   116  		return &gauge_messages.ProtoExecutionResult{}
   117  	}
   118  	ei := &executionInfo{runner: r, pluginHandler: h}
   119  	simpleExecution := newSimpleExecution(ei, false, false)
   120  	simpleExecution.suiteResult = result.NewSuiteResult(ExecuteTags, simpleExecution.startTime)
   121  	simpleExecution.notifyAfterSuite()
   122  
   123  	afterSuiteScreenshots := simpleExecution.suiteResult.PostHookScreenshotFiles
   124  	expected := []string{"screenshot1.png", "screenshot2.png"}
   125  
   126  	if len(afterSuiteScreenshots) != len(expected) {
   127  		t.Errorf("Expected 2 screenshots, got : %d", len(afterSuiteScreenshots))
   128  	}
   129  	for i, e := range expected {
   130  		if string(afterSuiteScreenshots[i]) != e {
   131  			t.Errorf("Expected `%s` screenshot, got : %s", e, afterSuiteScreenshots[i])
   132  		}
   133  	}
   134  }
   135  func TestExecuteSpecsShouldAddsBeforeSpecHookFailureScreenshotFile(t *testing.T) {
   136  	r := &mockRunner{}
   137  	h := &mockPluginHandler{NotifyPluginsfunc: func(m *gauge_messages.Message) {}, GracefullyKillPluginsfunc: func() {}}
   138  	r.ExecuteAndGetStatusFunc = func(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult {
   139  		if m.MessageType == gauge_messages.Message_SpecExecutionStarting {
   140  			return &gauge_messages.ProtoExecutionResult{
   141  				Failed:                true,
   142  				ExecutionTime:         10,
   143  				FailureScreenshotFile: "before-spec-hook-failure-screenshot.png",
   144  			}
   145  		}
   146  		return &gauge_messages.ProtoExecutionResult{}
   147  	}
   148  	ei := &executionInfo{runner: r, pluginHandler: h, errMaps: &gauge.BuildErrors{}}
   149  	simpleExecution := newSimpleExecution(ei, false, false)
   150  	specsC := createSpecCollection()
   151  	simpleExecution.suiteResult = result.NewSuiteResult(ExecuteTags, simpleExecution.startTime)
   152  	specResult := simpleExecution.executeSpecs(specsC)
   153  
   154  	actualScreenshotFile := specResult[0].ProtoSpec.PreHookFailures[0].FailureScreenshotFile
   155  	expectedScreenshotFile := "before-spec-hook-failure-screenshot.png"
   156  
   157  	if actualScreenshotFile != expectedScreenshotFile {
   158  		t.Errorf("Expected `%s` screenshot, got : %s", expectedScreenshotFile, actualScreenshotFile)
   159  	}
   160  }
   161  
   162  func TestExecuteSpecsShouldAddsAfterSpecHookFailureScreenshotFile(t *testing.T) {
   163  	r := &mockRunner{}
   164  	h := &mockPluginHandler{NotifyPluginsfunc: func(m *gauge_messages.Message) {}, GracefullyKillPluginsfunc: func() {}}
   165  	r.ExecuteAndGetStatusFunc = func(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult {
   166  		if m.MessageType == gauge_messages.Message_SpecExecutionEnding {
   167  			return &gauge_messages.ProtoExecutionResult{
   168  				Failed:                true,
   169  				ExecutionTime:         10,
   170  				FailureScreenshotFile: "after-spec-hook-failure-screenshot.png",
   171  			}
   172  		}
   173  		return &gauge_messages.ProtoExecutionResult{}
   174  	}
   175  	ei := &executionInfo{runner: r, pluginHandler: h, errMaps: &gauge.BuildErrors{}}
   176  	simpleExecution := newSimpleExecution(ei, false, false)
   177  	specsC := createSpecCollection()
   178  	simpleExecution.suiteResult = result.NewSuiteResult(ExecuteTags, simpleExecution.startTime)
   179  	specResult := simpleExecution.executeSpecs(specsC)
   180  
   181  	actualScreenshotFile := specResult[0].ProtoSpec.PostHookFailures[0].FailureScreenshotFile
   182  	expectedScreenshotFile := "after-spec-hook-failure-screenshot.png"
   183  
   184  	if actualScreenshotFile != expectedScreenshotFile {
   185  		t.Errorf("Expected `%s` screenshot, got : %s", expectedScreenshotFile, actualScreenshotFile)
   186  	}
   187  }
   188  
   189  func createSpecCollection() *gauge.SpecCollection {
   190  	var specs []*gauge.Specification
   191  	specs = append(specs, &gauge.Specification{
   192  		FileName: "spec-1.spec",
   193  		Heading: &gauge.Heading{
   194  			Value:       "Spec heading",
   195  			LineNo:      1,
   196  			SpanEnd:     12,
   197  			HeadingType: 0,
   198  		},
   199  	})
   200  	return gauge.NewSpecCollection(specs, false)
   201  }