organize test steps
Этот коммит содержится в:
родитель
ebc2621555
коммит
fc1f94c999
1 изменённых файлов: 30 добавлений и 15 удалений
|
@ -2,6 +2,7 @@ package godog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -14,21 +15,30 @@ type firedEvent struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type suiteFeature struct {
|
type suiteFeature struct {
|
||||||
testedSuite *suite
|
testedSuite *suite
|
||||||
events []*firedEvent
|
events []*firedEvent
|
||||||
|
tempFeatures []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *suiteFeature) HandleBeforeScenario(scenario *gherkin.Scenario) {
|
func (s *suiteFeature) HandleBeforeScenario(*gherkin.Scenario) {
|
||||||
// reset whole suite with the state
|
// reset whole suite with the state
|
||||||
s.testedSuite = &suite{fmt: &testFormatter{}}
|
s.testedSuite = &suite{fmt: &testFormatter{}}
|
||||||
// our tested suite will have the same context registered
|
// our tested suite will have the same context registered
|
||||||
SuiteContext(s.testedSuite)
|
SuiteContext(s.testedSuite)
|
||||||
// reset feature paths
|
// reset feature paths
|
||||||
cfg.paths = []string{}
|
cfg.paths = []string{}
|
||||||
|
s.tempFeatures = []string{}
|
||||||
// reset all fired events
|
// reset all fired events
|
||||||
s.events = []*firedEvent{}
|
s.events = []*firedEvent{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *suiteFeature) HandleAfterScenario(*gherkin.Scenario) {
|
||||||
|
// remove temp files
|
||||||
|
for _, f := range s.tempFeatures {
|
||||||
|
os.Remove("/tmp/" + f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (s *suiteFeature) iAmListeningToSuiteEvents(args ...*Arg) error {
|
func (s *suiteFeature) iAmListeningToSuiteEvents(args ...*Arg) error {
|
||||||
s.testedSuite.BeforeSuite(BeforeSuiteHandlerFunc(func() {
|
s.testedSuite.BeforeSuite(BeforeSuiteHandlerFunc(func() {
|
||||||
s.events = append(s.events, &firedEvent{"BeforeSuite", []interface{}{}})
|
s.events = append(s.events, &firedEvent{"BeforeSuite", []interface{}{}})
|
||||||
|
@ -51,6 +61,14 @@ func (s *suiteFeature) iAmListeningToSuiteEvents(args ...*Arg) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *suiteFeature) aFailingStep(...*Arg) error {
|
||||||
|
return fmt.Errorf("intentional failure")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *suiteFeature) tempFeatureFile(args ...*Arg) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *suiteFeature) featurePath(args ...*Arg) error {
|
func (s *suiteFeature) featurePath(args ...*Arg) error {
|
||||||
cfg.paths = append(cfg.paths, args[0].String())
|
cfg.paths = append(cfg.paths, args[0].String())
|
||||||
return nil
|
return nil
|
||||||
|
@ -159,24 +177,18 @@ func SuiteContext(g Suite) {
|
||||||
|
|
||||||
g.BeforeScenario(s)
|
g.BeforeScenario(s)
|
||||||
|
|
||||||
g.Step(
|
g.Step(regexp.MustCompile(`^a feature path "([^"]*)"$`), StepHandlerFunc(s.featurePath))
|
||||||
regexp.MustCompile(`^a feature path "([^"]*)"$`),
|
g.Step(regexp.MustCompile(`^I parse features$`), StepHandlerFunc(s.parseFeatures))
|
||||||
StepHandlerFunc(s.featurePath))
|
g.Step(regexp.MustCompile(`^I'm listening to suite events$`), StepHandlerFunc(s.iAmListeningToSuiteEvents))
|
||||||
g.Step(
|
g.Step(regexp.MustCompile(`^I run feature suite$`), StepHandlerFunc(s.iRunFeatureSuite))
|
||||||
regexp.MustCompile(`^I parse features$`),
|
g.Step(regexp.MustCompile(`^feature "([^"]*)" file:$`), StepHandlerFunc(s.tempFeatureFile))
|
||||||
StepHandlerFunc(s.parseFeatures))
|
|
||||||
g.Step(
|
g.Step(
|
||||||
regexp.MustCompile(`^I should have ([\d]+) features? files?:$`),
|
regexp.MustCompile(`^I should have ([\d]+) features? files?:$`),
|
||||||
StepHandlerFunc(s.iShouldHaveNumFeatureFiles))
|
StepHandlerFunc(s.iShouldHaveNumFeatureFiles))
|
||||||
g.Step(
|
g.Step(
|
||||||
regexp.MustCompile(`^I should have ([\d]+) scenarios? registered$`),
|
regexp.MustCompile(`^I should have ([\d]+) scenarios? registered$`),
|
||||||
StepHandlerFunc(s.numScenariosRegistered))
|
StepHandlerFunc(s.numScenariosRegistered))
|
||||||
g.Step(
|
|
||||||
regexp.MustCompile(`^I'm listening to suite events$`),
|
|
||||||
StepHandlerFunc(s.iAmListeningToSuiteEvents))
|
|
||||||
g.Step(
|
|
||||||
regexp.MustCompile(`^I run feature suite$`),
|
|
||||||
StepHandlerFunc(s.iRunFeatureSuite))
|
|
||||||
g.Step(
|
g.Step(
|
||||||
regexp.MustCompile(`^there (was|were) ([\d]+) "([^"]*)" events? fired$`),
|
regexp.MustCompile(`^there (was|were) ([\d]+) "([^"]*)" events? fired$`),
|
||||||
StepHandlerFunc(s.thereWereNumEventsFired))
|
StepHandlerFunc(s.thereWereNumEventsFired))
|
||||||
|
@ -186,4 +198,7 @@ func SuiteContext(g Suite) {
|
||||||
g.Step(
|
g.Step(
|
||||||
regexp.MustCompile(`^these events had to be fired for a number of times:$`),
|
regexp.MustCompile(`^these events had to be fired for a number of times:$`),
|
||||||
StepHandlerFunc(s.theseEventsHadToBeFiredForNumberOfTimes))
|
StepHandlerFunc(s.theseEventsHadToBeFiredForNumberOfTimes))
|
||||||
|
|
||||||
|
g.Step(regexp.MustCompile(`^a failing step`), StepHandlerFunc(s.aFailingStep))
|
||||||
|
g.Step(regexp.MustCompile(`^this step should fail`), StepHandlerFunc(s.aFailingStep))
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче