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 """
As a developer, your work is done as soon as you’ve made the ls command behave as described in the Scenario.
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 Build() (string, error)
- func FlagSet(format, tags *string, defs, sof, noclr *bool, cr *int) *flag.FlagSet
- func Format(name, description string, f Formatter)
- func Run(contextInitializer func(suite *Suite)) int
- type Formatter
- type StepDef
- type Suite
- func (s *Suite) AfterScenario(f func(interface{}, error))
- func (s *Suite) AfterStep(f func(*gherkin.Step, error))
- func (s *Suite) AfterSuite(f func())
- func (s *Suite) BeforeScenario(f func(interface{}))
- func (s *Suite) BeforeStep(f func(*gherkin.Step))
- func (s *Suite) BeforeSuite(f func())
- func (s *Suite) Step(expr interface{}, stepFunc interface{})
Constants ¶
const Version = "v0.5.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 Build ¶
Build creates a test package like go test command. 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 Format ¶
Format registers a feature suite output Formatter as the name and descriptiongiven. Formatter is used to represent suite output
func Run ¶
Run creates and runs the feature suite. 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.
Types ¶
type Formatter ¶
type Formatter interface { Feature(*gherkin.Feature, string) Node(interface{}) Failed(*gherkin.Step, *StepDef, error) Passed(*gherkin.Step, *StepDef) Skipped(*gherkin.Step) Undefined(*gherkin.Step) Pending(*gherkin.Step, *StepDef) 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 StepDef ¶
type StepDef struct { Expr *regexp.Regexp Handler interface{} // contains filtered or unexported fields }
StepDef 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 Suite ¶
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.
func (*Suite) AfterScenario ¶
AfterScenario registers an function or method to be run after every scenario or scenario outline
The interface argument may be *gherkin.Scenario or *gherkin.ScenarioOutline
func (*Suite) AfterStep ¶
AfterStep registers an function or method to be run after every scenario
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 (*Suite) AfterSuite ¶
func (s *Suite) AfterSuite(f func())
AfterSuite registers a function or method to be run once after suite runner
func (*Suite) BeforeScenario ¶
func (s *Suite) BeforeScenario(f func(interface{}))
BeforeScenario registers a function or method to be run before every scenario or scenario outline.
The interface argument may be *gherkin.Scenario or *gherkin.ScenarioOutline
It is a good practice to restore the default state before every scenario so it would be isolated from any kind of state.
func (*Suite) BeforeStep ¶
BeforeStep registers a function or method to be run before every scenario
func (*Suite) BeforeSuite ¶
func (s *Suite) BeforeSuite(f 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...
func (*Suite) Step ¶
func (s *Suite) Step(expr interface{}, stepFunc interface{})
Step allows to register a *StepDef 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 *StepDef is matched, then ErrUndefined error will be returned when running steps.