gobdd

package module
v0.0.0-...-d0f23b2 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2021 License: MIT Imports: 17 Imported by: 0

README

GOBDD

GoDoc Coverage Status

This is a BDD testing framework. Uses gherkin for the test's syntax. From version 1.0, the API is stable.

Why did I make the library?

There is godog library for BDD tests in Go. I found this library useful but it runs as an external application which compiles our code. It has several disadvantages:

  • no debugging (breakpoints) in the test. Sometimes it’s useful to go through the whole execution step by step
  • metrics don’t count the test run this way
  • some style checkers recognise tests as dead code
  • it’s impossible to use built-in features like build constraints.
  • no context in steps - so the state have to be stored somewhere else - in my opinion, it makes the maintenance harder

Quick start

Add the package to your project:

go get github.com/go-bdd/gobdd

Inside features folder create your scenarios. Here is an example:

Feature: math operations
  Scenario: add two digits
    When I add 1 and 2
    Then I the result should equal 3

Add a new test main_test.go:

func add(t gobdd.StepTest, ctx gobdd.Context, var1, var2 int) {
	res := var1 + var2
	ctx.Set("sumRes", res)
}

func check(t gobdd.StepTest, ctx gobdd.Context, sum int) {
	received, err := ctx.GetInt("sumRes")
	if err != nil {
		t.Error(err)
		return
	}

	if sum != received {
		t.Error(errors.New("the math does not work for you"))
	}
}

func TestScenarios(t *testing.T) {
	suite := NewSuite(t)
	suite.AddStep(`I add (\d+) and (\d+)`, add)
	suite.AddStep(`I the result should equal (\d+)`, check)
	suite.Run()
}

and run tests

go test ./...

More detailed documentation can be found on the docs page: https://go-bdd.github.io/gobdd/. A sample application is available in a separate repository.

Contributing

All contributions are very much welcome. If you'd like to help with GoBDD development, please see open issues and submit your pull request via GitHub.

Support

If you didn't find the answer to your question in the documentation, feel free to ask us directly!

Please join us on the #gobdd-library channel on the Gophers slack: You can get an invite here. You can find updates about the progress on Twitter: GoBdd.

You can support my work using issuehunt or by buying me a coffee.

Documentation

Overview

Code generated .* DO NOT EDIT.

This project is created as the alternative to https://github.com/DATA-DOG/godog and is inspirited by it. There are a few differences between both solutions:

- GoBDD uses the built-in testing framework

- GoBDD is run as standard test case (not a separate program)

- you can use every Go native feature like build tags, pprof and so on

- the context in every test case contains all the required information to run (values passed from previous steps). More information can be found in the readme file https://github.com/go-bdd/gobdd/blob/master/README.md

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunInParallel

func RunInParallel() func(*SuiteOptions)

RunInParallel runs tests in parallel

func WithAfterScenario

func WithAfterScenario(f func(ctx Context)) func(*SuiteOptions)

WithAfterScenario configures functions that should be executed after every scenario

func WithAfterStep

func WithAfterStep(f func(ctx Context)) func(*SuiteOptions)

WithAfterStep configures functions that should be executed after every step

func WithBeforeScenario

func WithBeforeScenario(f func(ctx Context)) func(*SuiteOptions)

WithBeforeScenario configures functions that should be executed before every scenario

func WithBeforeStep

func WithBeforeStep(f func(ctx Context)) func(*SuiteOptions)

WithBeforeStep configures functions that should be executed before every step

func WithFeaturesPath

func WithFeaturesPath(path string) func(*SuiteOptions)

WithFeaturesPath configures a pattern (regexp) where feature can be found. The default value is "features/*.feature"

func WithIgnoredTags

func WithIgnoredTags(tags []string) func(*SuiteOptions)

WithIgnoredTags configures which tags should be skipped while executing a suite Every tag has to start with @ otherwise will be ignored

func WithTags

func WithTags(tags []string) func(*SuiteOptions)

WithTags configures which tags should be skipped while executing a suite Every tag has to start with @

Types

type Context

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

Holds data from previously executed steps

func NewContext

func NewContext() Context

Creates a new (empty) context struct

func (Context) Clone

func (ctx Context) Clone() Context

Clone creates a copy of the context

func (Context) Get

func (ctx Context) Get(key interface{}, defaultValue ...interface{}) (interface{}, error)

Returns the data under the key. If couldn't find anything but the default value is provided, returns the default value. Otherwise, it returns an error.

func (Context) GetBool

func (ctx Context) GetBool(key interface{}, defaultValue ...bool) (bool, error)

func (Context) GetError

func (ctx Context) GetError(key interface{}, defaultValue ...error) (error, error)

It is a shortcut for getting the value already casted as error.

func (Context) GetFloat32

func (ctx Context) GetFloat32(key interface{}, defaultValue ...float32) (float32, error)

func (Context) GetFloat64

func (ctx Context) GetFloat64(key interface{}, defaultValue ...float64) (float64, error)

func (Context) GetInt

func (ctx Context) GetInt(key interface{}, defaultValue ...int) (int, error)

func (Context) GetInt16

func (ctx Context) GetInt16(key interface{}, defaultValue ...int16) (int16, error)

func (Context) GetInt32

func (ctx Context) GetInt32(key interface{}, defaultValue ...int32) (int32, error)

func (Context) GetInt64

func (ctx Context) GetInt64(key interface{}, defaultValue ...int64) (int64, error)

func (Context) GetInt8

func (ctx Context) GetInt8(key interface{}, defaultValue ...int8) (int8, error)

func (Context) GetString

func (ctx Context) GetString(key interface{}, defaultValue ...string) (string, error)

func (Context) Set

func (ctx Context) Set(key interface{}, value interface{})

Sets the value under the key

type StepTest

type StepTest interface {
	Log(...interface{})
	Logf(string, ...interface{})
	Fatal(...interface{})
	Fatalf(string, ...interface{})
	Errorf(string, ...interface{})
	Error(...interface{})

	Fail()
	FailNow()
}

type Suite

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

Suite holds all the information about the suite (options, steps to execute etc)

func NewSuite

func NewSuite(t TestingT, optionClosures ...func(*SuiteOptions)) *Suite

Creates a new suites with given configuration and empty steps defined

func (*Suite) AddParameterTypes

func (s *Suite) AddParameterTypes(from string, to []string)

AddParameterTypes adds a list of parameter types that will be used to simplify step definitions.

The first argument is the parameter type and the second parameter is a list of regular expressions that should replace the parameter type.

s.AddParameterTypes(`{int}`, []string{`(\d)`})

The regular expression should compile, otherwise will produce an error and stop executing.

func (*Suite) AddRegexStep

func (s *Suite) AddRegexStep(expr *regexp.Regexp, step interface{})

AddRegexStep registers a step in the suite.

The second parameter is the step function that gets executed when a step definition matches the provided regular expression.

A step function can have any number of parameters (even zero), but it MUST accept a gobdd.StepTest and gobdd.Context as the first parameters (if there is any):

func myStepFunction(t gobdd.StepTest, ctx gobdd.Context, first int, second int) {
}

func (*Suite) AddStep

func (s *Suite) AddStep(expr string, step interface{})

AddStep registers a step in the suite.

The second parameter is the step function that gets executed when a step definition matches the provided regular expression.

A step function can have any number of parameters (even zero), but it MUST accept a gobdd.StepTest and gobdd.Context as the first parameters (if there is any):

func myStepFunction(t gobdd.StepTest, ctx gobdd.Context, first int, second int) {
}

func (*Suite) Run

func (s *Suite) Run()

Executes the suite with given options and defined steps

func (*Suite) WithJsonReport

func (s *Suite) WithJsonReport(filepath string)

type SuiteOptions

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

SuiteOptions holds all the information about how the suite or features/steps should be configured

func NewSuiteOptions

func NewSuiteOptions() SuiteOptions

NewSuiteOptions creates a new suite configuration with default values

type TestingT

type TestingT interface {
	StepTest
	Parallel()
	Run(name string, f func(t *testing.T)) bool
}

type TestingTKey

type TestingTKey struct{}

TestingTKey is used to store reference to current *testing.T instance

Directories

Path Synopsis
formatter

Jump to

Keyboard shortcuts

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