From 9441eb3b3110fc838c63a6c0ee81a23c88dbc041 Mon Sep 17 00:00:00 2001 From: gedi Date: Tue, 13 Mar 2018 10:27:49 +0200 Subject: [PATCH] add support for binding godog flags to specified *flag.FlagSet allows to run with go test and use all godog flags for filtering related to #120 --- README.md | 39 +++++++++++++++++++++++++++++-- examples/godogs/godogs_test.go | 18 ++++++++++----- flags.go | 42 +++++++++++++++++++++------------- godog.go | 2 +- 4 files changed, 76 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 1fefb7c..8432f2f 100644 --- a/README.md +++ b/README.md @@ -237,6 +237,42 @@ 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: +The following example binds **godog** flags with specified prefix `godog` +in order to prevent flag collisions. + +``` go +var opt = godog.Options{Output: colors.Colored(os.Stdout)} + +func init() { + godog.BindFlags("godog.", flag.CommandLine, &opt) +} + +func TestMain(m *testing.M) { + flag.Parse() + opt.Paths = flag.Args() + + status := godog.RunWithOptions("godogs", func(s *godog.Suite) { + FeatureContext(s) + }, opt) + + if st := m.Run(); st > status { + status = st + } + os.Exit(status) +} +``` + +Then you may run tests with by specifying flags in order to filter +features. + +``` +go test -v --godog.format=progress --godog.random --godog.tags=wip +go test -v --godog.format=pretty --godog.random -race -coverprofile=coverage.txt -covermode=atomic +``` + +The following example does not bind godog flags, instead manually +configuring needed options. + ``` go func TestMain(m *testing.M) { status := godog.RunWithOptions("godog", func(s *godog.Suite) { @@ -251,8 +287,7 @@ func TestMain(m *testing.M) { status = st } os.Exit(status) -} -``` +} ``` You can even go one step further and reuse **go test** flags, like **verbose** mode in order to switch godog **format**. See the following diff --git a/examples/godogs/godogs_test.go b/examples/godogs/godogs_test.go index 538c43b..dc7eebe 100644 --- a/examples/godogs/godogs_test.go +++ b/examples/godogs/godogs_test.go @@ -2,22 +2,28 @@ package main import ( + "flag" "fmt" "os" "testing" - "time" "github.com/DATA-DOG/godog" + "github.com/DATA-DOG/godog/colors" ) +var opt = godog.Options{Output: colors.Colored(os.Stdout)} + +func init() { + godog.BindFlags("godog.", flag.CommandLine, &opt) +} + func TestMain(m *testing.M) { + flag.Parse() + opt.Paths = flag.Args() + status := godog.RunWithOptions("godogs", func(s *godog.Suite) { FeatureContext(s) - }, godog.Options{ - Format: "progress", - Paths: []string{"features"}, - Randomize: time.Now().UTC().UnixNano(), // randomize scenario execution order - }) + }, opt) if st := m.Run(); st > status { status = st diff --git a/flags.go b/flags.go index 71bea29..05734f1 100644 --- a/flags.go +++ b/flags.go @@ -34,7 +34,17 @@ var descRandomOption = "Randomly shuffle the scenario execution order.\n" + s(4) + `e.g. ` + colors.Yellow(`--random`) + " or " + colors.Yellow(`--random=5738`) // FlagSet allows to manage flags by external suite runner +// builds flag.FlagSet with godog flags binded func FlagSet(opt *Options) *flag.FlagSet { + set := flag.NewFlagSet("godog", flag.ExitOnError) + BindFlags("", set, opt) + set.Usage = usage(set, opt.Output) + return set +} + +// BindFlags binds godog flags to given flag set prefixed +// by given prefix, without overriding usage +func BindFlags(prefix string, set *flag.FlagSet, opt *Options) { descFormatOption := "How to format tests output. Built-in formats:\n" // @TODO: sort by name for name, desc := range AvailableFormatters() { @@ -42,21 +52,18 @@ func FlagSet(opt *Options) *flag.FlagSet { } descFormatOption = strings.TrimSpace(descFormatOption) - set := flag.NewFlagSet("godog", flag.ExitOnError) - set.StringVar(&opt.Format, "format", "pretty", descFormatOption) - set.StringVar(&opt.Format, "f", "pretty", descFormatOption) - set.StringVar(&opt.Tags, "tags", "", descTagsOption) - set.StringVar(&opt.Tags, "t", "", descTagsOption) - set.IntVar(&opt.Concurrency, "concurrency", 1, descConcurrencyOption) - set.IntVar(&opt.Concurrency, "c", 1, descConcurrencyOption) - set.BoolVar(&opt.ShowStepDefinitions, "definitions", false, "Print all available step definitions.") - set.BoolVar(&opt.ShowStepDefinitions, "d", false, "Print all available step definitions.") - set.BoolVar(&opt.StopOnFailure, "stop-on-failure", false, "Stop processing on first failed scenario.") - set.BoolVar(&opt.Strict, "strict", false, "Fail suite when there are pending or undefined steps.") - set.BoolVar(&opt.NoColors, "no-colors", false, "Disable ansi colors.") - set.Var(&randomSeed{&opt.Randomize}, "random", descRandomOption) - set.Usage = usage(set, opt.Output) - return set + set.StringVar(&opt.Format, prefix+"format", "pretty", descFormatOption) + set.StringVar(&opt.Format, prefix+"f", "pretty", descFormatOption) + set.StringVar(&opt.Tags, prefix+"tags", "", descTagsOption) + set.StringVar(&opt.Tags, prefix+"t", "", descTagsOption) + set.IntVar(&opt.Concurrency, prefix+"concurrency", 1, descConcurrencyOption) + set.IntVar(&opt.Concurrency, prefix+"c", 1, descConcurrencyOption) + set.BoolVar(&opt.ShowStepDefinitions, prefix+"definitions", false, "Print all available step definitions.") + set.BoolVar(&opt.ShowStepDefinitions, prefix+"d", false, "Print all available step definitions.") + set.BoolVar(&opt.StopOnFailure, prefix+"stop-on-failure", false, "Stop processing on first failed scenario.") + set.BoolVar(&opt.Strict, prefix+"strict", false, "Fail suite when there are pending or undefined steps.") + set.BoolVar(&opt.NoColors, prefix+"no-colors", false, "Disable ansi colors.") + set.Var(&randomSeed{&opt.Randomize}, prefix+"random", descRandomOption) } type flagged struct { @@ -180,7 +187,10 @@ func (rs *randomSeed) Set(s string) error { return err } -func (rs randomSeed) String() string { +func (rs *randomSeed) String() string { + if rs.ref == nil { + return "0" + } return strconv.FormatInt(*rs.ref, 10) } diff --git a/godog.go b/godog.go index 163982c..525611e 100644 --- a/godog.go +++ b/godog.go @@ -39,4 +39,4 @@ Godog was inspired by Behat and Cucumber the above description is taken from it' package godog // Version of package - based on Semantic Versioning 2.0.0 http://semver.org/ -const Version = "v0.7.6" +const Version = "v0.7.7"