github.com/maps90/godog@v0.7.5-0.20170923143419-0093943021d4/examples/godogs/godogs_test.go (about)

     1  /* file: $GOPATH/src/godogs/godogs_test.go */
     2  package main
     3  
     4  import (
     5  	"fmt"
     6  	"os"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/DATA-DOG/godog"
    11  )
    12  
    13  func TestMain(m *testing.M) {
    14  	status := godog.RunWithOptions("godogs", func(s *godog.Suite) {
    15  		FeatureContext(s)
    16  	}, godog.Options{
    17  		Format:    "progress",
    18  		Paths:     []string{"features"},
    19  		Randomize: time.Now().UTC().UnixNano(), // randomize scenario execution order
    20  	})
    21  
    22  	if st := m.Run(); st > status {
    23  		status = st
    24  	}
    25  	os.Exit(status)
    26  }
    27  
    28  func thereAreGodogs(available int) error {
    29  	Godogs = available
    30  	return nil
    31  }
    32  
    33  func iEat(num int) error {
    34  	if Godogs < num {
    35  		return fmt.Errorf("you cannot eat %d godogs, there are %d available", num, Godogs)
    36  	}
    37  	Godogs -= num
    38  	return nil
    39  }
    40  
    41  func thereShouldBeRemaining(remaining int) error {
    42  	if Godogs != remaining {
    43  		return fmt.Errorf("expected %d godogs to be remaining, but there is %d", remaining, Godogs)
    44  	}
    45  	return nil
    46  }
    47  
    48  func FeatureContext(s *godog.Suite) {
    49  	s.Step(`^there are (\d+) godogs$`, thereAreGodogs)
    50  	s.Step(`^I eat (\d+)$`, iEat)
    51  	s.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining)
    52  
    53  	s.BeforeScenario(func(interface{}) {
    54  		Godogs = 0 // clean the state before every scenario
    55  	})
    56  }