1
- package godogs
1
+ package godogs_test
2
2
3
3
// This example shows how to set up test suite runner with Go subtests and godog command line parameters.
4
4
// Sample commands:
@@ -15,14 +15,18 @@ import (
15
15
"context"
16
16
"flag"
17
17
"fmt"
18
+ "github.com/cucumber/godog/_examples/godogs"
18
19
"os"
19
20
"testing"
20
21
21
22
"github.com/cucumber/godog"
22
23
"github.com/cucumber/godog/colors"
23
24
)
24
25
25
- var opts = godog.Options {Output : colors .Colored (os .Stdout )}
26
+ var opts = godog.Options {
27
+ Output : colors .Colored (os .Stdout ),
28
+ Concurrency : 4 ,
29
+ }
26
30
27
31
func init () {
28
32
godog .BindFlags ("godog." , flag .CommandLine , & opts )
@@ -48,38 +52,54 @@ func TestFeatures(t *testing.T) {
48
52
}
49
53
}
50
54
51
- func thereAreGodogs (available int ) error {
52
- Godogs = available
53
- return nil
55
+ type godogsCtxKey struct {}
56
+
57
+ func godogsToContext (ctx context.Context , g godogs.Godogs ) context.Context {
58
+ return context .WithValue (ctx , godogsCtxKey {}, & g )
54
59
}
55
60
56
- func iEat (num int ) error {
57
- if Godogs < num {
58
- return fmt .Errorf ("you cannot eat %d godogs, there are %d available" , num , Godogs )
59
- }
60
- Godogs -= num
61
- return nil
61
+ func godogsFromContext (ctx context.Context ) * godogs.Godogs {
62
+ g , _ := ctx .Value (godogsCtxKey {}).(* godogs.Godogs )
63
+
64
+ return g
65
+ }
66
+
67
+ // Concurrent execution of scenarios may lead to race conditions on shared resources.
68
+ // Use context to maintain data separation and avoid data races.
69
+
70
+ // Step definition can optionally receive context as a first argument.
71
+
72
+ func thereAreGodogs (ctx context.Context , available int ) {
73
+ godogsFromContext (ctx ).Add (available )
62
74
}
63
75
64
- func thereShouldBeRemaining (remaining int ) error {
65
- if Godogs != remaining {
66
- return fmt .Errorf ("expected %d godogs to be remaining, but there is %d" , remaining , Godogs )
76
+ // Step definition can return error, context, context and error, or nothing.
77
+
78
+ func iEat (ctx context.Context , num int ) error {
79
+ return godogsFromContext (ctx ).Eat (num )
80
+ }
81
+
82
+ func thereShouldBeRemaining (ctx context.Context , remaining int ) error {
83
+ available := godogsFromContext (ctx ).Available ()
84
+ if available != remaining {
85
+ return fmt .Errorf ("expected %d godogs to be remaining, but there is %d" , remaining , available )
67
86
}
87
+
68
88
return nil
69
89
}
70
90
71
- func thereShouldBeNoneRemaining () error {
72
- return thereShouldBeRemaining (0 )
91
+ func thereShouldBeNoneRemaining (ctx context. Context ) error {
92
+ return thereShouldBeRemaining (ctx , 0 )
73
93
}
74
94
75
95
func InitializeTestSuite (ctx * godog.TestSuiteContext ) {
76
- ctx .BeforeSuite (func () { Godogs = 0 })
96
+ ctx .BeforeSuite (func () { fmt . Println ( "Get the party started!" ) })
77
97
}
78
98
79
99
func InitializeScenario (ctx * godog.ScenarioContext ) {
80
100
ctx .Before (func (ctx context.Context , sc * godog.Scenario ) (context.Context , error ) {
81
- Godogs = 0 // clean the state before every scenario
82
- return ctx , nil
101
+ // Add initial godogs to context.
102
+ return godogsToContext ( ctx , 0 ) , nil
83
103
})
84
104
85
105
ctx .Step (`^there are (\d+) godogs$` , thereAreGodogs )
0 commit comments