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 ¶
- func Given(setup func(T)) func(t *testing.T)
- type Checker
- func AsChecker[T any](matcher internal.Matcher[T], actual T) Checker
- func AsError(expect *error, actual error) Checker
- func AsErrorType[T error](actual error) Checker
- func AsNegativeChecker[T any](matcher internal.Matcher[T], actual T) Checker
- func Be[A any](actual, expect A) Checker
- func BeAssignableTo[E any](actual any) Checker
- func BeConvertibleTo[E any](actual any) Checker
- func BeFalse(a bool) Checker
- func BeGt[T cmp.Ordered](actual, expect T) Checker
- func BeGte[T cmp.Ordered](actual, expect T) Checker
- func BeLt[T cmp.Ordered](actual, expect T) Checker
- func BeLte[T cmp.Ordered](actual, expect T) Checker
- func BeNil[A any](a A) Checker
- func BeTrue(a bool) Checker
- func ConsistOfSlice[E comparable, S ~[]E](expect, actual S) Checker
- func Contains[E comparable, S ~[]E](s S, v E) Checker
- func ContainsSubString(s, sub string) Checker
- func Equal[A any](actual, expect A) Checker
- func EquivalentSlice[E comparable, S ~[]E](expect, actual S) Checker
- func ErrorContains(err error, sub string) Checker
- func ErrorEqual(actual error, expect string) Checker
- func Failed(err error) Checker
- func HaveCap[A any](a A, cap int) Checker
- func HaveKey[K comparable, V any, M ~map[K]V](m M, k K) Checker
- func HaveLen[A any](a A, len int) Checker
- func HavePrefix(s, prefix string) Checker
- func HaveSuffix(s, suffix string) Checker
- func IsCodeError[Code codex.Code](actual error, expect Code) Checker
- func IsError(expect, actual error) Checker
- func IsNotZero[A any](expect A) Checker
- func IsType[E any](actual any) Checker
- func IsZero[A any](expect A) Checker
- func MatchRegexp(pattern string, actual string) Checker
- func NegativeChecker[T any](c Checker) Checker
- func NotBe[A any](actual, expect A) Checker
- func NotBeNil[A any](a A) Checker
- func NotEqual[A any](actual, expect A) Checker
- func Succeed(err error) Checker
- type T
- type TB
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Checker ¶
type Checker interface {
Check(t TB)
}
Checker defines the interface for executing a test assertion.
func AsError ¶ added in v0.5.0
AsError asserts that the actual error can be assigned to the expected error target (using errors.As).
func AsErrorType ¶ added in v0.5.0
AsErrorType asserts that the actual error can be assigned to a specific error type T.
func AsNegativeChecker ¶
AsNegativeChecker creates a new Checker that negates the result of the given matcher.
func BeAssignableTo ¶
BeAssignableTo asserts that the actual value is assignable to the type E.
func BeConvertibleTo ¶
BeConvertibleTo asserts that the actual value is convertible to the type E.
func BeGte ¶ added in v0.5.0
BeGte asserts that the actual value is greater than or equal to the expected value.
func BeLte ¶ added in v0.5.0
BeLte asserts that the actual value is less than or equal to the expected value.
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 ¶
ContainsSubString asserts that the actual string contains the expected substring.
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 ¶
ErrorContains asserts that the actual error's message contains the expected substring.
func ErrorEqual ¶
ErrorEqual asserts that the actual error's message exactly matches the expected string.
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 HavePrefix ¶
HavePrefix asserts that the actual string starts with the expected prefix.
func HaveSuffix ¶
HaveSuffix asserts that the actual string ends with the expected suffix.
func IsCodeError ¶
IsCodeError asserts that the actual error implements codex.Error and matches the expected code.
func MatchRegexp ¶ added in v0.5.0
MatchRegexp asserts that the actual string matches the expected regular expression pattern.
func NegativeChecker ¶
NegativeChecker takes an existing Checker and negates its internal matcher.
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))
})
})