diff --git a/README.md b/README.md index bf30997..cc3149b 100644 --- a/README.md +++ b/README.md @@ -193,6 +193,49 @@ Now when you run the `godog` again, you should see: more events, like **AfterStep** to test against an error and print more details about the error or state before failure. Or **BeforeSuite** to prepare a database. +#### Running Godog with go test + +There was a question asked whether it is possible to run **godog** from +**go test** command. And the answer is yes. You can run it using go +[TestMain](https://golang.org/pkg/testing/#hdr-Main) func available since +go 1.4. In this case it is not necessary to have **godog** command +installed. See the following example: + +``` go +/* file: $GOPATH/src/godogs/godogs_test.go */ +package main + +import ( + "flag" + "os" + "testing" + + "github.com/DATA-DOG/godog" +) + +func TestMain(m *testing.M) { + args := os.Args + // args for godog + os.Args = []string{ + args[0], + "-f", "progress", + "features", + } + + status := godog.Run(func(s *godog.Suite) { + FeatureContext(s) + }) + + os.Args = args + flag.Parse() + + if st := m.Run(); st > status { + status = st + } + os.Exit(status) +} +``` + ### References and Tutorials - [how to use godog by semaphoreci](https://semaphoreci.com/community/tutorials/how-to-use-godog-for-behavior-driven-development-in-go) diff --git a/examples/godogs/godogs_test.go b/examples/godogs/godogs_test.go index 975a3dc..20546c3 100644 --- a/examples/godogs/godogs_test.go +++ b/examples/godogs/godogs_test.go @@ -2,11 +2,36 @@ package main import ( + "flag" "fmt" + "os" + "testing" "github.com/DATA-DOG/godog" ) +func TestMain(m *testing.M) { + args := os.Args + // args for godog + os.Args = []string{ + args[0], + "-f", "progress", + "features", + } + + status := godog.Run(func(s *godog.Suite) { + FeatureContext(s) + }) + + os.Args = args + flag.Parse() + + if st := m.Run(); st > status { + status = st + } + os.Exit(status) +} + func thereAreGodogs(available int) error { Godogs = available return nil