gitee.com/mirrors/gauge@v1.0.6/execution/scenarioExecutor_test.go (about)

     1  // Copyright 2015 ThoughtWorks, Inc.
     2  
     3  // This file is part of Gauge.
     4  
     5  // Gauge is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  
    10  // Gauge is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  
    15  // You should have received a copy of the GNU General Public License
    16  // along with Gauge.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package execution
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/getgauge/gauge/execution/result"
    24  	"github.com/getgauge/gauge/gauge"
    25  
    26  	"github.com/getgauge/gauge/gauge_messages"
    27  )
    28  
    29  func TestNotifyBeforeScenarioShouldAddBeforeScenarioHookMessages(t *testing.T) {
    30  	r := &mockRunner{}
    31  	h := &mockPluginHandler{NotifyPluginsfunc: func(m *gauge_messages.Message) {}, GracefullyKillPluginsfunc: func() {}}
    32  	r.ExecuteAndGetStatusFunc = func(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult {
    33  		if m.MessageType == gauge_messages.Message_ScenarioExecutionStarting {
    34  			return &gauge_messages.ProtoExecutionResult{
    35  				Message:       []string{"Before Scenario Called"},
    36  				Failed:        false,
    37  				ExecutionTime: 10,
    38  			}
    39  		}
    40  		return &gauge_messages.ProtoExecutionResult{}
    41  	}
    42  	ei := &gauge_messages.ExecutionInfo{}
    43  	sce := newScenarioExecutor(r, h, ei, nil, nil, nil, 0)
    44  	scenario := &gauge.Scenario{
    45  		Heading: &gauge.Heading{Value: "A scenario"},
    46  		Span:    &gauge.Span{Start: 2, End: 10},
    47  	}
    48  	scenarioResult := result.NewScenarioResult(gauge.NewProtoScenario(scenario))
    49  	sce.notifyBeforeScenarioHook(scenarioResult)
    50  	gotMessages := scenarioResult.ProtoScenario.PreHookMessages
    51  
    52  	if len(gotMessages) != 1 {
    53  		t.Errorf("Expected 1 message, got : %d", len(gotMessages))
    54  	}
    55  	if gotMessages[0] != "Before Scenario Called" {
    56  		t.Errorf("Expected `Before Scenario Called` message, got : %s", gotMessages[0])
    57  	}
    58  }
    59  
    60  func TestNotifyAfterScenarioShouldAddAfterScenarioHookMessages(t *testing.T) {
    61  	r := &mockRunner{}
    62  	h := &mockPluginHandler{NotifyPluginsfunc: func(m *gauge_messages.Message) {}, GracefullyKillPluginsfunc: func() {}}
    63  	r.ExecuteAndGetStatusFunc = func(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult {
    64  		if m.MessageType == gauge_messages.Message_ScenarioExecutionEnding {
    65  			return &gauge_messages.ProtoExecutionResult{
    66  				Message:       []string{"After Scenario Called"},
    67  				Failed:        false,
    68  				ExecutionTime: 10,
    69  			}
    70  		}
    71  		return &gauge_messages.ProtoExecutionResult{}
    72  	}
    73  	ei := &gauge_messages.ExecutionInfo{}
    74  	sce := newScenarioExecutor(r, h, ei, nil, nil, nil, 0)
    75  	scenario := &gauge.Scenario{
    76  		Heading: &gauge.Heading{Value: "A scenario"},
    77  		Span:    &gauge.Span{Start: 2, End: 10},
    78  	}
    79  	scenarioResult := result.NewScenarioResult(gauge.NewProtoScenario(scenario))
    80  	sce.notifyAfterScenarioHook(scenarioResult)
    81  	gotMessages := scenarioResult.ProtoScenario.PostHookMessages
    82  
    83  	if len(gotMessages) != 1 {
    84  		t.Errorf("Expected 1 message, got : %d", len(gotMessages))
    85  	}
    86  	if gotMessages[0] != "After Scenario Called" {
    87  		t.Errorf("Expected `After Scenario Called` message, got : %s", gotMessages[0])
    88  	}
    89  }
    90  
    91  func TestNotifyBeforeScenarioShouldAddBeforeScenarioHookScreenshots(t *testing.T) {
    92  	r := &mockRunner{}
    93  	h := &mockPluginHandler{NotifyPluginsfunc: func(m *gauge_messages.Message) {}, GracefullyKillPluginsfunc: func() {}}
    94  	r.ExecuteAndGetStatusFunc = func(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult {
    95  		if m.MessageType == gauge_messages.Message_ScenarioExecutionStarting {
    96  			return &gauge_messages.ProtoExecutionResult{
    97  				Screenshots:   [][]byte{[]byte("screenshot1"), []byte("screenshot2")},
    98  				Failed:        false,
    99  				ExecutionTime: 10,
   100  			}
   101  		}
   102  		return &gauge_messages.ProtoExecutionResult{}
   103  	}
   104  	ei := &gauge_messages.ExecutionInfo{}
   105  	sce := newScenarioExecutor(r, h, ei, nil, nil, nil, 0)
   106  	scenario := &gauge.Scenario{
   107  		Heading: &gauge.Heading{Value: "A scenario"},
   108  		Span:    &gauge.Span{Start: 2, End: 10},
   109  	}
   110  	scenarioResult := result.NewScenarioResult(gauge.NewProtoScenario(scenario))
   111  	sce.notifyBeforeScenarioHook(scenarioResult)
   112  	beforeScenarioScreenShots := scenarioResult.ProtoScenario.PreHookScreenshots
   113  	expected := []string{"screenshot1", "screenshot2"}
   114  
   115  	if len(beforeScenarioScreenShots) != len(expected) {
   116  		t.Errorf("Expected 2 screenshots, got : %d", len(beforeScenarioScreenShots))
   117  	}
   118  
   119  	for i, e := range expected {
   120  		if string(beforeScenarioScreenShots[i]) != e {
   121  			t.Errorf("Expected `%s` screenshot, got : %s", e, beforeScenarioScreenShots[i])
   122  		}
   123  	}
   124  }
   125  
   126  func TestNotifyAfterScenarioShouldAddAfterScenarioHookScreenshots(t *testing.T) {
   127  	r := &mockRunner{}
   128  	h := &mockPluginHandler{NotifyPluginsfunc: func(m *gauge_messages.Message) {}, GracefullyKillPluginsfunc: func() {}}
   129  	r.ExecuteAndGetStatusFunc = func(m *gauge_messages.Message) *gauge_messages.ProtoExecutionResult {
   130  		if m.MessageType == gauge_messages.Message_ScenarioExecutionEnding {
   131  			return &gauge_messages.ProtoExecutionResult{
   132  				Screenshots:   [][]byte{[]byte("screenshot1"), []byte("screenshot2")},
   133  				Failed:        false,
   134  				ExecutionTime: 10,
   135  			}
   136  		}
   137  		return &gauge_messages.ProtoExecutionResult{}
   138  	}
   139  	ei := &gauge_messages.ExecutionInfo{}
   140  	sce := newScenarioExecutor(r, h, ei, nil, nil, nil, 0)
   141  	scenario := &gauge.Scenario{
   142  		Heading: &gauge.Heading{Value: "A scenario"},
   143  		Span:    &gauge.Span{Start: 2, End: 10},
   144  	}
   145  	scenarioResult := result.NewScenarioResult(gauge.NewProtoScenario(scenario))
   146  	sce.notifyAfterScenarioHook(scenarioResult)
   147  	afterScenarioScreenShots := scenarioResult.ProtoScenario.PostHookScreenshots
   148  	expected := []string{"screenshot1", "screenshot2"}
   149  
   150  	if len(afterScenarioScreenShots) != len(expected) {
   151  		t.Errorf("Expected 2 screenshots, got : %d", len(afterScenarioScreenShots))
   152  	}
   153  
   154  	for i, e := range expected {
   155  		if string(afterScenarioScreenShots[i]) != e {
   156  			t.Errorf("Expected `%s` screenshot, got : %s", e, afterScenarioScreenShots[i])
   157  		}
   158  	}
   159  }