This repository has been archived by the owner on Aug 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
godog_test.go
98 lines (79 loc) · 1.93 KB
/
godog_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package bootstrap
import (
"flag"
"fmt"
"io/ioutil"
"math/rand"
"path/filepath"
"strings"
"testing"
"github.com/cucumber/godog"
"github.com/nhatthm/surveyexpect"
"github.com/stretchr/testify/assert"
"github.com/nhatthm/surveydog"
)
// Used by init().
//
//nolint:gochecknoglobals
var (
runGoDogTests bool
out = new(surveyexpect.Buffer)
opt = godog.Options{
Strict: true,
Output: out,
}
)
// This has to run on init to define -godog flag, otherwise "undefined flag" error happens.
//
//nolint:gochecknoinits
func init() {
flag.BoolVar(&runGoDogTests, "godog", false, "Set this flag is you want to run godog BDD tests")
godog.BindCommandLineFlags("", &opt)
}
func TestIntegration(t *testing.T) {
if !runGoDogTests {
t.Skip(`Missing "-godog" flag, skipping integration test.`)
}
p := NewPrompt()
m := surveydog.New(t).
WithStarter(p.WithStdio)
RunSuite(t, "..", func(_ *testing.T, ctx *godog.ScenarioContext) {
m.RegisterContext(ctx)
p.RegisterContext(ctx)
})
}
func RunSuite(t *testing.T, path string, featureContext func(t *testing.T, ctx *godog.ScenarioContext)) {
t.Helper()
flag.Parse()
if opt.Randomize == 0 {
opt.Randomize = rand.Int63() // nolint: gosec
}
var paths []string
files, err := ioutil.ReadDir(filepath.Clean(path))
assert.NoError(t, err)
paths = make([]string, 0, len(files))
for _, f := range files {
if strings.HasSuffix(f.Name(), ".feature") {
paths = append(paths, filepath.Join(path, f.Name()))
}
}
for _, path := range paths {
path := path
t.Run(path, func(t *testing.T) {
opt.Paths = []string{path}
suite := godog.TestSuite{
Name: "Integration",
TestSuiteInitializer: nil,
ScenarioInitializer: func(s *godog.ScenarioContext) {
featureContext(t, s)
},
Options: &opt,
}
status := suite.Run()
if status != 0 {
fmt.Println(out.String())
assert.Fail(t, "one or more scenarios failed in feature: "+path)
}
})
}
}