gucumber

package module
v0.0.0-...-7d5c79e Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2018 License: MIT Imports: 15 Imported by: 173

README

Gucumber

GoDoc Build Status MIT License

An implementation of Cucumber BDD-style testing for Go.

Installing

$ go get github.com/gucumber/gucumber/cmd/gucumber

Usage

Cucumber tests are made up of plain text ".feature" files and program source "step definitions", which for Gucumber are written in Go.

Features

Put feature files internal/features/ with whatever organization you prefer. For example, you might create internal/features/accounts/login.feature with the following text:

@login
Feature: Login Support

  Scenario: User successfully logs in
    Given I have user/pass "foo" / "bar"
    And they log into the website with user "foo" and password "bar"
    Then the user should be successfully logged in

Step Definitions

Create step definitions to match each step in your feature files. These go in ".go" files in the same internal/features/ directory. We might create internal/features/accounts/step_definitions.go:

package accounts

import (
	. "github.com/gucumber/gucumber"
)

func init() {
	user, pass := "", ""

	Before("@login", func() {
		// runs before every feature or scenario tagged with @login
		generatePassword()
	})

	Given(`^I have user/pass "(.+?)" / "(.+?)"$`, func(u, p string) {
		user, pass = u, p
	})

	// ...

	Then(`^the user should be successfully logged in$`, func() {
		if !userIsLoggedIn() {
			T.Errorf("user should have been logged in")
		}
	})
}
T?

The T value is a testing.T style value that represents the test context for each test. It mostly supports Errorf(fmt, args...), but also supports other convenience methods. See the API documentation for more information.

Running

To run your tests, execute:

$ gucumber

You can also specify the path to features in command line arguments:

$ gucumber path/to/features

You can also filter features and scenarios by tags:

$ gucumber -tags=@login # only run login feature(s)

Or:

$ gucumber -tags=~@slow # ignore all "slow" scenarios

This library was written by Loren Segal in 2015. It is licensed for use under the MIT license.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	GlobalContext = Context{
		Steps:         []StepDefinition{},
		World:         map[string]interface{}{},
		BeforeFilters: map[string]func(){},
		AfterFilters:  map[string]func(){},
		Filters:       []string{},
	}

	T Tester

	World = GlobalContext.World
)

Functions

func After

func After(filter string, fn func())

func AfterAll

func AfterAll(fn func())

func AfterMulti

func AfterMulti(filters []string, fn func())

func And

func And(match string, fn interface{})

func Before

func Before(filter string, fn func())

func BeforeAll

func BeforeAll(fn func())

func BeforeMulti

func BeforeMulti(filters []string, fn func())

func BuildAndRunDir

func BuildAndRunDir(dir string, filters []string) error

BuildAndRunDir builds the given director's features into Go Code. Using the filters provided. An error is returned if the build fails.

func BuildAndRunDirWithGoBuildTags

func BuildAndRunDirWithGoBuildTags(dir string, filters []string, goBuildTags string) error

BuildAndRunDirWithGoBuildTags builds the given director's features into Go Code using the filters provided. Also takes a string for the build tags to be passed to the go command. An error is returned if the build fails.

If goBuildTags is empty, the param will be ignored.

func Execute

func Execute(t Tester, line string, arg string) (bool, error)

func Given

func Given(match string, fn interface{})

func RunMain

func RunMain()

func Then

func Then(match string, fn interface{})

func When

func When(match string, fn interface{})

Types

type Context

type Context struct {
	Filters         []string
	World           map[string]interface{}
	BeforeFilters   map[string]func()
	AfterFilters    map[string]func()
	BeforeAllFilter func()
	AfterAllFilter  func()
	Steps           []StepDefinition
	T               Tester
}

func (*Context) After

func (c *Context) After(filter string, fn func())

func (*Context) AfterAll

func (c *Context) AfterAll(fn func())

func (*Context) AfterMulti

func (c *Context) AfterMulti(filters []string, fn func())

func (*Context) And

func (c *Context) And(match string, fn interface{})

func (*Context) Before

func (c *Context) Before(filter string, fn func())

func (*Context) BeforeAll

func (c *Context) BeforeAll(fn func())

func (*Context) BeforeMulti

func (c *Context) BeforeMulti(filters []string, fn func())

func (*Context) Execute

func (c *Context) Execute(t Tester, line string, arg string) (bool, error)

func (*Context) Given

func (c *Context) Given(match string, fn interface{})

func (*Context) RunDir

func (c *Context) RunDir(dir string) (*Runner, error)

func (*Context) RunFiles

func (c *Context) RunFiles(featureFiles []string) (*Runner, error)

func (*Context) Then

func (c *Context) Then(match string, fn interface{})

func (*Context) When

func (c *Context) When(match string, fn interface{})

type Runner

type Runner struct {
	*Context
	Features  []*gherkin.Feature
	Results   []RunnerResult
	Unmatched []*gherkin.Step
	Failures  []string
	FailCount int
	SkipCount int
}

func (*Runner) MissingMatcherStubs

func (c *Runner) MissingMatcherStubs() string

type RunnerResult

type RunnerResult struct {
	*TestingT
	*gherkin.Feature
	*gherkin.Scenario
}

type StepDefinition

type StepDefinition struct {
	Matcher  *regexp.Regexp
	Function reflect.Value
}

func (*StepDefinition) CallIfMatch

func (s *StepDefinition) CallIfMatch(c *Context, test Tester, line string, arg string) (bool, error)

type TestError

type TestError struct {
	// contains filtered or unexported fields
}

type Tester

type Tester interface {
	Errorf(format string, args ...interface{})
	Skip(args ...interface{})
}

type TestingT

type TestingT struct {
	// contains filtered or unexported fields
}

func (*TestingT) Error

func (t *TestingT) Error(err error)

func (*TestingT) Errorf

func (t *TestingT) Errorf(format string, args ...interface{})

func (*TestingT) Failed

func (t *TestingT) Failed() bool

func (*TestingT) Skip

func (t *TestingT) Skip(args ...interface{})

func (*TestingT) Skipped

func (t *TestingT) Skipped() bool

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

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