expectto/be is a Golang package that offers a substantial collection of Be matchers. Every Be matcher is
compatible with both Ginkgo/Gomega
and Gomock. Where possible, arguments of matchers can be either finite values or
matchers (Be/Gomega/Gomock).
Employing expectto/be matchers enables you to create straightforward, readable, and maintainable unit or
integration tests in Golang. Tasks such as testing HTTP requests, validating JSON responses, and more become remarkably
comprehensive and straightforward.
To use Be in your Golang project, simply import it:
import "github.com/expectto/be"Consider the following example demonstrating the usage of expectto/be's HTTP request matchers:
req, err := buildRequestForServiceFoo()
Expect(err).To(Succeed())
// Matching an HTTP request
Expect(req).To(be_http.Request(
// Matching the URL
be_http.HavingURL(be_url.URL(
be_url.WithHttps(),
be_url.HavingHost("example.com"),
be_url.HavingPath("/path"),
be_url.HavingSearchParam("status", "active"),
be_url.HavingSearchParam("v", be_reflected.AsNumericString()),
be_url.HavingSearchParam("q", "Hello World"),
)),
// Matching the HTTP method
be_http.POST(),
// Matching the request's context
be_http.HavingCtx(
be_ctx.CtxWithDeadline(be_time.LaterThan(time.Now().Add(30*time.Minute))),
be_ctx.CtxWithValue("foobar", 100),
),
// Matching the request body using JSON matchers
be_http.HavingBody(
be.JSON(
be_json.JsonAsReader,
be_json.HaveKeyValue("hello", "world"),
// NOTE: JSON numbers decode to float64, so use AsFloat (not AsInteger) here
be_json.HaveKeyValue("n", be_reflected.AsFloat(), be_math.GreaterThan(10)),
be_json.HaveKeyValue("ids", be_reflected.AsSliceOf[string]()),
Not(be_json.HaveKeyValue("deleted_field")), // not to have a deleted field
be_json.HaveKeyValue("email", be_string.ValidEmail(), HaveSuffix("@tests.com")),
// "details":[{"key":"foo"},{"key":"bar"}]
be_json.HaveKeyValue("details", And(
be_reflected.AsObjects(),
be.HaveLength(be_math.GreaterThan(2)),
ContainElements(
be_json.HaveKeyValue("key", "foo"),
be_json.HaveKeyValue("key", "bar"),
),
)),
),
),
// Matching HTTP headers
be_http.HavingHeader("X-Custom", "Hey-There"),
be_http.HavingHeader("Authorization",
be_string.MatchTemplate("Bearer {{jwt}}",
be_string.V("jwt",
be_jwt.Token(
be_jwt.Valid(),
be_jwt.HavingClaim("name", "John Doe"),
),
),
),
)) be matchers are framework-agnostic. The core github.com/expectto/be module
imports no test framework β pick how you run assertions:
Standard library (no extra deps). Two equivalent spellings β a fluent one and
a flat, testify-style one β both backed by the same engine, both driven by the
stdlib *testing.T:
import "github.com/expectto/be"
// fluent (ginkgo/gomega-flavored)
be.Expect(t, n).To(be_math.GreaterThan(10)) // soft fail (assert-style)
be.Require(t, n).To(be_math.GreaterThan(10)) // hard fail (require-style)
be.Expect(t, s).NotTo(be_string.EmptyString())
// flat (testify-flavored)
be.AssertThat(t, n, be_math.GreaterThan(10)) // soft fail (assert-style)
be.RequireThat(t, s, be_string.NonEmptyString()) // hard fail (require-style)Already on testify? Keep your assert/require calls and reach for be
only where a matcher earns its keep β be.AssertThat / be.RequireThat are the
drop-in slots (no extra dependency):
assert.Equal(t, want, got) // testify, as usual
be.AssertThat(t, got, be.Eq(want)) // be β and now `got` can face any matcher,
// e.g. be_url.URL(be_url.HavingHost("x"), ...)The subject comes first and the expected value lives inside the matcher (
be.Eq(want)), so β unlike testify'sEqual(t, want, got)β there's no want/got order to memorize or get wrong.
Ginkgo / Gomega: every be matcher already satisfies gomega's matcher
interface, so use it directly inside Expect(...).To(...).
be matchers also work as mock argument matchers:
Gomock: every be matcher already satisfies gomock.Matcher, so pass it directly:
mockObj.EXPECT().Do(be_math.GreaterThan(10)).Return("ok")Testify mock / mockery: wrap with MatchedBy (works for hand-written and
mockery-generated mocks) β the matcher equivalent of testify's own
mock.MatchedBy. This is the one place you need the separate x/mock module
(it's what keeps testify out of the core deps); install it with @latest (the
submodule shares version numbers with the core module, which confuses
go get <pkg>@<version>):
go get github.com/expectto/be/x/mock@latestimport bemock "github.com/expectto/be/x/mock"
svc.On("Do", bemock.MatchedBy(be_math.GreaterThan(10))).Return("ok")π¦ be provides a set of core matchers for common testing scenarios.
See detailed docs
Always, Never, All, Any, Eq, Not, HaveLength, Dive, DiveAny, DiveFirst
Nil, NotNil, True, False, Eq, Ne, Empty, NotEmpty, Identical, NotIdentical, Via, Succeed, HaveOccurred, MatchError, Panic, NotPanic, ContainElement, ContainElements, ContainSubstring, HaveKey, HaveKeyWithValue
π¦ be_reflected provides Be matchers that use reflection, enabling expressive assertions on values' reflect kinds and
types.
See detailed docs
AsKind, AsFunc, AsChan, AsPointer, AsFinalPointer, AsStruct, AsPointerToStruct, AsSlice, AsPointerToSlice, AsSliceOf, AsMap, AsPointerToMap, AsObject, AsObjects, AsPointerToObject
AsString, AsBytes, AsNumeric, AsNumericString, AsInteger, AsIntegerString, AsFloat, AsFloatishString,
AsReader,AsStringer
AssignableTo, Implementing
π¦ be_math provides Be matchers for mathematical operations.
See detailed docs
GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, Approx, InRange, Odd, Even, Negative, Positive, Zero, Integral, DivisibleBy
Gt, Gte, Lt, Lte
π¦ be_string provides Be matchers for string-related assertions.
See detailed docs
NonEmptyString, EmptyString, Alpha, Numeric, AlphaNumeric, AlphaNumericWithDots, Float, Titled, LowerCaseOnly, MatchWildcard, ValidEmail
MatchTemplate
π¦ be_time provides Be matchers on time.Time.
See detailed docs
LaterThan, LaterThanEqual, EarlierThan, EarlierThanEqual, Eq, Approx,
SameExactMilli, SameExactSecond, SameExactMinute, SameExactHour,
SameExactDay, SameExactWeekday, SameExactWeek, SameExactMonth,
SameSecond, SameMinute, SameHour, SameDay, SameYearDay,
SameWeek, SameMonth, SameYear, SameTimzone, SameOffset, IsDST
π¦ be_jwt provides Be matchers for handling JSON Web Tokens (JWT). It includes matchers for transforming and validating
JWT tokens. Matchers corresponds to specific
golang jwt implementation.
See detailed docs
TransformSignedJwtFromString, TransformJwtFromString
Token, Valid, HavingClaims, HavingClaim, HavingMethodAlg, SignedVia
π¦ be_url provides Be matchers on url.URL.
See detailed docs
TransformUrlFromString, TransformSchemelessUrlFromString
URL, Values, HavingHost, HavingHostname, HavingScheme, NotHavingScheme, WithHttps, WithHttp, HavingPort, NotHavingPort, HavingPath, HavingRawQuery, HavingSearchParam, NotHavingSearchParam, HavingMultipleSearchParam, HavingUsername, HavingUserinfo, HavingPassword
π¦ be_ctx provides Be matchers on context.Context.
See detailed docs
Ctx, CtxWithValue, CtxWithDeadline, CtxWithError
π¦ be_json provides Be matchers for expressive assertions on JSON.
See detailed docs
Matcher, HaveKeyValue
π¦ be_http provides Be matchers for expressive assertions on http.Request.
See detailed docs
Request, HavingMethod,
GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, CONNECT, TRACE,
HavingURL, HavingBody, HavingHost, HavingProto, HavingCtx, HavingHeader, HavingHeaders
Be welcomes contributions! Feel free to open issues, suggest improvements, or submit pull
requests. Contribution guidelines for this project
This project is licensed under the MIT License.