bdd

package
v0.5.7 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package bdd provides a Behavior-Driven Development (BDD) style testing framework.

It offers a domain-specific language (DSL) that is close to natural language, making test structures highly readable and expressive. The framework is built on Go's standard `testing` package and integrates seamlessly with `testing.T`.

Key Concepts

The BDD framework is structured around three main keywords:

  • Given: Sets up the initial state or preconditions for the test.
  • When: Defines the action, event, or behavior being tested.
  • Then: Asserts the expected outcomes using various Checkers.

Usage Example

You can start a BDD test by wrapping a standard `*testing.T` using `bdd.From(t)`.

func TestCalculator(t *testing.T) {
	bdd.From(t).
		Given("initial value is 1", func(t bdd.T) {
			v := 1

			t.When("we add 1", func(t bdd.T) {
				v += 1
				t.Then("it should equal 2", bdd.Equal(2, v))
			})

			t.When("we add 2", func(t bdd.T) {
				v += 2
				t.Then("it should equal 3", bdd.Equal(3, v))
				t.Then("it should not equal 4", bdd.NotEqual(4, v))
			})
		})
}

Alternatively, you can use the `bdd.Given` helper to create a test function directly:

func TestStringConversion(t *testing.T) {
	v := 0
	bdd.Given(func(t bdd.T) {
		v = 1
		t.Then(
			"the string representation should be '1'",
			bdd.Equal("1", strconv.Itoa(v)),
		)
	})(t)
}

Checkers

The `Then` step uses `Checker` interfaces to perform assertions. This package provides a wide range of built-in checkers (e.g., `Equal`, `BeNil`, `BeTrue`, `HaveLen`, `IsError`, etc.) which are wrappers around the matchers from the `github.com/xoctopus/x/testx` package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Given

func Given(setup func(T)) func(t *testing.T)

Given is a helper function that creates a standard testing function which initializes a BDD context and runs the provided setup function.

Types

type Checker

type Checker interface {
	Check(t TB)
}

Checker defines the interface for executing a test assertion.

func AsChecker

func AsChecker[T any](matcher internal.Matcher[T], actual T) Checker

AsChecker creates a new Checker from a given matcher and an actual value.

func AsError added in v0.5.0

func AsError(expect *error, actual error) Checker

AsError asserts that the actual error can be assigned to the expected error target (using errors.As).

func AsErrorType added in v0.5.0

func AsErrorType[T error](actual error) Checker

AsErrorType asserts that the actual error can be assigned to a specific error type T.

func AsNegativeChecker

func AsNegativeChecker[T any](matcher internal.Matcher[T], actual T) Checker

AsNegativeChecker creates a new Checker that negates the result of the given matcher.

func Be

func Be[A any](actual, expect A) Checker

Be asserts that the actual value is strictly identical to the expected value.

func BeAssignableTo

func BeAssignableTo[E any](actual any) Checker

BeAssignableTo asserts that the actual value is assignable to the type E.

func BeConvertibleTo

func BeConvertibleTo[E any](actual any) Checker

BeConvertibleTo asserts that the actual value is convertible to the type E.

func BeFalse

func BeFalse(a bool) Checker

BeFalse asserts that the actual boolean value is false.

func BeGt added in v0.5.0

func BeGt[T cmp.Ordered](actual, expect T) Checker

BeGt asserts that the actual value is greater than the expected value.

func BeGte added in v0.5.0

func BeGte[T cmp.Ordered](actual, expect T) Checker

BeGte asserts that the actual value is greater than or equal to the expected value.

func BeLt added in v0.5.0

func BeLt[T cmp.Ordered](actual, expect T) Checker

BeLt asserts that the actual value is less than the expected value.

func BeLte added in v0.5.0

func BeLte[T cmp.Ordered](actual, expect T) Checker

BeLte asserts that the actual value is less than or equal to the expected value.

func BeNil

func BeNil[A any](a A) Checker

BeNil asserts that the actual value is nil.

func BeTrue

func BeTrue(a bool) Checker

BeTrue asserts that the actual boolean value is true.

func ConsistOfSlice added in v0.5.0

func ConsistOfSlice[E comparable, S ~[]E](expect, actual S) Checker

ConsistOfSlice asserts that the actual slice has exactly the same elements in the same order as the expected slice.

func Contains

func Contains[E comparable, S ~[]E](s S, v E) Checker

Contains asserts that the actual slice contains the expected element.

func ContainsSubString

func ContainsSubString(s, sub string) Checker

ContainsSubString asserts that the actual string contains the expected substring.

func Equal

func Equal[A any](actual, expect A) Checker

Equal asserts that the actual value is deeply equal to the expected value.

func EquivalentSlice

func EquivalentSlice[E comparable, S ~[]E](expect, actual S) Checker

EquivalentSlice asserts that the actual slice has the same elements as the expected slice, regardless of order.

func ErrorContains

func ErrorContains(err error, sub string) Checker

ErrorContains asserts that the actual error's message contains the expected substring.

func ErrorEqual

func ErrorEqual(actual error, expect string) Checker

ErrorEqual asserts that the actual error's message exactly matches the expected string.

func Failed

func Failed(err error) Checker

Failed asserts that the actual error is not nil.

func HaveCap

func HaveCap[A any](a A, cap int) Checker

HaveCap asserts that the actual collection has the expected capacity.

func HaveKey added in v0.5.0

func HaveKey[K comparable, V any, M ~map[K]V](m M, k K) Checker

HaveKey asserts that the actual map contains the expected key.

func HaveLen

func HaveLen[A any](a A, len int) Checker

HaveLen asserts that the actual collection has the expected length.

func HavePrefix

func HavePrefix(s, prefix string) Checker

HavePrefix asserts that the actual string starts with the expected prefix.

func HaveSuffix

func HaveSuffix(s, suffix string) Checker

HaveSuffix asserts that the actual string ends with the expected suffix.

func IsCodeError

func IsCodeError[Code codex.Code](actual error, expect Code) Checker

IsCodeError asserts that the actual error implements codex.Error and matches the expected code.

func IsError

func IsError(expect, actual error) Checker

IsError asserts that the actual error is the expected error (using errors.Is).

func IsNotZero

func IsNotZero[A any](expect A) Checker

IsNotZero asserts that the actual value is not the zero value for its type.

func IsType added in v0.5.0

func IsType[E any](actual any) Checker

IsType asserts that the actual value is exactly of the type E.

func IsZero

func IsZero[A any](expect A) Checker

IsZero asserts that the actual value is the zero value for its type.

func MatchRegexp added in v0.5.0

func MatchRegexp(pattern string, actual string) Checker

MatchRegexp asserts that the actual string matches the expected regular expression pattern.

func NegativeChecker

func NegativeChecker[T any](c Checker) Checker

NegativeChecker takes an existing Checker and negates its internal matcher.

func NotBe

func NotBe[A any](actual, expect A) Checker

NotBe asserts that the actual value is not strictly identical to the expected value.

func NotBeNil

func NotBeNil[A any](a A) Checker

NotBeNil asserts that the actual value is not nil.

func NotEqual

func NotEqual[A any](actual, expect A) Checker

NotEqual asserts that the actual value is not deeply equal to the expected value.

func Succeed

func Succeed(err error) Checker

Succeed asserts that the actual error is nil.

type T

type T interface {
	TB

	// Given defines a precondition or initial state for the test.
	Given(preconditionSummary string, do func(t T))
	// When defines an action or event that triggers the behavior being tested.
	When(actionSummary string, do func(t T))
	// Then defines the expected outcome or assertions to be verified.
	Then(outcomeSummary string, checkers ...Checker)

	// Unwrap returns the underlying *testing.T instance.
	Unwrap() *testing.T
}

T defines a Behavior Driver Development testing. It provides a domain-specific language (DSL) that is close to natural language, making test structures read like:

bdd.From(t).Given("Precondition", func(b bdd.T) {
	// setup precondition
	b.When("SomeAction", func(b bdd.T) {
		// do action
		b.Then("Expects", bdd.Equal("1", v))
	})
})

func From

func From(t *testing.T) T

From wraps a standard *testing.T into a BDD-style T interface.

type TB

type TB = testing.TB

TB is an alias for testing.TB to simplify the interface signature.

Jump to

Keyboard shortcuts

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