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
Этот коммит содержится в:
gedi 2018-03-13 10:27:49 +02:00
родитель 0371765570
коммит 9441eb3b31
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 56604CDCCC201556
4 изменённых файлов: 76 добавлений и 25 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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)
}

Просмотреть файл

@ -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"