-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature.go
91 lines (72 loc) · 1.79 KB
/
feature.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
// Package feature provides a bootstrap functionality to run feature test with godog.
package feature
import (
"bytes"
"flag"
"fmt"
"math/rand"
"testing"
"github.com/bool64/godogx"
"github.com/bool64/godogx/allure"
"github.com/cucumber/godog"
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
)
// Used by init().
//
//nolint:gochecknoglobals
var (
runWithTags string
runFeature string
runAllure bool
out = bytes.Buffer{}
opt = godog.Options{
Tags: runWithTags,
Strict: true,
Output: &out,
}
)
// This has to run on init to define -feature flag.
//
//nolint:gochecknoinits
func init() {
flagSet := pflag.CommandLine
flagSet.StringVar(&runFeature, "feature", "",
"Optional feature to run. Filename without the extension .feature")
flag.BoolVar(&runAllure, "allure", false,
"Enable allure formatter")
godog.BindCommandLineFlags("", &opt)
}
// RunFeatures bootstrap and run feature tests.
func RunFeatures(t *testing.T, path string, featureContext func(t *testing.T, s *godog.ScenarioContext)) {
t.Helper()
if testing.Short() {
t.Skip("skipping test in short mode.")
}
flag.Parse()
if runFeature != "" {
path = fmt.Sprintf("%s/%s.feature", path, runFeature)
}
godogx.RegisterPrettyFailedFormatter()
if opt.Randomize == 0 {
opt.Randomize = rand.Int63() // nolint: gosec
}
if opt.Format == "" {
opt.Format = "pretty-failed"
}
opt.Paths = []string{path}
opt.TestingT = t
opt.StopOnFailure = true
suite := godog.TestSuite{
Name: "Integration test",
ScenarioInitializer: func(s *godog.ScenarioContext) {
featureContext(t, s)
},
Options: &opt,
}
if runAllure {
allure.RegisterFormatter()
suite.Options.Format += ",allure"
}
assert.Equal(t, 0, suite.Run(), "non-zero status returned, failed to run feature tests")
}