You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update README to reflect current best practices in creating and running tests (cucumber#477)
* Update README to reflect current best practices in creating and running tests
* Update CHANGELOG and README
Co-authored-by: Matt Wynne <[email protected]>
Copy file name to clipboardexpand all lines: CHANGELOG.md
+3
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,9 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt
8
8
9
9
## Unreleased
10
10
11
+
### Changed
12
+
- README example is updated with `context.Context` and `go test` usage. ([477](https://github.com/cucumber/godog/pull/477) - [vearutop](https://github.com/vearutop))
13
+
11
14
## [v0.12.5]
12
15
### Changed
13
16
- Changed underlying cobra command setup to return errors instead of calling `os.Exit` directly to enable simpler testing. ([454](https://github.com/cucumber/godog/pull/454) - [mxygem](https://github.com/mxygem))
Copy file name to clipboardexpand all lines: README.md
+99-83
Original file line number
Diff line number
Diff line change
@@ -15,10 +15,6 @@ Please read the full README, you may find it very useful. And do not forget to p
15
15
16
16
Package godog is the official Cucumber BDD framework for Golang, it merges specification and test documentation into one cohesive whole, using Gherkin formatted scenarios in the format of Given, When, Then.
17
17
18
-
**Godog** does not intervene with the standard **go test** command behavior. You can leverage both frameworks to functionally test your application while maintaining all test related source code in **_test.go** files.
19
-
20
-
**Godog** acts similar compared to **go test** command, by using go compiler and linker tool in order to produce test executable. Godog contexts need to be exported the same way as **Test** functions for go tests. Note, that if you use **godog** command tool, it will use `go` executable to determine compiler and linker.
21
-
22
18
The project was inspired by [behat][behat] and [cucumber][cucumber].
23
19
24
20
## Why Godog/Cucumber
@@ -44,18 +40,6 @@ When automated testing is this much fun, teams can easily protect themselves fro
go install github.com/cucumber/godog/cmd/godog@v0.12.0
50
-
```
51
-
Adding `@v0.12.0` will install v0.12.0 specifically instead of master.
52
-
53
-
With `go` version prior to 1.17, use `go get github.com/cucumber/godog/cmd/godog@v0.12.0`.
54
-
Running `within the $GOPATH`, you would also need to set `GO111MODULE=on`, like this:
55
-
```
56
-
GO111MODULE=on go get github.com/cucumber/godog/cmd/godog@v0.12.0
57
-
```
58
-
59
43
## Contributions
60
44
61
45
Godog is a community driven Open Source Project within the Cucumber organization. We [welcome contributions from everyone](https://cucumber.io/blog/open-source/tackling-structural-racism-(and-sexism)-in-open-so/), and we're ready to support you if you have the enthusiasm to contribute.
@@ -92,13 +76,13 @@ Initiate the go module - `go mod init godogs`
92
76
93
77
#### Step 2 - Install godog
94
78
95
-
Install the godog binary - `go get github.com/cucumber/godog/cmd/godog`
79
+
Install the `godog` binary - `go install github.com/cucumber/godog/cmd/godog@latest`.
96
80
97
81
#### Step 3 - Create gherkin feature
98
82
99
83
Imagine we have a **godog cart** to serve godogs for lunch.
100
84
101
-
First of all, we describe our feature in plain text - `vim features/godogs.feature`
85
+
First of all, we describe our feature in plain text - `vim features/godogs.feature`.
102
86
103
87
```gherkin
104
88
Feature: eat godogs
@@ -116,7 +100,7 @@ Feature: eat godogs
116
100
117
101
**NOTE:** same as **go test** godog respects package level isolation. All your step definitions should be in your tested package root directory. In this case: **godogs**.
118
102
119
-
If we run godog inside the module: - `godog`
103
+
If we run godog inside the module: - `godog run`
120
104
121
105
You should see that the steps are undefined:
122
106
```
@@ -190,7 +174,7 @@ godogs
190
174
- godogs_test.go
191
175
```
192
176
193
-
Run godog again - `godog`
177
+
Run godog again - `godog run`
194
178
195
179
You should now see that the scenario is pending with one step pending and two steps skipped:
196
180
```
@@ -255,101 +239,111 @@ Replace the contents of `godogs_test.go` with the code below - `vim godogs_test.
255
239
package main
256
240
257
241
import (
258
-
"context"
259
-
"fmt"
242
+
"context"
243
+
"errors"
244
+
"fmt"
245
+
"testing"
260
246
261
-
"github.com/cucumber/godog"
247
+
"github.com/cucumber/godog"
262
248
)
263
249
264
-
functhereAreGodogs(availableint) error {
265
-
Godogs = available
266
-
returnnil
267
-
}
250
+
// godogsCtxKey is the key used to store the available godogs in the context.Context.
251
+
type godogsCtxKey struct{}
268
252
269
-
funciEat(numint) error {
270
-
if Godogs < num {
271
-
return fmt.Errorf("you cannot eat %d godogs, there are %d available", num, Godogs)
sc.Step(`^there are (\d+) godogs$`, thereAreGodogs)
302
+
sc.Step(`^I eat (\d+)$`, iEat)
303
+
sc.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining)
304
+
}
303
305
304
-
```go
305
-
type cntCtxKey struct{} // Key for a particular context value type.
306
-
307
-
s.Step("^I have a random number of godogs$", func(ctx context.Context) context.Context {
308
-
// Creating a random number of godog and storing it in context for future reference.
309
-
cnt:= rand.Int()
310
-
Godogs = cnt
311
-
return context.WithValue(ctx, cntCtxKey{}, cnt)
312
-
})
313
-
314
-
s.Step("I eat all available godogs", func(ctx context.Context) error {
315
-
// Getting previously stored number of godogs from context.
316
-
cnt:= ctx.Value(cntCtxKey{}).(uint32)
317
-
ifGodogs < cnt {
318
-
return errors.New("can't eat more than I have")
319
-
}
320
-
Godogs -= cnt
321
-
returnnil
322
-
})
323
306
```
324
307
325
-
When you run godog again - `godog`
308
+
In this example we are using `context.Context` to pass the state between the steps.
309
+
Every scenario starts with an empty context and then steps and hooks can add relevant information to it.
310
+
Instrumented context is chained through the steps and hooks and is safe to use when multiple scenarios are running concurrently.
311
+
312
+
When you run godog again with `go test -v godogs_test.go` or with a CLI `godog run`.
326
313
327
314
You should see a passing run:
328
-
```gherkin
315
+
```
316
+
=== RUN TestFeatures
329
317
Feature: eat godogs
330
318
In order to be happy
331
319
As a hungry gopher
332
320
I need to be able to eat godogs
321
+
=== RUN TestFeatures/Eat_5_out_of_12
333
322
334
323
Scenario: Eat 5 out of 12 # features/godogs.feature:6
335
-
Given there are 12 godogs # godogs_test.go:10 -> thereAreGodogs
336
-
When I eat 5 # godogs_test.go:14 -> iEat
337
-
Then there should be 7 remaining # godogs_test.go:22 -> thereShouldBeRemaining
338
-
```
339
-
```
324
+
Given there are 12 godogs # godogs_test.go:14 -> command-line-arguments.thereAreGodogs
325
+
When I eat 5 # godogs_test.go:18 -> command-line-arguments.iEat
326
+
Then there should be 7 remaining # godogs_test.go:33 -> command-line-arguments.thereShouldBeRemaining
327
+
340
328
1 scenarios (1 passed)
341
329
3 steps (3 passed)
342
-
258.302µs
330
+
275.333µs
331
+
--- PASS: TestFeatures (0.00s)
332
+
--- PASS: TestFeatures/Eat_5_out_of_12 (0.00s)
333
+
PASS
334
+
ok command-line-arguments 0.130s
343
335
```
344
336
345
-
We have hooked to `ScenarioContext`**Before** event in order to reset the application state before each scenario.
337
+
You may hook to `ScenarioContext`**Before** event in order to reset or pre-seed the application state before each scenario.
346
338
You may hook into more events, like `sc.StepContext()`**After** to print all state in case of an error.
347
339
Or **BeforeSuite** to prepare a database.
348
340
349
341
By now, you should have figured out, how to use **godog**. Another advice is to make steps orthogonal, small and simple to read for a user. Whether the user is a dumb website user or an API developer, who may understand a little more technical context - it should target that user.
350
342
351
343
When steps are orthogonal and small, you can combine them just like you do with Unix tools. Look how to simplify or remove ones, which can be composed.
352
344
345
+
`TestFeatures` acts as a regular Go test, so you can leverage your IDE facilities to run and debug it.
346
+
353
347
## Code of Conduct
354
348
355
349
Everyone interacting in this codebase and issue tracker is expected to follow the Cucumber [code of conduct](https://github.com/cucumber/cucumber/blob/master/CODE_OF_CONDUCT.md).
Another way to use `godog` is to run it in CLI mode.
581
+
582
+
In this mode `godog` CLI will use `go` under the hood to compile and run your test suite.
583
+
584
+
**Godog** does not intervene with the standard **go test** command behavior. You can leverage both frameworks to functionally test your application while maintaining all test related source code in **_test.go** files.
585
+
586
+
**Godog** acts similar compared to **go test** command, by using go compiler and linker tool in order to produce test executable. Godog contexts need to be exported the same way as **Test** functions for go tests. Note, that if you use **godog** command tool, it will use `go` executable to determine compiler and linker.
587
+
588
+
### Install
589
+
```
590
+
go install github.com/cucumber/godog/cmd/godog@latest
591
+
```
592
+
Adding `@v0.12.0` will install v0.12.0 specifically instead of master.
593
+
594
+
With `go` version prior to 1.17, use `go get github.com/cucumber/godog/cmd/godog@v0.12.0`.
595
+
Running `within the $GOPATH`, you would also need to set `GO111MODULE=on`, like this:
596
+
```
597
+
GO111MODULE=on go get github.com/cucumber/godog/cmd/godog@v0.12.0
598
+
```
599
+
584
600
### Configure common options for godog CLI
585
601
586
602
There are no global options or configuration files. Alias your common or project based commands: `alias godog-wip="godog --format=progress --tags=@wip"`
587
603
588
-
###Concurrency
604
+
## Concurrency
589
605
590
-
When concurrency is configured in options, godog will execute the scenarios concurrently, which is support by all supplied formatters.
606
+
When concurrency is configured in options, godog will execute the scenarios concurrently, which is supported by all supplied formatters.
591
607
592
608
In order to support concurrency well, you should reset the state and isolate each scenario. They should not share any state. It is suggested to run the suite concurrently in order to make sure there is no state corruption or race conditions in the application.
593
609
594
-
It is also useful to randomize the order of scenario execution, which you can now do with **--random** command option.
610
+
It is also useful to randomize the order of scenario execution, which you can now do with `--random` command option or `godog.Options.Randomize` setting.
595
611
596
612
### Building your own custom formatter
597
613
A simple example can be [found here](/_examples/custom-formatter).
0 commit comments