godog

package module
v0.8.2-0...-9eca166 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2019 License: BSD-3-Clause Imports: 30 Imported by: 0

README

Build Status GoDoc codecov.io

Godog

Notice about spikerlabs fork: only latest golang version is supported.

Godog logo

The API is likely to change a few times before we reach 1.0.0

Please read all the README, you may find it very useful. And do not forget to peek into the CHANGELOG from time to time.

Package godog is the official Cucumber BDD framework for Golang, it merges specification and test documentation into one cohesive whole. The author is a member of cucumber team.

The project is inspired by behat and cucumber and is based on cucumber gherkin3 parser.

Godog does not intervene with the standard go test command 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, by using go compiler and linker tool in order to produce test executable. Godog contexts need to be exported the same way as Test functions for go tests. Note, that if you use godog command tool, it will use go executable to determine compiler and linker.

Godog ships gherkin parser dependency as a subpackage. This will ensure that it is always compatible with the installed version of godog. So in general there are no vendor dependencies needed for installation.

The following about section was taken from cucumber homepage.

About

A single source of truth

Cucumber merges specification and test documentation into one cohesive whole.

Living documentation

Because they're automatically tested by Cucumber, your specifications are always bang up-to-date.

Focus on the customer

Business and IT don't always understand each other. Cucumber's executable specifications encourage closer collaboration, helping teams keep the business goal in mind at all times.

Less rework

When automated testing is this much fun, teams can easily protect themselves from costly regressions.

Install

go get github.com/spikerlabs/godog/cmd/godog

Example

The following example can be found here.

Step 1

Given we create a new go package $GOPATH/src/godogs. From now on, this is our work directory cd $GOPATH/src/godogs.

Imagine we have a godog cart to serve godogs for lunch. First of all, we describe our feature in plain text - vim $GOPATH/src/godogs/features/godogs.feature:

# file: $GOPATH/src/godogs/features/godogs.feature
Feature: eat godogs
  In order to be happy
  As a hungry gopher
  I need to be able to eat godogs

  Scenario: Eat 5 out of 12
    Given there are 12 godogs
    When I eat 5
    Then there should be 7 remaining

NOTE: same as go test godog respects package level isolation. All your step definitions should be in your tested package root directory. In this case - $GOPATH/src/godogs

Step 2

If godog is installed in your GOPATH. We can run godog inside the $GOPATH/src/godogs directory. You should see that the steps are undefined:

Undefined step snippets

If we wish to vendor godog dependency, we can do it as usual, using tools you prefer:

git clone https://github.com/spikerlabs/godog.git $GOPATH/src/godogs/vendor/github.com/spikerlabs/godog

It gives you undefined step snippets to implement in your test context. You may copy these snippets into your godogs_test.go file.

Our directory structure should now look like:

Directory layout

If you copy the snippets into our test file and run godog again. We should see the step definition is now pending:

Pending step definition

You may change ErrPending to nil and the scenario will pass successfully.

Since we need a working implementation, we may start by implementing only what is necessary.

Step 3

We only need a number of godogs for now. Lets keep it simple.

/* file: $GOPATH/src/godogs/godogs.go */
package main

// Godogs available to eat
var Godogs int

func main() { /* usual main func */ }
Step 4

Now lets implement our step definitions, which we can copy from generated console output snippets in order to test our feature requirements:

/* file: $GOPATH/src/godogs/godogs_test.go */
package main

import (
	"fmt"

	"github.com/spikerlabs/godog"
)

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 FeatureContext(s *godog.Suite) {
	s.Step(`^there are (\d+) godogs$`, thereAreGodogs)
	s.Step(`^I eat (\d+)$`, iEat)
	s.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining)

	s.BeforeScenario(func(interface{}) {
		Godogs = 0 // clean the state before every scenario
	})
}

Now when you run the godog again, you should see:

Passed suite

We have hooked to BeforeScenario event in order to reset application state before each scenario. You may hook into more events, like AfterStep to print all state in case of an error. Or BeforeSuite to prepare a database.

By now, you should have figured out, how to use godog. Another advice is to make steps orthogonal, small and simple to read for an user. Whether the user is a dumb website user or an API developer, who may understand a little more technical context - it should target that user.

When steps are orthogonal and small, you can combine them just like you do with Unix tools. Look how to simplify or remove ones, which can be composed.

References and Tutorials
Documentation

See godoc for general API details. See .travis.yml for supported go versions. See godog -h for general command options.

See implementation examples:

FAQ

Running Godog with go test

You may integrate running godog in your go test command. You can run it using go TestMain func available since go 1.4. In this case it is not necessary to have godog command installed. See the following examples.

The following example binds godog flags with specified prefix godog in order to prevent flag collisions.

var opt = godog.Options{
	Output: colors.Colored(os.Stdout),
	Format: "progress", // can define default values
}

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

func TestMain(m *testing.M) {
	status := godog.RunWithOptions("godog", func(s *godog.Suite) {
		FeatureContext(s)
	}, godog.Options{
		Format:    "progress",
		Paths:     []string{"features"},
		Randomize: time.Now().UTC().UnixNano(), // randomize scenario execution order
	})

	if st := m.Run(); st > status {
		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 example:

func TestMain(m *testing.M) {
	format := "progress"
	for _, arg := range os.Args[1:] {
		if arg == "-test.v=true" { // go test transforms -v option
			format = "pretty"
			break
		}
	}
	status := godog.RunWithOptions("godog", func(s *godog.Suite) {
		godog.SuiteContext(s)
	}, godog.Options{
		Format: format,
		Paths:     []string{"features"},
	})

	if st := m.Run(); st > status {
		status = st
	}
	os.Exit(status)
}

Now when running go test -v it will use pretty format.

Configure common options for godog CLI

There are no global options or configuration files. Alias your common or project based commands: alias godog-wip="godog --format=progress --tags=@wip"

Testing browser interactions

godog does not come with builtin packages to connect to the browser. You may want to look at selenium and probably phantomjs. See also the following components:

  1. browsersteps - provides basic context steps to start selenium and navigate browser content.
  2. You may wish to have goquery in order to work with HTML responses like with JQuery.
Concurrency

In order to support concurrency well, you should reset the state and isolate each scenario. They should not share any state. It is suggested to run the suite concurrently in order to make sure there is no state corruption or race conditions in the application.

It is also useful to randomize the order of scenario execution, which you can now do with --random command option.

NOTE: if suite runs with concurrency option, it concurrently runs every feature, not scenario per different features. This gives a flexibility to isolate state per feature. For example using BeforeFeature hook, it is possible to spin up costly service and shut it down only in AfterFeature hook and share the service between all scenarios in that feature. It is not advisable though, because you are risking having a state dependency.

Contributions

Feel free to open a pull request. Note, if you wish to contribute an extension to public (exported methods or types) - please open an issue before to discuss whether these changes can be accepted. All backward incompatible changes are and will be treated cautiously.

License

Godog is licensed under the three clause BSD license

Gherkin is licensed under the MIT and developed as a part of the cucumber project

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

View Source
const Version = "v0.8.0"

Version of package - based on Semantic Versioning 2.0.0 http://semver.org/

Variables

View Source
var ErrPending = fmt.Errorf("step implementation is pending")

ErrPending should be returned by step definition if step implementation is pending

View Source
var ErrUndefined = fmt.Errorf("step is undefined")

ErrUndefined is returned in case if step definition was not found

Functions

func AvailableFormatters

func AvailableFormatters() map[string]string

AvailableFormatters gives a map of all formatters registered with their name as key and description as value

func BindFlags

func BindFlags(prefix string, set *flag.FlagSet, opt *Options)

BindFlags binds godog flags to given flag set prefixed by given prefix, without overriding usage

func Build

func Build(bin string) error

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

func FlagSet(opt *Options) *flag.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

func Run(suite string, contextInitializer func(suite *Suite)) int

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

func RunWithOptions

func RunWithOptions(suite string, contextInitializer func(suite *Suite), opt Options) int

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

func SuiteContext

func SuiteContext(s *Suite, additionalContextInitializers ...func(suite *Suite))

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`.

Types

type Formatter

type Formatter interface {
	Feature(*gherkin.Feature, string, []byte)
	Node(interface{})
	Defined(*gherkin.Step, *StepDef)
	Failed(*gherkin.Step, *StepDef, error)
	Passed(*gherkin.Step, *StepDef)
	Skipped(*gherkin.Step, *StepDef)
	Undefined(*gherkin.Step, *StepDef)
	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 FormatterFunc

type FormatterFunc func(string, io.Writer) Formatter

FormatterFunc builds a formatter with given suite name and io.Writer to record output

func FindFmt

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

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

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) AfterFeature

func (s *Suite) AfterFeature(fn func(*gherkin.Feature))

AfterFeature registers a function or method to be run once after feature executed all scenarios.

func (*Suite) AfterScenario

func (s *Suite) AfterScenario(fn func(interface{}, error))

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

func (s *Suite) AfterStep(fn func(*gherkin.Step, error))

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(fn func())

AfterSuite registers a function or method to be run once after suite runner

func (*Suite) BeforeFeature

func (s *Suite) BeforeFeature(fn func(*gherkin.Feature))

BeforeFeature registers a function or method to be run once before every feature execution.

If godog is run with concurrency option, it will run every feature per goroutine. So user may choose whether to isolate state within feature context or scenario.

Best practice is not to have any state dependency on every scenario, but in some cases if VM for example needs to be started it may take very long for each scenario to restart it.

Use it wisely and avoid sharing state between scenarios.

func (*Suite) BeforeScenario

func (s *Suite) BeforeScenario(fn 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

func (s *Suite) BeforeStep(fn func(*gherkin.Step))

BeforeStep registers a function or method to be run before every scenario

func (*Suite) BeforeSuite

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

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.

Directories

Path Synopsis
cmd
api
Example - demonstrates REST API server implementation tests.
Example - demonstrates REST API server implementation tests.
db
godogs
file: $GOPATH/src/godogs/godogs.go
file: $GOPATH/src/godogs/godogs.go

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL