Documentation ¶
Overview ¶
Package godog is the official Cucumber BDD framework for Golang, it merges specification and test documentation into one cohesive whole.
Godog does not intervene with the standard "go test" command and it's behavior. You can leverage both frameworks to functionally test your application while maintaining all test related source code in *_test.go files.
Godog acts similar compared to go test command. It uses go compiler and linker tool in order to produce test executable. Godog contexts needs to be exported same as Test functions for go test.
For example, imagine you’re about to create the famous UNIX ls command. Before you begin, you describe how the feature should work, see the example below..
Example:
Feature: ls In order to see the directory structure As a UNIX user I need to be able to list the current directory's contents Scenario: Given I am in a directory "test" And I have a file named "foo" And I have a file named "bar" When I run ls Then I should get output: """ bar foo """
Now, wouldn’t it be cool if something could read this sentence and use it to actually run a test against the ls command? Hey, that’s exactly what this package does! As you’ll see, Godog is easy to learn, quick to use, and will put the fun back into tests.
Godog was inspired by Behat and Cucumber the above description is taken from it's documentation.
Index ¶
- Constants
- Variables
- func AvailableFormatters() map[string]string
- func BindFlags(prefix string, set *flag.FlagSet, opt *Options)
- func Build(bin string) error
- func FlagSet(opt *Options) *flag.FlagSet
- func Format(name, description string, f FormatterFunc)
- func Run(suite string, initializer func(*Suite)) intdeprecated
- func RunWithOptions(suite string, initializer func(*Suite), opt Options) intdeprecated
- func SuiteContext(s *Suite, additionalContextInitializers ...func(suite *Suite))deprecated
- type ConcurrentFormatterdeprecated
- type DocString
- type Formatter
- type FormatterFunc
- type Options
- type Scenario
- type ScenarioContext
- func (ctx *ScenarioContext) AfterScenario(fn func(sc *Scenario, err error))
- func (ctx *ScenarioContext) AfterStep(fn func(st *Step, err error))
- func (ctx *ScenarioContext) BeforeScenario(fn func(sc *Scenario))
- func (ctx *ScenarioContext) BeforeStep(fn func(st *Step))
- func (ctx *ScenarioContext) Step(expr, stepFunc interface{})
- type Step
- type StepDefinition
- type Steps
- type Suitedeprecated
- func (s *Suite) AfterScenario(fn func(*messages.Pickle, error))deprecated
- func (s *Suite) AfterStep(fn func(*messages.Pickle_PickleStep, error))deprecated
- func (s *Suite) AfterSuite(fn func())deprecated
- func (s *Suite) BeforeScenario(fn func(*messages.Pickle))deprecated
- func (s *Suite) BeforeStep(fn func(*messages.Pickle_PickleStep))deprecated
- func (s *Suite) BeforeSuite(fn func())deprecated
- func (s *Suite) Step(expr interface{}, stepFunc interface{})deprecated
- type Table
- type TestSuite
- type TestSuiteContext
Constants ¶
const Version = "v0.10.0"
Version of package - based on Semantic Versioning 2.0.0 http://semver.org/
Variables ¶
var ErrPending = fmt.Errorf("step implementation is pending")
ErrPending should be returned by step definition if step implementation is pending
var ErrUndefined = fmt.Errorf("step is undefined")
ErrUndefined is returned in case if step definition was not found
Functions ¶
func AvailableFormatters ¶
AvailableFormatters gives a map of all formatters registered with their name as key and description as value
func BindFlags ¶ added in v0.7.9
BindFlags binds godog flags to given flag set prefixed by given prefix, without overriding usage
func Build ¶
Build creates a test package like go test command at given target path. If there are no go files in tested directory, then it simply builds a godog executable to scan features.
If there are go test files, it first builds a test package with standard go test command.
Finally it generates godog suite executable which registers exported godog contexts from the test files of tested package.
Returns the path to generated executable
func FlagSet ¶
FlagSet allows to manage flags by external suite runner builds flag.FlagSet with godog flags binded
func Format ¶
func Format(name, description string, f FormatterFunc)
Format registers a feature suite output formatter by given name, description and FormatterFunc constructor function, to initialize formatter with the output recorder.
func Run
deprecated
Run creates and runs the feature suite. Reads all configuration options from flags. uses contextInitializer to register contexts
the concurrency option allows runner to initialize a number of suites to be run separately. Only progress formatter is supported when concurrency level is higher than 1
contextInitializer must be able to register the step definitions and event handlers.
The exit codes may vary from:
0 - success 1 - failed 2 - command line usage error 128 - or higher, os signal related error exit codes
If there are flag related errors they will be directed to os.Stderr
Deprecated: The current Suite initializer will be removed and replaced by two initializers, one for the Test Suite and one for the Scenarios. Use:
godog.TestSuite{ Name: name, TestSuiteInitializer: testSuiteInitializer, ScenarioInitializer: scenarioInitializer, }.Run()
instead.
func RunWithOptions
deprecated
RunWithOptions is same as Run function, except it uses Options provided in order to run the test suite without parsing flags
This method is useful in case if you run godog in for example TestMain function together with go tests
The exit codes may vary from:
0 - success 1 - failed 2 - command line usage error 128 - or higher, os signal related error exit codes
If there are flag related errors they will be directed to os.Stderr
Deprecated: The current Suite initializer will be removed and replaced by two initializers, one for the Test Suite and one for the Scenarios. Use:
godog.TestSuite{ Name: name, TestSuiteInitializer: testSuiteInitializer, ScenarioInitializer: scenarioInitializer, Options: &opts, }.Run()
instead.
func SuiteContext
deprecated
added in
v0.7.3
SuiteContext provides steps for godog suite execution and can be used for meta-testing of godog features/steps themselves.
Beware, steps or their definitions might change without backward compatibility guarantees. A typical user of the godog library should never need this, rather it is provided for those developing add-on libraries for godog.
For an example of how to use, see godog's own `features/` and `suite_test.go`.
Deprecated: Use InitializeScenario instead.
Types ¶
type ConcurrentFormatter
deprecated
added in
v0.8.0
type ConcurrentFormatter interface { Formatter Copy(ConcurrentFormatter) Sync(ConcurrentFormatter) }
ConcurrentFormatter is an interface for a Concurrent version of the Formatter interface.
Deprecated: The formatters need to handle concurrency instead of being copied and synchronized for each thread.
type DocString ¶ added in v0.10.0
type DocString = messages.PickleStepArgument_PickleDocString
DocString represents the DocString argument made to a step definition
type Formatter ¶
type Formatter interface { TestRunStarted() Feature(*messages.GherkinDocument, string, []byte) Pickle(*messages.Pickle) Defined(*messages.Pickle, *messages.Pickle_PickleStep, *StepDefinition) Failed(*messages.Pickle, *messages.Pickle_PickleStep, *StepDefinition, error) Passed(*messages.Pickle, *messages.Pickle_PickleStep, *StepDefinition) Skipped(*messages.Pickle, *messages.Pickle_PickleStep, *StepDefinition) Undefined(*messages.Pickle, *messages.Pickle_PickleStep, *StepDefinition) Pending(*messages.Pickle, *messages.Pickle_PickleStep, *StepDefinition) Summary() }
Formatter is an interface for feature runner output summary presentation.
New formatters may be created to represent suite results in different ways. These new formatters needs to be registered with a godog.Format function call
type FormatterFunc ¶
FormatterFunc builds a formatter with given suite name and io.Writer to record output
func FindFmt ¶ added in v0.7.9
func FindFmt(name string) FormatterFunc
FindFmt searches available formatters registered and returns FormaterFunc matched by given format name or nil otherwise
type Options ¶
type Options struct { // Print step definitions found and exit ShowStepDefinitions bool // Randomize, if not `0`, will be used to run scenarios in a random order. // // Randomizing scenario order is especially helpful for detecting // situations where you have state leaking between scenarios, which can // cause flickering or fragile tests. // // The default value of `0` means "do not randomize". // // The magic value of `-1` means "pick a random seed for me", and godog will // assign a seed on it's own during the `RunWithOptions` phase, similar to if // you specified `--random` on the command line. // // Any other value will be used as the random seed for shuffling. Re-using the // same seed will allow you to reproduce the shuffle order of a previous run // to isolate an error condition. Randomize int64 // Stops on the first failure StopOnFailure bool // Fail suite when there are pending or undefined steps Strict bool // Forces ansi color stripping NoColors bool // Various filters for scenarios parsed // from feature files Tags string // The formatter name Format string // Concurrency rate, not all formatters accepts this Concurrency int // All feature file paths Paths []string // Where it should print formatter output Output io.Writer }
Options are suite run options flags are mapped to these options.
It can also be used together with godog.RunWithOptions to run test suite from go source directly
See the flags for more details
type Scenario ¶ added in v0.10.0
type Scenario = messages.Pickle
Scenario represents the executed scenario
type ScenarioContext ¶ added in v0.10.0
type ScenarioContext struct {
// contains filtered or unexported fields
}
ScenarioContext allows various contexts to register steps and event handlers.
When running a scenario, the instance of ScenarioContext is passed to all functions (contexts), which have it as a first and only argument.
Note that all event hooks does not catch panic errors in order to have a trace information. Only step executions are catching panic error since it may be a context specific error.
func (*ScenarioContext) AfterScenario ¶ added in v0.10.0
func (ctx *ScenarioContext) AfterScenario(fn func(sc *Scenario, err error))
AfterScenario registers an function or method to be run after every scenario.
func (*ScenarioContext) AfterStep ¶ added in v0.10.0
func (ctx *ScenarioContext) AfterStep(fn func(st *Step, err error))
AfterStep registers an function or method to be run after every step.
It may be convenient to return a different kind of error in order to print more state details which may help in case of step failure
In some cases, for example when running a headless browser, to take a screenshot after failure.
func (*ScenarioContext) BeforeScenario ¶ added in v0.10.0
func (ctx *ScenarioContext) BeforeScenario(fn func(sc *Scenario))
BeforeScenario registers a function or method to be run before every scenario.
It is a good practice to restore the default state before every scenario so it would be isolated from any kind of state.
func (*ScenarioContext) BeforeStep ¶ added in v0.10.0
func (ctx *ScenarioContext) BeforeStep(fn func(st *Step))
BeforeStep registers a function or method to be run before every step.
func (*ScenarioContext) Step ¶ added in v0.10.0
func (ctx *ScenarioContext) Step(expr, stepFunc interface{})
Step allows to register a *StepDefinition in the Godog feature suite, the definition will be applied to all steps matching the given Regexp expr.
It will panic if expr is not a valid regular expression or stepFunc is not a valid step handler.
The expression can be of type: *regexp.Regexp, string or []byte
The stepFunc may accept one or several arguments of type: - int, int8, int16, int32, int64 - float32, float64 - string - []byte - *godog.DocString - *godog.Table
The stepFunc need to return either an error or []string for multistep
Note that if there are two definitions which may match the same step, then only the first matched handler will be applied.
If none of the *StepDefinition is matched, then ErrUndefined error will be returned when running steps.
type Step ¶ added in v0.10.0
type Step = messages.Pickle_PickleStep
Step represents the executed step
type StepDefinition ¶ added in v0.9.0
type StepDefinition struct { Expr *regexp.Regexp Handler interface{} // contains filtered or unexported fields }
StepDefinition is a registered step definition contains a StepHandler and regexp which is used to match a step. Args which were matched by last executed step
This structure is passed to the formatter when step is matched and is either failed or successful
type Steps ¶ added in v0.7.1
type Steps []string
Steps allows to nest steps instead of returning an error in step func it is possible to return combined steps:
func multistep(name string) godog.Steps { return godog.Steps{ fmt.Sprintf(`an user named "%s"`, name), fmt.Sprintf(`user "%s" is authenticated`, name), } }
These steps will be matched and executed in sequential order. The first one which fails will result in main step failure.
type Suite
deprecated
type Suite struct {
// contains filtered or unexported fields
}
Suite allows various contexts to register steps and event handlers.
When running a test suite, the instance of Suite is passed to all functions (contexts), which have it as a first and only argument.
Note that all event hooks does not catch panic errors in order to have a trace information. Only step executions are catching panic error since it may be a context specific error.
Deprecated: The current Suite initializer will be removed and replaced by two initializers, one for the Test Suite and one for the Scenarios. This struct will therefore not be exported in the future.
func (*Suite) AfterScenario
deprecated
AfterScenario registers an function or method to be run after every pickle.
Deprecated: The current Suite initializer will be removed and replaced by two initializers, one for the Test Suite and one for the Scenarios. Use func (ctx *ScenarioContext) AfterScenario instead.
func (*Suite) AfterStep
deprecated
AfterStep registers an function or method to be run after every step.
It may be convenient to return a different kind of error in order to print more state details which may help in case of step failure
In some cases, for example when running a headless browser, to take a screenshot after failure.
Deprecated: The current Suite initializer will be removed and replaced by two initializers, one for the Test Suite and one for the Scenarios. Use func (ctx *ScenarioContext) AfterStep instead.
func (*Suite) AfterSuite
deprecated
func (s *Suite) AfterSuite(fn func())
AfterSuite registers a function or method to be run once after suite runner
Deprecated: The current Suite initializer will be removed and replaced by two initializers, one for the Test Suite and one for the Scenarios. Use func (ctx *TestSuiteContext) AfterSuite instead.
func (*Suite) BeforeScenario
deprecated
func (s *Suite) BeforeScenario(fn func(*messages.Pickle))
BeforeScenario registers a function or method to be run before every pickle.
It is a good practice to restore the default state before every scenario so it would be isolated from any kind of state.
Deprecated: The current Suite initializer will be removed and replaced by two initializers, one for the Test Suite and one for the Scenarios. Use func (ctx *ScenarioContext) BeforeScenario instead.
func (*Suite) BeforeStep
deprecated
func (s *Suite) BeforeStep(fn func(*messages.Pickle_PickleStep))
BeforeStep registers a function or method to be run before every step.
Deprecated: The current Suite initializer will be removed and replaced by two initializers, one for the Test Suite and one for the Scenarios. Use func (ctx *ScenarioContext) BeforeStep instead.
func (*Suite) BeforeSuite
deprecated
func (s *Suite) BeforeSuite(fn func())
BeforeSuite registers a function or method to be run once before suite runner.
Use it to prepare the test suite for a spin. Connect and prepare database for instance...
Deprecated: The current Suite initializer will be removed and replaced by two initializers, one for the Test Suite and one for the Scenarios. Use func (ctx *TestSuiteContext) BeforeSuite instead.
func (*Suite) Step
deprecated
func (s *Suite) Step(expr interface{}, stepFunc interface{})
Step allows to register a *StepDefinition in Godog feature suite, the definition will be applied to all steps matching the given Regexp expr.
It will panic if expr is not a valid regular expression or stepFunc is not a valid step handler.
Note that if there are two definitions which may match the same step, then only the first matched handler will be applied.
If none of the *StepDefinition is matched, then ErrUndefined error will be returned when running steps.
Deprecated: The current Suite initializer will be removed and replaced by two initializers, one for the Test Suite and one for the Scenarios. Use func (ctx *ScenarioContext) Step instead.
type Table ¶ added in v0.10.0
type Table = messages.PickleStepArgument_PickleTable
Table represents the Table argument made to a step definition
type TestSuite ¶ added in v0.10.0
type TestSuite struct { Name string TestSuiteInitializer func(*TestSuiteContext) ScenarioInitializer func(*ScenarioContext) Options *Options }
TestSuite allows for configuration of the Test Suite Execution
func (TestSuite) Run ¶ added in v0.10.0
Run will execute the test suite.
If options are not set, it will reads all configuration options from flags.
The exit codes may vary from:
0 - success 1 - failed 2 - command line usage error 128 - or higher, os signal related error exit codes
If there are flag related errors they will be directed to os.Stderr
type TestSuiteContext ¶ added in v0.10.0
type TestSuiteContext struct {
// contains filtered or unexported fields
}
TestSuiteContext allows various contexts to register event handlers.
When running a test suite, the instance of TestSuiteContext is passed to all functions (contexts), which have it as a first and only argument.
Note that all event hooks does not catch panic errors in order to have a trace information
func (*TestSuiteContext) AfterSuite ¶ added in v0.10.0
func (ctx *TestSuiteContext) AfterSuite(fn func())
AfterSuite registers a function or method to be run once after suite runner
func (*TestSuiteContext) BeforeSuite ¶ added in v0.10.0
func (ctx *TestSuiteContext) BeforeSuite(fn func())
BeforeSuite registers a function or method to be run once before suite runner.
Use it to prepare the test suite for a spin. Connect and prepare database for instance...
Source Files ¶
- ast.go
- builder.go
- feature.go
- flags.go
- fmt.go
- fmt_base.go
- fmt_cucumber.go
- fmt_events.go
- fmt_junit.go
- fmt_pretty.go
- fmt_progress.go
- godog.go
- options.go
- parser.go
- results.go
- run.go
- stacktrace.go
- stepdef.go
- storage.go
- suite.go
- suite_context.go
- tag_filter.go
- test_context.go
- undefined_snippets_gen.go
- utils.go