Documentation
¶
Overview ¶
Package sentrytest provides test fixtures for the Sentry Go SDK.
Fixture bundles a context, scope, client, and MockTransport with assertion helpers, eliminating boilerplate across integration and unit tests.
Isolated mode (default) creates a context and scope safe for parallel tests. Global mode (WithGlobal) calls sentry.Init for tests that use global state.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultEventCmpOpts = cmp.Options{ cmpopts.IgnoreFields(sentry.Event{}, "Contexts", "EventID", "Modules", "Platform", "Release", "Sdk", "ServerName", "Timestamp", ), cmpopts.IgnoreFields(sentry.Request{}, "Env"), cmpopts.IgnoreUnexported(sentry.Event{}), cmpopts.EquateEmpty(), }
DefaultEventCmpOpts are cmp.Options for comparing sentry.Event values. They ignore fields that vary between runs (IDs, timestamps, server metadata) and all unexported fields.
Functions ¶
func CheckRequestIsolation ¶
CheckRequestIsolation exercises sequential reuse and concurrent requests. request returns the completed request context and, optionally, its outer middleware context. Both contexts must be canceled when the request ends.
func NewContext ¶
NewContext creates a context backed by a new Fixture without a synctest bubble. If parent is nil, context.Background is used.
func Run ¶
Run creates a Fixture inside a synctest.Test bubble and calls fn with it. All background goroutines (batch processors) use fake time, so Fixture.Flush completes instantly. This is the preferred way to create fixtures.
For the rare case where synctest is incompatible (e.g. third-party code that leaks goroutines), use NewFixture directly.
Types ¶
type Fixture ¶
type Fixture struct {
// T is the test context.
T testing.TB
// Context carries the fixture's isolated scope and client.
Context context.Context
// Scope is the fixture's isolated scope.
Scope *sentry.Scope
// Client is the fixture's client, configured with the MockTransport.
Client *sentry.Client
// Transport captures all events sent through the client.
Transport *sentry.MockTransport
// contains filtered or unexported fields
}
Fixture provides an isolated Sentry environment for testing. It bundles a context, scope, client, and MockTransport with assertion helpers.
func NewFixture ¶
NewFixture creates a new test fixture without a synctest bubble. Prefer Run for most tests; use this only when synctest is incompatible.
func (*Fixture) AssertEventCount ¶
AssertEventCount flushes and asserts the number of captured events.
func (*Fixture) DiffEvents ¶
DiffEvents flushes and returns a cmp.Diff of captured events against want. Uses DefaultEventCmpOpts merged with any additional opts. Returns "" when events match.
func (*Fixture) Events ¶
Events returns all captured events, including transactions.
TODO: Add typed helper views (errors, transactions, logs, metrics, check-ins).
func (*Fixture) Flush ¶
func (f *Fixture) Flush()
Flush flushes the fixture's client.
Inside a Run bubble it calls synctest.Wait first to let all background goroutines settle, then flushes under fake time (completing instantly). Outside a bubble it awaits for a real Flush.
func (*Fixture) NewContext ¶
NewContext returns parent with a fresh fixture scope and client. If parent is nil, context.Background is used.
type Option ¶
type Option func(*config)
Option configures a Fixture.
func WithClientOptions ¶
func WithClientOptions(opts sentry.ClientOptions) Option
WithClientOptions sets the sentry.ClientOptions for the fixture. Transport is always overridden to use the fixture's mock transport. If Dsn is empty, the fixture sets a placeholder test DSN.
func WithGlobal ¶
func WithGlobal() Option
WithGlobal makes the fixture call sentry.Init to set the global client instead of creating an isolated client context.
Tests using global mode must not run in parallel.