-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcucumber_test.go
85 lines (68 loc) · 2.44 KB
/
cucumber_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
package cucumber_test
import (
"fmt"
"io/ioutil"
"testing"
"github.com/pranas/cucumber-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRun(t *testing.T) {
summary := cucumber.NewSummaryFormatter(ioutil.Discard)
s, err := cucumber.NewSuite(cucumber.Config{Formatter:summary})
require.NoError(t, err)
exitCode := s.Run()
assert.Equal(t, 1, exitCode)
assert.False(t, summary.Success)
assert.Equal(t, 2, summary.TestCasesTotal)
assert.Equal(t, 0, summary.TestCasesPassed)
assert.Equal(t, 4, summary.StepsTotal)
assert.Equal(t, 0, summary.StepsPassed)
summary = cucumber.NewSummaryFormatter(ioutil.Discard)
s, err = cucumber.NewSuite(cucumber.Config{Formatter:summary})
require.NoError(t, err)
s.DefineStep(`^you concat "([^"]*)" and "([^"]*)"$`, concat)
s.DefineStep(`^you should have "([^"]*)"$`, matchOutput)
exitCode = s.Run()
assert.Equal(t, 0, exitCode)
assert.True(t, summary.Success)
assert.Equal(t, 2, summary.TestCasesTotal)
assert.Equal(t, 2, summary.TestCasesPassed)
assert.Equal(t, 4, summary.StepsTotal)
assert.Equal(t, 4, summary.StepsPassed)
summary = cucumber.NewSummaryFormatter(ioutil.Discard)
s, err = cucumber.NewSuite(cucumber.Config{Formatter:summary}, "features/concat.feature:6")
require.NoError(t, err)
exitCode = s.Run()
assert.Equal(t, 1, exitCode)
assert.False(t, summary.Success)
assert.Equal(t, 1, summary.TestCasesTotal)
assert.Equal(t, 0, summary.TestCasesPassed)
assert.Equal(t, 2, summary.StepsTotal)
assert.Equal(t, 0, summary.StepsPassed)
summary = cucumber.NewSummaryFormatter(ioutil.Discard)
s, err = cucumber.NewSuite(cucumber.Config{Formatter:summary})
require.NoError(t, err)
s.DefineStep(`^you concat "([^"]*)" and "([^"]*)"$`, concat)
s.DefineStep(`^you should have "([^"]*)"$`, func(_ cucumber.TestCase, s ...string) error {
panic("should recover from this")
})
exitCode = s.Run()
assert.Equal(t, 1, exitCode)
assert.False(t, summary.Success)
assert.Equal(t, 2, summary.TestCasesTotal)
assert.Equal(t, 0, summary.TestCasesPassed)
assert.Equal(t, 4, summary.StepsTotal)
assert.Equal(t, 2, summary.StepsPassed)
}
func concat(tc cucumber.TestCase, matches ...string) error {
tc.Set("state", matches[0] + matches[1])
return nil
}
func matchOutput(tc cucumber.TestCase, expected ...string) error {
actual := tc.Get("state").(string)
if actual != expected[0] {
return fmt.Errorf("expected %s but got %s", expected[0], actual)
}
return nil
}