Using godog flags with TestMain and Subtests #495
-
I have a basic example that combines TestMain and Subtests so I can run individual scenario while maintaining the ability to do extra setup or teardown before or after the test execute: var opts = godog.Options{
Output: colors.Colored(os.Stdout),
Format: "pretty",
}
func init() {
godog.BindCommandLineFlags("godog.", &opts)
}
func TestMain(m *testing.M) {
flag.Parse()
pflag.Parse()
opts.Paths = flag.Args()
os.Exit(m.Run())
}
func TestFeatures(t *testing.T) {
tsi := func(ctx *godog.TestSuiteContext) {
}
si := func(ctx *godog.ScenarioContext) {
ctx.Step(`^I eat (\d+)$`, func() error { return godog.ErrPending })
ctx.Step(`^there are (\d+) godogs$`, func() error { return godog.ErrPending })
ctx.Step(`^there should be (\d+) remaining$`, func() error { return godog.ErrPending })
}
o := opts
o.TestingT = t
status := godog.TestSuite{
Name: "godogs",
Options: &o,
TestSuiteInitializer: tsi,
ScenarioInitializer: si,
}.Run()
assert.Equal(t, 0, status)
} However I'm unable to make the --godog.* flags working, as example:
Without the godog flags, everything work as expected:
Has anyone managed to make it working ? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 10 replies
-
Based on the example https://github.com/cucumber/godog/blob/v0.12.5/_examples/godogs/godogs_test.go the test could be as follows.
package main // Package does not have to be main, these tests can live in other package as well.
import (
"github.com/cucumber/godog"
"github.com/cucumber/godog/colors"
)
// This example shows how to set up test suite runner with Go subtests and godog command line parameters.
// Sample commands:
// * run all scenarios from default directory (features): go test -test.run "^TestFeatures/"
// * run all scenarios and list subtest names: go test -test.v -test.run "^TestFeatures/"
// * run all scenarios from one feature file: go test -test.v -godog.paths features/nodogs.feature -test.run "^TestFeatures/"
// * run all scenarios from multiple feature files: go test -test.v -godog.paths features/nodogs.feature,features/godogs.feature -test.run "^TestFeatures/"
// * run single scenario as a subtest: go test -test.v -test.run "^TestFeatures/Eat_5_out_of_12$"
// * show usage help: go test -godog.help
// * show usage help if there were other test files in directory: go test -godog.help godogs_test.go
// * run scenarios with multiple formatters: go test -test.v -godog.format cucumber:cuc.json,pretty -test.run "^TestFeatures/"
import (
"context"
"flag"
"fmt"
"os"
"strings"
"testing"
)
var (
opts = godog.Options{Output: colors.Colored(os.Stdout)}
showHelp bool
paths string
)
func init() {
godog.BindFlags("godog.", flag.CommandLine, &opts)
flag.StringVar(&paths, "godog.paths", "", "Comma-separated list of paths.")
flag.BoolVar(&showHelp, "godog.help", false, "Show usage.")
}
func TestFeatures(t *testing.T) {
if showHelp {
flag.Usage()
t.SkipNow()
}
o := opts
o.TestingT = t
if paths != "" {
o.Paths = strings.Split(paths, ",")
}
status := godog.TestSuite{
Name: "godogs",
Options: &o,
TestSuiteInitializer: InitializeTestSuite,
ScenarioInitializer: InitializeScenario,
}.Run()
if status == 2 {
t.SkipNow()
}
if status != 0 {
t.Fatalf("zero status code expected, %d received", status)
}
}
func thereAreGodogs(available int) error {
Godogs = available
return nil
}
func iEat(num int) error {
if Godogs < num {
return fmt.Errorf("you cannot eat %d godogs, there are %d available", num, Godogs)
}
Godogs -= num
return nil
}
func thereShouldBeRemaining(remaining int) error {
if Godogs != remaining {
return fmt.Errorf("expected %d godogs to be remaining, but there is %d", remaining, Godogs)
}
return nil
}
func thereShouldBeNoneRemaining() error {
return thereShouldBeRemaining(0)
}
func InitializeTestSuite(ctx *godog.TestSuiteContext) {
ctx.BeforeSuite(func() { Godogs = 0 })
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
Godogs = 0 // clean the state before every scenario
return ctx, nil
})
ctx.Step(`^there are (\d+) godogs$`, thereAreGodogs)
ctx.Step(`^I eat (\d+)$`, iEat)
ctx.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining)
ctx.Step(`^there should be none remaining$`, thereShouldBeNoneRemaining)
} The issue is that You can use I'm also planning to update |
Beta Was this translation helpful? Give feedback.
-
My reproducer is here: https://github.com/lburgazzoli/cucumber-playground |
Beta Was this translation helpful? Give feedback.
Based on the example https://github.com/cucumber/godog/blob/v0.12.5/_examples/godogs/godogs_test.go the test could be as follows.
godogs_test.go